5 #ifndef CVMFS_JSON_DOCUMENT_WRITE_H_
6 #define CVMFS_JSON_DOCUMENT_WRITE_H_
15 #ifdef CVMFS_NAMESPACE_GUARD
16 namespace CVMFS_NAMESPACE_GUARD {
44 JsonEntry(
const std::string &key_escaped,
const std::string &val)
46 , key_escaped(key_escaped)
47 , str_val_escaped(val)
51 JsonEntry(
const std::string &key_escaped,
const std::string &val,
54 , key_escaped(key_escaped)
55 , str_val_escaped(val)
59 JsonEntry(
const std::string &key_escaped,
const int val)
61 , key_escaped(key_escaped)
66 JsonEntry(
const std::string &key_escaped,
const float val)
68 , key_escaped(key_escaped)
73 JsonEntry(
const std::string &key_escaped,
const int64_t val)
75 , key_escaped(key_escaped)
83 return "\"" + key_escaped +
"\":\"" + str_val_escaped +
"\"";
85 return "\"" + key_escaped +
"\":" +
StringifyInt(int_val);
89 return "\"" + key_escaped +
"\":" + str_val_escaped;
97 void Add(
const std::string &key,
const std::string &val) {
98 const JsonEntry entry(Escape(key), Escape(val));
99 entries.push_back(entry);
102 void Add(
const std::string &key,
const int val) {
104 entries.push_back(entry);
107 void Add(
const std::string &key,
const float val) {
109 entries.push_back(entry);
112 void Add(
const std::string &key,
const int64_t val) {
114 entries.push_back(entry);
119 const JsonEntry entry(Escape(key), json, kJsonObject);
120 entries.push_back(entry);
127 for (
size_t i = 0u; i < this->entries.size(); ++i) {
128 output += this->entries[i].Format();
129 if (i < this->entries.size() - 1) {
133 output += std::string(
"}");
144 const std::string
Escape(
const std::string &input)
const {
146 result.reserve(input.size());
147 for (
size_t i = 0; i < input.size(); i++) {
150 result.append(
"\\\"");
153 result.append(
"\\\\");
156 result.append(
"\\b");
159 result.append(
"\\f");
162 result.append(
"\\n");
165 result.append(
"\\r");
168 result.append(
"\\t");
171 result.push_back(input[i]);
181 #ifdef CVMFS_NAMESPACE_GUARD
185 #endif // CVMFS_JSON_DOCUMENT_WRITE_H_
void Add(const std::string &key, const float val)
void Add(const std::string &key, const std::string &val)
JsonEntry(const std::string &key_escaped, const int val)
const std::string Escape(const std::string &input) const
string StringifyDouble(const double value)
std::vector< JsonEntry > entries
std::string str_val_escaped
JsonEntry(const std::string &key_escaped, const float val)
string StringifyInt(const int64_t value)
std::string Format() const
void Add(const std::string &key, const int val)
JsonEntry(const std::string &key_escaped, const int64_t val)
void Add(const std::string &key, const int64_t val)
JsonEntry(const std::string &key_escaped, const std::string &val)
std::string GenerateString() const
void AddJsonObject(const std::string &key, const std::string &json)
JsonEntry(const std::string &key_escaped, const std::string &val, const JsonVariant variant)