HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
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 
139  {
140  friend class JsonWriteData;
141  friend class JsonWriteDocument;
142 
143  protected:
144 
145  virtual void add_member(rapidjson::Document::ValueType&& name,
146  rapidjson::Document::ValueType&& value,
147  rapidjson::Document::AllocatorType& allocator);
148  virtual void finalize(JsonWriteComplex* cplx) override;
149 
150  public:
157  JsonWriteObject(const std::string& tag, JsonWriteComplex* parent);
158  virtual ~JsonWriteObject() {;}
159 
166  JsonWriteData& operator[](const std::string& tag);
167 
174  JsonWriteObject& add_object(const std::string& tag);
175 
182  JsonWriteArray& add_array(const std::string& tag);
183  };
184 
189  {
190  friend class JsonWriteData;
191  friend class JsonWriteDocument;
192 
193  protected:
194  virtual void finalize(JsonWriteComplex* cplx) override;
195 
196  public:
203  JsonWriteArray(const std::string& tag, JsonWriteComplex* parent);
204  virtual ~JsonWriteArray() {;}
205 
212  JsonWriteArray& operator<<(const std::string& txt);
213 
220  JsonWriteArray& operator<<(int ivalue);
221 
228 
235  };
236 
242  {
243  friend class JsonWriteData;
244 
245  protected:
246  rapidjson::Document mRapidDocument;
247 
248  virtual rapidjson::Document::AllocatorType& allocator() override;
249  virtual void finalize(JsonWriteComplex* cplx) override;
250  virtual void add_member(rapidjson::Document::ValueType&& name,
251  rapidjson::Document::ValueType&& value,
252  rapidjson::Document::AllocatorType& allocator) override;
253  void dump(rapidjson::Value& parent);
254 
255  public:
257  virtual ~JsonWriteDocument() {;}
258 
265  bool serialize(const std::string& filename);
266 
270  void dump();
271  };
272 
276  namespace JsonConverter {
282  std::unordered_map<std::string,std::string> stringToDictionary(const std::string& json_string);
283 
289  std::string dictionaryToString(const std::unordered_map<std::string,std::string>& key_values);
290  }
291 }
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)
Definition: defines.h:45
std::string name