ed
json_writer.h
Go to the documentation of this file.
1 #ifndef ED_IO_JSON_WRITER_H_
2 #define ED_IO_JSON_WRITER_H_
3 
4 #include "ed/io/writer.h"
5 
6 #include <sstream>
7 #include <vector>
8 
9 namespace ed
10 {
11 
12 namespace io
13 {
14 
15 class JSONWriter : public Writer
16 {
17 
18 public:
19 
21  {
22  out << "{";
23  }
24 
26 
27  void writeGroup(const std::string& name)
28  {
29  if (add_comma_)
30  out_ << ",";
31 
32  out_ << "\"" << name << "\":{";
34  add_comma_ = false;
35  }
36 
37  void endGroup()
38  {
39  out_ << "}";
40  if (type_stack_.empty() || type_stack_.back() != 'g')
41  std::cout << "JSONWriter::endArray(): no group to close." << std::endl;
42  else
44  add_comma_ = true;
45  }
46 
47  void writeValue(const std::string& key, float f)
48  {
49  if (add_comma_)
50  out_ << ",";
51 
52  out_ << "\"" << key << "\":" << f;
53  add_comma_ = true;
54  }
55 
56  void writeValue(const std::string& key, int i)
57  {
58  if (add_comma_)
59  out_ << ",";
60 
61  out_ << "\"" << key << "\":" << i;
62  add_comma_ = true;
63  }
64 
65  void writeValue(const std::string& key, const std::string& s)
66  {
67  if (add_comma_)
68  out_ << ",";
69 
70  out_ << "\"" << key << "\":\"" << s << "\"";
71  add_comma_ = true;
72  }
73 
74  void writeValue(const std::string& key, double d)
75  {
76  if (add_comma_)
77  out_ << ",";
78 
79  out_ << "\"" << key << "\":" << d;
80  add_comma_ = true;
81  }
82 
83  void writeValue(const std::string& key, const float* fs, std::size_t size)
84  {
85  if (add_comma_)
86  out_ << ",";
87 
88  out_ << "\"" << key << "\":[";
89 
90  if (size > 0)
91  {
92  out_ << fs[0];
93  for(unsigned int i = 1; i < size; ++i)
94  out_ << "," << fs[i];
95  }
96  out_ << "]";
97  }
98 
99  void writeValue(const std::string& key, const int* is, std::size_t size)
100  {
101  if (add_comma_)
102  out_ << ",";
103 
104  out_ << "\"" << key << "\":[";
105 
106  if (size > 0)
107  {
108  out_ << is[0];
109  for(unsigned int i = 1; i < size; ++i)
110  out_ << "," << is[i];
111  }
112  out_ << "]";
113  }
114 
115  void writeValue(const std::string& key, const std::string* ss, std::size_t size)
116  {
117  if (add_comma_)
118  out_ << ",";
119 
120  out_ << "\"" << key << "\":[";
121 
122  if (size > 0)
123  {
124  out_ << "\"" << ss[0] << "\"";
125  for(unsigned int i = 1; i < size; ++i)
126  out_ << "\"" << ss[i] << "\"";
127  }
128  out_ << "]";
129  }
130 
131  void writeArray(const std::string& key)
132  {
133  if (add_comma_)
134  out_ << ",";
135 
136  out_ << "\"" << key << "\":[";
137  type_stack_.push_back('a');
138  add_comma_ = false;
139  }
140 
142  {
143  if (add_comma_)
144  out_ << ",";
145 
146  out_ << "{";
147  type_stack_.push_back('i');
148  add_comma_ = false;
149  }
151  {
152  out_ << "}";
153  if (type_stack_.empty() || type_stack_.back() != 'i')
154  std::cout << "JSONWriter::endArray(): no array item to close." << std::endl;
155  else
157  add_comma_ = true;
158  }
159 
160  void endArray()
161  {
162  out_ << "]";
163  if (type_stack_.empty() || type_stack_.back() != 'a')
164  std::cout << "JSONWriter::endArray(): no array to close." << std::endl;
165  else
167  add_comma_ = true;
168  }
169 
170  void finish()
171  {
172  while(!type_stack_.empty())
173  {
174  char t = type_stack_.back();
176 
177  if (t == 'g')
178  endGroup();
179  else if (t == 'i')
180  endArrayItem();
181  else if (t == 'a')
182  endArray();
183  }
184  out_ << "}";
185  }
186 
187 private:
188 
191 
192 };
193 
194 }
195 
196 } // end namespace era
197 
198 #endif
sstream
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, const float *fs, std::size_t size)
Definition: json_writer.h:83
ed::io::JSONWriter::writeArray
void writeArray(const std::string &key)
Definition: json_writer.h:131
ed::io::JSONWriter::endArrayItem
void endArrayItem()
Definition: json_writer.h:150
ed::io::JSONWriter::add_comma_
bool add_comma_
Definition: json_writer.h:189
std::string
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, float f)
Definition: json_writer.h:47
t
Timer t
vector
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, int i)
Definition: json_writer.h:56
ed::io::JSONWriter::writeGroup
void writeGroup(const std::string &name)
Definition: json_writer.h:27
std::vector::back
T back(T... args)
ed::io::Writer::out_
std::ostream & out_
Definition: writer.h:51
ed::io::JSONWriter::~JSONWriter
~JSONWriter()
Definition: json_writer.h:25
std::vector::push_back
T push_back(T... args)
std::cout
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, double d)
Definition: json_writer.h:74
ed::io::JSONWriter::endArray
void endArray()
Definition: json_writer.h:160
std::ostream
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, const std::string *ss, std::size_t size)
Definition: json_writer.h:115
ed::io::JSONWriter::endGroup
void endGroup()
Definition: json_writer.h:37
ed::io::JSONWriter::JSONWriter
JSONWriter(std::ostream &out)
Definition: json_writer.h:20
ed::io::JSONWriter::type_stack_
std::vector< char > type_stack_
Definition: json_writer.h:190
std::vector::pop_back
T pop_back(T... args)
ed::io::Writer
Definition: writer.h:17
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, const std::string &s)
Definition: json_writer.h:65
ed::io::JSONWriter::finish
void finish()
Definition: json_writer.h:170
std::endl
T endl(T... args)
ed::io::JSONWriter
Definition: json_writer.h:15
std::vector::empty
T empty(T... args)
ed
Definition: convex_hull.h:8
std::size_t
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, const int *is, std::size_t size)
Definition: json_writer.h:99
ed::io::JSONWriter::addArrayItem
void addArrayItem()
Definition: json_writer.h:141
writer.h