14 inline void level_indent(std::ostream& s,
unsigned int level) {
15 while(level--) s <<
" ";
27 virtual void str(std::ostream& s,
unsigned int level = 0)
const = 0;
32 inline std::string
str()
const {
33 std::ostringstream oss;
50 inline TValue(
const T& value) : m_value(value) {};
55 virtual inline void str(
56 std::ostream& s,
unsigned int = 0)
const override {
63 const char quote_char =
'\"';
64 const std::string quote_escape =
"\\\"";
70 if(m_value == quote_char) {
81 m_value = std::string(value);
85 for(
size_t x = m_value.find(quote_char);
86 x != std::string::npos;
87 x = m_value.find(quote_char, pos)) {
89 m_value.replace(x, 1, quote_escape);
96 s << quote_char << m_value << quote_char;
105 std::vector<std::shared_ptr<Value>> m_values;
113 inline void add(
const T& value) {
114 m_values.push_back(std::make_shared<
TValue<T>>(value));
120 inline void add(
const char* value) {
121 add(std::string(value));
128 m_values.push_back(std::make_shared<Array>(value));
134 void add(
const Object& obj);
141 virtual inline void str(std::ostream& s,
unsigned int level = 0)
const override {
143 auto end = m_values.end();
144 for(
auto it = m_values.begin(); it != end; ++it) {
145 (*it)->str(s, level);
146 if(std::next(it) != end) s <<
',';
155 std::map<std::string, std::shared_ptr<Value>> m_fields;
164 inline void set(
const std::string& key,
const T& value) {
165 m_fields[key] = std::make_shared<TValue<T>>(value);
172 inline void set(
const std::string& key,
const char* value) {
173 set(key, std::string(value));
180 inline void set(
const std::string& key,
const Object& value) {
181 m_fields[key] = std::make_shared<Object>(value);
188 inline void set(
const std::string& key,
const Array& value) {
189 m_fields[key] = std::make_shared<Array>(value);
197 virtual inline void str(std::ostream& s,
unsigned int level = 0)
const override {
198 s <<
'{' << std::endl;
200 auto end = m_fields.end();
201 for(
auto it = m_fields.begin(); it != end; ++it) {
202 level_indent(s, level + 1);
204 s <<
'\"' << it->first <<
"\": ";
205 it->second->str(s, level + 1);
207 if(std::next(it) != end) s <<
',';
211 level_indent(s, level);
218 inline std::string
str()
const {
225 m_values.push_back(std::make_shared<Object>(value));
std::string str() const
Returns the object's JSON string representation.
Contains the text compression and encoding framework.
virtual void str(std::ostream &s, unsigned int level=0) const override
Writes the array's JSON string representation into the given stream.
virtual void str(std::ostream &s, unsigned int=0) const override
Yields the JSON string representation of the contained value.
std::string str() const
Returns this value's JSON string representation.
Represents an array of values.
void add(const T &value)
Adds a value to the array.
Represents a JSON object that behaves like a dictionary.
Template for containers of values that are supported by the std::ostream left shift operator for stri...
void add(const Array &value)
Adds an array object to the array.
virtual void str(std::ostream &s, unsigned int level=0) const override
Writes the object's JSON string representation into the given stream.
Represents a single value that can be represented as a JSON string.
TValue(const T &value)
Constructs a value container.
void add(const char *value)
Adds a C string value to the array.