Devel Lib
JsonDocument.h
1 #pragma once
2 
3 #include "Core/Global.h"
4 #include "Core/Exceptions.h"
5 #include "IO/JsonObject/JsonObject.h"
6 #include "IO/Buffer/Buffer.h"
7 
10 namespace Devel::IO {
11  static auto InvalidFormatException = std::runtime_error("Object format is invalid!");
12  static auto InvalidEncodingException = std::runtime_error("Object encoding is invalid!");
13 
35  class CJsonDocument {
36  public:
39  CJsonDocument();
40 
43  CJsonDocument(const std::string &i_stJson);
44 
48  CJsonDocument(const CJsonDocument &i_oOther) : CJsonDocument() { this->operator=(i_oOther); }
49 
53  CJsonDocument(CJsonDocument &&i_oOther) noexcept: CJsonDocument() { this->operator=(std::move(i_oOther)); }
54 
56  virtual ~CJsonDocument();
57 
58  private:
61  CJsonObject m_oObject;
62  private:
65  char *m_pBuffer;
68  char *m_pPointer;
71  char *m_pEnd;
72  private:
75  void removeWhitespace();
76 
81  void selectParser(std::string &i_stOutBuffer, CJsonObject *i_pObject = nullptr);
82 
83  private:
87  uint parseUnicodeHex();
88 
89  private:
94  void parseNull(std::string &i_stOutBuffer, CJsonObject *i_pObject);
95 
100  void parseBool(std::string &i_stOutBuffer, CJsonObject *i_pObject);
101 
106  void parseObject(std::string &i_stOutBuffer, CJsonObject *i_pObject);
107 
112  void parseString(std::string &i_stOutBuffer, CJsonObject *i_pObject = nullptr);
113 
118  void parseNumber(std::string &i_stOutBuffer, CJsonObject *i_pObject);
119 
123  void parseArray(CJsonObject *i_pObject);
124 
125  private:
131  static void selectSerializer(std::string &i_stBuffer, CJsonObject &i_oObject, bool i_bFormatted = false);
132 
138  static void serializeArray(std::string &i_stBuffer, CJsonObject &i_oObject, bool i_bFormatted = false);
139 
145  static void serializeObject(std::string &i_stBuffer, CJsonObject &i_oObject, bool i_bFormatted = false);
146 
151  static void serializeNumber(std::string &i_stBuffer, CJsonObject &i_oObject);
152 
157  static void serializeString(std::string &i_stBuffer, CJsonObject &i_oObject);
158 
162  static void serializeNull(std::string &i_stBuffer);
163 
168  static void serializeBool(std::string &i_stBuffer, CJsonObject &i_oObject);
169 
170  private:
175  inline void setBuffer(char *i_pBuffer, const size_t i_nSize) {
176  this->clearBuffer();
177  this->m_pBuffer = i_pBuffer;
178  this->m_pPointer = i_pBuffer;
179  this->m_pEnd = i_pBuffer + i_nSize;
180  }
181 
184  inline void clearBuffer() {
185  delete[] m_pBuffer;
186  this->m_pBuffer = nullptr;
187  this->m_pPointer = nullptr;
188  this->m_pEnd = nullptr;
189  }
190 
193  inline void skipChar() {
194  this->m_pPointer++;
195  }
196 
197  private:
201  inline size_t getBufferSize() const { return this->m_pEnd - this->m_pBuffer; }
202 
207  inline bool checkChar(const char i_chExpect) {
208  if (*this->m_pPointer == i_chExpect) {
209  this->m_pPointer++;
210  return true;
211  }
212  return false;
213  }
214 
215  public:
219  void parse(const std::string &i_stJson);
220 
225  std::string serialize(bool i_bFormatted);
226 
227  public:
230  void clear() {
231  this->m_oObject.clear();
232  }
233 
234  public:
239  inline CJsonObject &getObject(const std::string &i_stKey) { return this->operator[](i_stKey); }
240 
244  inline CJsonObject &toObject() { return this->m_oObject; }
245 
246  public:
251  inline CJsonObject &operator[](const std::string &i_stKey) {
252  return this->m_oObject.operator[](i_stKey);
253  }
254 
259  inline CJsonDocument &operator=(const CJsonDocument &i_oOther) {
260  this->m_oObject = i_oOther.m_oObject;
261  return *this;
262  }
263 
268  inline CJsonDocument &operator=(CJsonDocument &&i_oOther) noexcept {
269  this->m_oObject = std::move(i_oOther.m_oObject);
270  return *this;
271  }
272  };
273 }
unsigned int uint
An unsigned integer type.
Definition: Typedef.h:19
A class for representing and handling a JSON document.
Definition: JsonDocument.h:35
void clear()
Clears the encapsulated JSON object.
Definition: JsonDocument.h:230
CJsonObject & getObject(const std::string &i_stKey)
Gets a JSON object by key from the encapsulated JSON object.
Definition: JsonDocument.h:239
virtual ~CJsonDocument()
Destructor for CJsonDocument.
Definition: JsonDocument.cpp:15
CJsonDocument()
Default constructor for CJsonDocument.
Definition: JsonDocument.cpp:4
CJsonDocument & operator=(CJsonDocument &&i_oOther) noexcept
Moves the ownership of the CJsonObject from the given CJsonDocument to this CJsonDocument.
Definition: JsonDocument.h:268
CJsonObject & toObject()
Gets the encapsulated JSON object.
Definition: JsonDocument.h:244
CJsonDocument(const CJsonDocument &i_oOther)
Copy constructor for CJsonDocument.
Definition: JsonDocument.h:48
std::string serialize(bool i_bFormatted)
Serializes the encapsulated JSON object into a JSON string.
Definition: JsonDocument.cpp:406
CJsonDocument(CJsonDocument &&i_oOther) noexcept
Move constructor for CJsonDocument.
Definition: JsonDocument.h:53
void parse(const std::string &i_stJson)
Parses a JSON string.
Definition: JsonDocument.cpp:378
CJsonDocument & operator=(const CJsonDocument &i_oOther)
Overload of move assignment operator for CJsonDocument.
Definition: JsonDocument.h:259
CJsonObject & operator[](const std::string &i_stKey)
Overload of assignment operator for CJsonDocument.
Definition: JsonDocument.h:251
A class that encapsulates a JSON object.
Definition: JsonObject.h:50
The namespace encapsulating I/O related classes and functions in the Devel framework.