Devel Lib
JsonObject.h
1 #pragma once
2 
3 #include "Core/Global.h"
4 #include "Core/Utils/StringUtils.h"
5 #include "Core/Typedef.h"
6 #include "Threading/LockGuard/LockGuard.h"
7 
8 #include <string_view>
9 #include <string>
10 #include <map>
11 
14 namespace Devel::IO {
17  enum class EJsonType {
18  JTObject,
19  JTNumber,
20  JTBoolean,
21  JTString,
22  JTArray,
23  JTNull,
24  };
25 
26  // Forward declaration of CJsonArray.
27  class CJsonArray;
28 
50  class CJsonObject : public std::string {
51  friend class CJsonDocument;
52 
53  public:
55  CJsonObject();
56 
59  CJsonObject(const CJsonObject &i_oOther) : CJsonObject() {
60  this->operator=(i_oOther);
61  }
62 
65  CJsonObject(CJsonObject &&i_oOther) noexcept: CJsonObject() {
66  this->operator=(std::move((i_oOther)));
67  }
68 
70  virtual ~CJsonObject();
71 
72  private:
75  EJsonType m_eType;
76  private:
79  Threading::CMutex m_oMutex;
82  CJsonArray *m_pArray;
83  private:
86  std::map<std::string, CJsonObject> m_aoData;
87  private:
91  void addObject(const std::string &i_stName, CJsonObject &&i_oObject);
92 
95  void setType(const EJsonType i_eType) { this->m_eType = i_eType; }
96 
98  void clear();
99 
100  public:
104  CJsonObject *find(std::string_view i_stName);
105 
108  std::vector<std::string> getKeys();
109 
112  CJsonArray toArray() const;
113 
114  public:
118  return this->m_oMutex;
119  }
120 
123  inline EJsonType type() const { return this->m_eType; }
124 
128  inline CJsonObject &get(const std::string_view i_stName) { return this->operator[](i_stName); }
129 
130  public:
132  inline void setNull() { this->m_eType = EJsonType::JTNull; }
133 
134  public:
137  inline uint64 toUInt64() const { return StringUtils::ToUInt64(*this); }
138 
141  inline int64 toInt64() const { return StringUtils::ToInt64(*this); }
142 
145  inline int toInt() const { return StringUtils::ToInt(*this); }
146 
149  inline uint toUInt() const { return StringUtils::ToInt(*this); }
150 
153  inline float toFloat() const { return StringUtils::ToFloat(*this); }
154 
157  inline double toDouble() const { return StringUtils::ToDouble(*this); }
158 
161  inline bool toBool() const { return (*this == "t" || *this == "1"); }
162 
165  inline bool isNull() const { return this->m_eType == EJsonType::JTNull; }
166 
167  public:
169  CJsonObject &operator=(const CJsonObject &i_oOther);
170 
171  CJsonObject &operator=(CJsonObject &&i_oOther) noexcept;
172 
173  CJsonObject &operator=(const CJsonArray &i_oOther);
174 
175  inline CJsonObject &operator=(const char *i_stValue) {
176  return this->operator=(std::string(i_stValue));
177  }
178 
179  inline CJsonObject &operator=(const std::string &i_stValue) {
180  this->m_eType = EJsonType::JTString;
181  std::string::operator=(i_stValue);
182  return *this;
183  }
184 
185  inline CJsonObject &operator=(const uint64 i_nValue) {
186  this->m_eType = EJsonType::JTNumber;
187  std::string::operator=(std::to_string(i_nValue));
188  return *this;
189  }
190 
191  inline CJsonObject &operator=(const uint i_nValue) {
192  this->m_eType = EJsonType::JTNumber;
193  std::string::operator=(std::to_string(i_nValue));
194  return *this;
195  }
196 
197  inline CJsonObject &operator=(const int64 i_nValue) {
198  this->m_eType = EJsonType::JTNumber;
199  std::string::operator=(std::to_string(i_nValue));
200  return *this;
201  }
202 
203  inline CJsonObject &operator=(const float i_fValue) {
204  this->m_eType = EJsonType::JTNumber;
205  std::string::operator=(std::to_string(i_fValue));
206  return *this;
207  }
208 
209  inline CJsonObject &operator=(const double i_dValue) {
210  this->m_eType = EJsonType::JTNumber;
211  std::string::operator=(std::to_string(i_dValue));
212  return *this;
213  }
214 
215  inline CJsonObject &operator=(const int i_nValue) {
216  this->m_eType = EJsonType::JTNumber;
217  std::string::operator=(std::to_string(i_nValue));
218  return *this;
219  }
220 
221  inline CJsonObject &operator=(const bool i_bState) {
222  this->operator=((i_bState ? "t" : "f"));
223  this->m_eType = EJsonType::JTBoolean;
224  return *this;
225  }
226 
230  inline CJsonObject &operator[](const std::string_view i_stKey) {
231  Threading::RecursiveLockGuard(this->m_oMutex);
232  return this->m_aoData.operator[](std::string(i_stKey));
233  }
234  };
235 }
This file contains a series of type definitions for the codebase.
unsigned int uint
An unsigned integer type.
Definition: Typedef.h:19
unsigned long long uint64
A 64-bit unsigned integer type.
Definition: Typedef.h:11
long long int64
A 64-bit integer type.
Definition: Typedef.h:7
A thread-safe array class for Json objects.
Definition: JsonArray.h:41
A class for representing and handling a JSON document.
Definition: JsonDocument.h:35
A class that encapsulates a JSON object.
Definition: JsonObject.h:50
void setNull()
Sets the type of the JSON object to null.
Definition: JsonObject.h:132
bool isNull() const
Checks if the JSON object is null.
Definition: JsonObject.h:165
CJsonObject(const CJsonObject &i_oOther)
Copy constructor.
Definition: JsonObject.h:59
bool toBool() const
Converts the JSON object to a boolean.
Definition: JsonObject.h:161
CJsonObject & operator[](const std::string_view i_stKey)
Subscript operator overload for string key.
Definition: JsonObject.h:230
EJsonType type() const
Returns the type of the JSON object.
Definition: JsonObject.h:123
double toDouble() const
Converts the JSON object to a double.
Definition: JsonObject.h:157
int64 toInt64() const
Converts the JSON object to a 64-bit signed integer.
Definition: JsonObject.h:141
float toFloat() const
Converts the JSON object to a float.
Definition: JsonObject.h:153
int toInt() const
Converts the JSON object to an integer.
Definition: JsonObject.h:145
CJsonObject()
Default constructor.
Definition: JsonObject.cpp:4
uint toUInt() const
Converts the JSON object to an unsigned integer.
Definition: JsonObject.h:149
uint64 toUInt64() const
Converts the JSON object to a 64-bit unsigned integer.
Definition: JsonObject.h:137
CJsonObject & operator=(const CJsonObject &i_oOther)
Assignment operator overloads for various data types including CJsonObject, CJsonArray,...
Definition: JsonObject.cpp:52
CJsonObject(CJsonObject &&i_oOther) noexcept
Move constructor.
Definition: JsonObject.h:65
virtual ~CJsonObject()
Virtual destructor.
Definition: JsonObject.cpp:7
CJsonArray toArray() const
Converts the JSON object to a JSON array.
Definition: JsonObject.cpp:44
CJsonObject * find(std::string_view i_stName)
Finds a value in the JSON object.
Definition: JsonObject.cpp:17
std::vector< std::string > getKeys()
Returns the keys of the JSON object.
Definition: JsonObject.cpp:32
CJsonObject & get(const std::string_view i_stName)
Gets the value associated with the specified name.
Definition: JsonObject.h:128
Threading::CMutex & mutex()
Returns a reference to the mutex.
Definition: JsonObject.h:117
A class for handling recursive mutexes.
Definition: Mutex.h:47
The namespace encapsulating I/O related classes and functions in the Devel framework.
EJsonType
Enumerates the possible types of JSON value.
Definition: JsonObject.h:17
@ JTString
A string type.
@ JTNull
A null type.
@ JTArray
An array type (an ordered collection of values).
@ JTObject
An object type (an unordered set of name/value pairs).
@ JTBoolean
A boolean type.
@ JTNumber
A number type.
float ToFloat(const std::string &i_stBuffer)
Converts a string to a float.
Definition: StringUtils.h:256
long long ToInt64(const std::string &i_sBuffer)
Converts a string to a 64-bit integer.
Definition: StringUtils.h:222
int ToInt(const std::string &i_sBuffer)
Converts a string to an integer.
Definition: StringUtils.h:239
double ToDouble(const std::string &i_stBuffer)
Converts a string to a double.
Definition: StringUtils.h:265
unsigned long long ToUInt64(const std::string &i_sBuffer)
Converts a string to an unsigned 64-bit integer.
Definition: StringUtils.h:231