tudocomp
– The TU Dortmund Compression Framework
Json.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <vector>
6 #include <sstream>
7 
8 namespace tdc {
9 
11 namespace json {
12 
14 inline void level_indent(std::ostream& s, unsigned int level) {
15  while(level--) s << " ";
16 }
18 
20 class Value {
21 public:
27  virtual void str(std::ostream& s, unsigned int level = 0) const = 0;
28 
32  inline std::string str() const {
33  std::ostringstream oss;
34  str(oss);
35  return oss.str();
36  }
37 };
38 
43 template<typename T>
44 class TValue : public Value {
45 private:
46  T m_value;
47 
48 public:
50  inline TValue(const T& value) : m_value(value) {};
51 
55  virtual inline void str(
56  std::ostream& s, unsigned int = 0) const override {
57 
58  s << m_value;
59  }
60 };
61 
63 const char quote_char = '\"';
64 const std::string quote_escape = "\\\"";
65 
66 template<>
67 inline void TValue<char>::str(std::ostream& s, unsigned int) const {
68  s << quote_char;
69 
70  if(m_value == quote_char) {
71  s << quote_escape;
72  } else {
73  s << m_value;
74  }
75 
76  s << quote_char;
77 }
78 
79 template<>
80 inline TValue<std::string>::TValue(const std::string& value) {
81  m_value = std::string(value);
82 
83  // escape quote character
84  size_t pos = 0;
85  for(size_t x = m_value.find(quote_char);
86  x != std::string::npos;
87  x = m_value.find(quote_char, pos)) {
88 
89  m_value.replace(x, 1, quote_escape);
90  pos = x+2;
91  }
92 }
93 
94 template<>
95 inline void TValue<std::string>::str(std::ostream& s, unsigned int) const {
96  s << quote_char << m_value << quote_char;
97 }
99 
100 class Object;
101 
103 class Array : public Value {
104 private:
105  std::vector<std::shared_ptr<Value>> m_values;
106 
107 public:
112  template<typename T>
113  inline void add(const T& value) {
114  m_values.push_back(std::make_shared<TValue<T>>(value));
115  }
116 
120  inline void add(const char* value) {
121  add(std::string(value));
122  }
123 
127  inline void add(const Array& value) {
128  m_values.push_back(std::make_shared<Array>(value));
129  }
130 
134  void add(const Object& obj); //defined below
135 
141  virtual inline void str(std::ostream& s, unsigned int level = 0) const override {
142  s << '[';
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 << ',';
147  }
148  s << ']';
149  }
150 };
151 
153 class Object : public Value {
154 private:
155  std::map<std::string, std::shared_ptr<Value>> m_fields;
156 
157 public:
163  template<typename T>
164  inline void set(const std::string& key, const T& value) {
165  m_fields[key] = std::make_shared<TValue<T>>(value);
166  }
167 
172  inline void set(const std::string& key, const char* value) {
173  set(key, std::string(value));
174  }
175 
180  inline void set(const std::string& key, const Object& value) {
181  m_fields[key] = std::make_shared<Object>(value);
182  }
183 
188  inline void set(const std::string& key, const Array& value) {
189  m_fields[key] = std::make_shared<Array>(value);
190  }
191 
197  virtual inline void str(std::ostream& s, unsigned int level = 0) const override {
198  s << '{' << std::endl;
199 
200  auto end = m_fields.end();
201  for(auto it = m_fields.begin(); it != end; ++it) {
202  level_indent(s, level + 1);
203 
204  s << '\"' << it->first << "\": ";
205  it->second->str(s, level + 1);
206 
207  if(std::next(it) != end) s << ',';
208  s << std::endl;
209  }
210 
211  level_indent(s, level);
212  s << '}';
213  }
214 
218  inline std::string str() const {
219  return Value::str();
220  }
221 };
222 
224 inline void Array::add(const Object& value) {
225  m_values.push_back(std::make_shared<Object>(value));
226 }
228 
229 }} //ns
std::string str() const
Returns the object&#39;s JSON string representation.
Definition: Json.hpp:218
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
virtual void str(std::ostream &s, unsigned int level=0) const override
Writes the array&#39;s JSON string representation into the given stream.
Definition: Json.hpp:141
virtual void str(std::ostream &s, unsigned int=0) const override
Yields the JSON string representation of the contained value.
Definition: Json.hpp:55
std::string str() const
Returns this value&#39;s JSON string representation.
Definition: Json.hpp:32
Represents an array of values.
Definition: Json.hpp:103
void add(const T &value)
Adds a value to the array.
Definition: Json.hpp:113
len_compact_t pos
Definition: LZSSFactors.hpp:38
Represents a JSON object that behaves like a dictionary.
Definition: Json.hpp:153
Template for containers of values that are supported by the std::ostream left shift operator for stri...
Definition: Json.hpp:44
void add(const Array &value)
Adds an array object to the array.
Definition: Json.hpp:127
virtual void str(std::ostream &s, unsigned int level=0) const override
Writes the object&#39;s JSON string representation into the given stream.
Definition: Json.hpp:197
Represents a single value that can be represented as a JSON string.
Definition: Json.hpp:20
TValue(const T &value)
Constructs a value container.
Definition: Json.hpp:50
void add(const char *value)
Adds a C string value to the array.
Definition: Json.hpp:120