HAL
json_write_document.cpp
Go to the documentation of this file.
2 
3 #include "rapidjson/filereadstream.h"
4 #include "rapidjson/stringbuffer.h"
5 
6 #include <fstream>
7 
8 #define PRETTY_JSON_OUTPUT 0
9 #if PRETTY_JSON_OUTPUT == 1
10 #include "rapidjson/prettywriter.h"
11 #else
12 #include "rapidjson/writer.h"
13 #endif
14 
15 namespace hal
16 {
17  //--- Data -------
18  JsonWriteData::JsonWriteData(const std::string& tag, JsonWriteComplex* parent) : mTagname(tag), mParent(parent)
19  {
20  ;
21  }
22 
23  rapidjson::Document::AllocatorType& JsonWriteData::allocator()
24  {
26  if (!p)
27  p = static_cast<JsonWriteObject*>(this); // no parent: must be document
28  else
29  while (p->mParent)
30  {
31  p = p->mParent;
32  assert(p != p->mParent);
33  }
34  JsonWriteDocument* doc = dynamic_cast<JsonWriteDocument*>(p);
35  assert(doc);
36  return doc->allocator();
37  }
38 
39  JsonWriteData& JsonWriteData::operator=(const std::string& txt)
40  {
41  JsonWriteObject* p = dynamic_cast<JsonWriteObject*>(mParent);
42  assert(p);
43  p->add_member(rapidjson::Value(mTagname, allocator()), rapidjson::Value(txt, allocator()), allocator());
44  return *this;
45  }
46 
48  {
49  JsonWriteObject* p = dynamic_cast<JsonWriteObject*>(mParent);
50  assert(p);
51  p->add_member(rapidjson::Value(mTagname, allocator()), rapidjson::Value(ivalue), allocator());
52  return *this;
53  }
54 
56  {
57  JsonWriteObject* p = dynamic_cast<JsonWriteObject*>(mParent);
58  assert(p);
59  p->add_member(rapidjson::Value(mTagname, allocator()), rapidjson::Value(u64Value), allocator());
60  return *this;
61  }
62 
64  {
65  JsonWriteObject* p = dynamic_cast<JsonWriteObject*>(mParent);
66  assert(p);
67  p->add_member(rapidjson::Value(mTagname, allocator()), rapidjson::Value(value), allocator());
68  return *this;
69  }
70 
71  //--- Complex ----
72  JsonWriteComplex::JsonWriteComplex(const std::string& tag, JsonWriteComplex* parent) : JsonWriteData(tag, parent)
73  {
74  ;
75  }
76 
78  {
79  for (JsonWriteData* obj : mChildData)
80  delete obj;
81  mChildData.clear();
82  }
83 
85  {
86  mParent->finalize(this);
87  }
88 
89  //--- Object -----
90  JsonWriteObject::JsonWriteObject(const std::string& tag, JsonWriteComplex* parent) : JsonWriteComplex(tag, parent)
91  {
92  mRapidValue.SetObject();
93  }
94 
95  void JsonWriteObject::add_member(rapidjson::Document::ValueType&& name, rapidjson::Document::ValueType&& value, rapidjson::Document::AllocatorType& allocator)
96  {
97  mRapidValue.AddMember(name, value, allocator);
98  }
99 
101  {
102  JsonWriteData* dat = new JsonWriteData(tag, this);
103  mChildData.push_back(dat);
104  return *dat;
105  }
106 
108  {
109  JsonWriteObject* obj = new JsonWriteObject(tag, this);
110  mChildData.push_back(obj);
111  return *obj;
112  }
113 
115  {
116  JsonWriteArray* arr = new JsonWriteArray(tag, this);
117  mChildData.push_back(arr);
118  return *arr;
119  }
120 
122  {
123  mRapidValue.AddMember(rapidjson::Value(cplx->mTagname, allocator()), cplx->mRapidValue, allocator());
124  }
125 
126  //--- Array ------
127  JsonWriteArray::JsonWriteArray(const std::string& tag, JsonWriteComplex* parent) : JsonWriteComplex(tag, parent)
128  {
129  mRapidValue.SetArray();
130  }
131 
133  {
134  mRapidValue.PushBack(rapidjson::Value(txt, allocator()), allocator());
135  return *this;
136  }
137 
139  {
140  mRapidValue.PushBack(rapidjson::Value(ivalue), allocator());
141  return *this;
142  }
143 
145  {
146  JsonWriteArray* arr = new JsonWriteArray(std::string(), this);
147  mChildData.push_back(arr);
148  return *arr;
149  }
150 
152  {
153  JsonWriteObject* obj = new JsonWriteObject(std::string(), this);
154  mChildData.push_back(obj);
155  return *obj;
156  }
157 
159  {
160  mRapidValue.PushBack(cplx->mRapidValue, allocator());
161  }
162 
163  //--- Document ---
165  {
166  mRapidDocument.SetObject();
167  }
168 
169  rapidjson::Document::AllocatorType& JsonWriteDocument::allocator()
170  {
171  return mRapidDocument.GetAllocator();
172  }
173 
175  {
176  mRapidDocument.AddMember(rapidjson::Value(cplx->mTagname, allocator()), cplx->mRapidValue, allocator());
177  }
178 
179  void JsonWriteDocument::add_member(rapidjson::Document::ValueType&& name, rapidjson::Document::ValueType&& value, rapidjson::Document::AllocatorType& allocator)
180  {
181  mRapidDocument.AddMember(name, value, allocator);
182  }
183 
184  bool JsonWriteDocument::serialize(const std::string& filename)
185  {
186  std::ofstream of(filename);
187  if (!of.good())
188  return false;
189 
190  rapidjson::StringBuffer strbuf;
191 #if PRETTY_JSON_OUTPUT == 1
192  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
193 #else
194  rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
195 #endif
196  mRapidDocument.Accept(writer);
197  of << strbuf.GetString();
198  of.close();
199  return true;
200  }
201 
203  {
205  }
206 
207  void JsonWriteDocument::dump(rapidjson::Value& parent)
208  {
209  UNUSED(parent);
210  /*
211  for (rapidjson::Document::MemberIterator it = parent.MemberBegin(); it!=parent.MemberEnd(); ++it)
212  {
213  qDebug() << "x" << it->name.GetString();
214  switch(it->value.GetType())
215  {
216  case rapidjson::kNumberType:
217  qDebug() << it->value.GetInt();
218  break;
219  case rapidjson::kStringType:
220  qDebug() << it->value.GetString();
221  break;
222  case rapidjson::kObjectType:
223  if (parent.HasMember("name"))
224  qDebug() << "***name***" << parent["name"].GetString();
225  dump(it->value);
226  break;
227  case rapidjson::kArrayType:
228  for (rapidjson::Document::ConstValueIterator jt = it->value.Begin(); jt!=it->value.End(); ++jt)
229  qDebug() << jt->GetString();
230  break;
231  default:
232  qDebug() << "type not handled" << it->value.GetType();
233  break;
234  }
235  }
236  */
237  }
238 
239  std::unordered_map<std::string, std::string> JsonConverter::stringToDictionary(const std::string& json_string)
240  {
241  std::unordered_map<std::string, std::string> retval;
242  rapidjson::Document doc;
243  doc.Parse(json_string.c_str());
244  if (doc.IsObject())
245  {
246  for (rapidjson::Value::ConstMemberIterator it = doc.GetObject().MemberBegin(); it != doc.GetObject().MemberEnd(); ++it)
247  {
248  retval[it->name.GetString()] = it->value.GetString();
249  }
250  }
251  return retval;
252  }
253 
254  std::string JsonConverter::dictionaryToString(const std::unordered_map<std::string, std::string>& key_values)
255  {
256  rapidjson::StringBuffer s;
257  rapidjson::Writer<rapidjson::StringBuffer> writer(s);
258  rapidjson::Document d;
259  d.SetObject();
260  for (auto it = key_values.begin(); it != key_values.end(); ++it)
261  {
262  rapidjson::Value k(it->first, d.GetAllocator());
263  rapidjson::Value v(it->second, d.GetAllocator());
264  d.AddMember(k, v, d.GetAllocator());
265  }
266  d.Accept(writer);
267  return s.GetString();
268  }
269 } // namespace hal
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf of
JsonWriteArray & operator<<(const std::string &txt)
virtual void finalize(JsonWriteComplex *cplx) override
JsonWriteObject & add_object()
JsonWriteArray & add_array()
std::vector< JsonWriteData * > mChildData
rapidjson::Value mRapidValue
virtual void finalize(JsonWriteComplex *cplx)=0
JsonWriteData(const std::string &tag, JsonWriteComplex *parent)
JsonWriteComplex * mParent
friend class JsonWriteComplex
JsonWriteData & operator=(const std::string &txt)
virtual rapidjson::Document::AllocatorType & allocator()
bool serialize(const std::string &filename)
rapidjson::Document mRapidDocument
virtual void finalize(JsonWriteComplex *cplx) override
virtual void add_member(rapidjson::Document::ValueType &&name, rapidjson::Document::ValueType &&value, rapidjson::Document::AllocatorType &allocator) override
virtual rapidjson::Document::AllocatorType & allocator() override
JsonWriteData & operator[](const std::string &tag)
JsonWriteObject & add_object(const std::string &tag)
virtual void add_member(rapidjson::Document::ValueType &&name, rapidjson::Document::ValueType &&value, rapidjson::Document::AllocatorType &allocator)
virtual void finalize(JsonWriteComplex *cplx) override
JsonWriteArray & add_array(const std::string &tag)
#define UNUSED(expr)
Definition: defines.h:49
std::unordered_map< std::string, std::string > stringToDictionary(const std::string &json_string)
std::string dictionaryToString(const std::unordered_map< std::string, std::string > &key_values)
std::string name