5 #ifndef CVMFS_JSON_DOCUMENT_WRITE_H_
6 #define CVMFS_JSON_DOCUMENT_WRITE_H_
15 #ifdef CVMFS_NAMESPACE_GUARD
16 namespace CVMFS_NAMESPACE_GUARD {
39 JsonEntry(
const std::string& key_escaped,
const std::string& val)
41 key_escaped(key_escaped),
46 JsonEntry(
const std::string& key_escaped,
const std::string& val,
49 key_escaped(key_escaped),
54 JsonEntry(
const std::string& key_escaped,
const int val)
56 key_escaped(key_escaped),
61 JsonEntry(
const std::string& key_escaped,
const float val)
63 key_escaped(key_escaped),
68 JsonEntry(
const std::string& key_escaped,
const int64_t val)
70 key_escaped(key_escaped),
78 return "\"" + key_escaped +
"\":\"" + str_val_escaped +
"\"";
80 return "\"" + key_escaped +
"\":" +
StringifyInt(int_val);
84 return "\"" + key_escaped +
"\":" + str_val_escaped;
92 void Add(
const std::string& key,
const std::string& val) {
93 const JsonEntry entry(Escape(key), Escape(val));
94 entries.push_back(entry);
97 void Add(
const std::string& key,
const int val) {
99 entries.push_back(entry);
102 void Add(
const std::string& key,
const float val) {
104 entries.push_back(entry);
107 void Add(
const std::string& key,
const int64_t val) {
109 entries.push_back(entry);
114 const JsonEntry entry(Escape(key), json, kJsonObject);
115 entries.push_back(entry);
122 for (
size_t i = 0u; i < this->entries.size(); ++i) {
123 output += this->entries[i].Format();
124 if (i < this->entries.size() - 1) {
128 output += std::string(
"}");
141 const std::string
Escape(
const std::string& input)
const {
143 result.reserve(input.size());
144 for (
size_t i = 0; i < input.size(); i++) {
147 result.append(
"\\\"");
150 result.append(
"\\\\");
153 result.append(
"\\b");
156 result.append(
"\\f");
159 result.append(
"\\n");
162 result.append(
"\\r");
165 result.append(
"\\t");
168 result.push_back(input[i]);
178 #ifdef CVMFS_NAMESPACE_GUARD
182 #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)