HAL
json_write_document.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #define RAPIDJSON_HAS_STDSTRING 1
29 
30 #include "hal_core/defines.h"
31 #include "rapidjson/document.h"
32 
33 #include <string>
34 #include <vector>
35 #include <unordered_map>
36 
37 namespace hal {
38  class JsonWriteComplex;
39  class JsonWriteObject;
40  class JsonWriteArray;
41  class JsonWriteDocument;
42 
48  {
49  friend class JsonWriteComplex;
50 
51  protected:
52  std::string mTagname;
54 
55  virtual rapidjson::Document::AllocatorType& allocator();
56 
57  public:
64  JsonWriteData(const std::string& tag, JsonWriteComplex* parent);
65  virtual ~JsonWriteData() {;}
66 
73  JsonWriteData& operator=(const std::string& txt);
74 
81  JsonWriteData& operator=(int ivalue);
82 
89  JsonWriteData& operator=(double value);
90 
97  JsonWriteData& operator=(uint64_t u64Value);
98  };
99 
107  {
108  friend class JsonWriteData;
109  friend class JsonWriteObject;
110  friend class JsonWriteArray;
111  friend class JsonWriteDocument;
112 
113  protected:
114  std::vector<JsonWriteData*> mChildData;
115  rapidjson::Value mRapidValue;
116 
117  virtual void finalize(JsonWriteComplex* cplx) = 0;
118 
119  public:
126  JsonWriteComplex(const std::string& tag, JsonWriteComplex* parent);
127  virtual ~JsonWriteComplex();
128 
132  void close();
133  };
134 
136  {
137  friend class JsonWriteData;
138  friend class JsonWriteDocument;
139 
140  protected:
141 
142  virtual void add_member(rapidjson::Document::ValueType&& name,
143  rapidjson::Document::ValueType&& value,
144  rapidjson::Document::AllocatorType& allocator);
145  virtual void finalize(JsonWriteComplex* cplx) override;
146 
147  public:
154  JsonWriteObject(const std::string& tag, JsonWriteComplex* parent);
155  virtual ~JsonWriteObject() {;}
156 
163  JsonWriteData& operator[](const std::string& tag);
164 
171  JsonWriteObject& add_object(const std::string& tag);
172 
179  JsonWriteArray& add_array(const std::string& tag);
180  };
181 
183  {
184  friend class JsonWriteData;
185  friend class JsonWriteDocument;
186 
187  protected:
188  virtual void finalize(JsonWriteComplex* cplx) override;
189 
190  public:
197  JsonWriteArray(const std::string& tag, JsonWriteComplex* parent);
198  virtual ~JsonWriteArray() {;}
199 
206  JsonWriteArray& operator<<(const std::string& txt);
207 
214  JsonWriteArray& operator<<(int ivalue);
215 
222 
229  };
230 
232  {
233  friend class JsonWriteData;
234 
235  protected:
236  rapidjson::Document mRapidDocument;
237 
238  virtual rapidjson::Document::AllocatorType& allocator() override;
239  virtual void finalize(JsonWriteComplex* cplx) override;
240  virtual void add_member(rapidjson::Document::ValueType&& name,
241  rapidjson::Document::ValueType&& value,
242  rapidjson::Document::AllocatorType& allocator) override;
243  void dump(rapidjson::Value& parent);
244 
245  public:
247  virtual ~JsonWriteDocument() {;}
248 
255  bool serialize(const std::string& filename);
256 
260  void dump();
261  };
262 
263  namespace JsonConverter {
269  std::unordered_map<std::string,std::string> stringToDictionary(const std::string& json_string);
270 
276  std::string dictionaryToString(const std::unordered_map<std::string,std::string>& key_values);
277  }
278 }
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)
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