22 * @file json.hh
33 * @author Simone Ancona
44 * @brief A JSON parser for C++
5- * @version 1.2.2
6- * @date 2023-07-20
7- *
5+ * @version 1.3
6+ * @date 2023-07-22
7+ *
88 * @copyright Copyright (c) 2023
9- *
9+ *
1010 */
1111
1212#pragma once
1818#include < cctype>
1919#include < vector>
2020
21- #define JSON_ARRAY (char )0x00
22- #define JSON_OBJECT (char )0x01
23- #define JSON_STRING (char )0xF0
24- #define JSON_BOOLEAN (char )0xF1
25- #define JSON_NUMBER (char )0xF2
26- #define JSON_NULL (char )0xF3
27-
2821namespace Jpp
2922{
30- typedef char json_type_t ;
23+ enum JsonType
24+ {
25+ JSON_ARRAY,
26+ JSON_OBJECT,
27+ JSON_STRING,
28+ JSON_BOOLEAN,
29+ JSON_NUMBER,
30+ JSON_NULL
31+ };
3132
3233 enum Token
3334 {
@@ -44,85 +45,156 @@ namespace Jpp
4445
4546 /* *
4647 * @brief The Json class allows to parse a json string
47- *
48+ *
4849 */
4950 class Json
5051 {
5152 private:
52- json_type_t type;
53+ JsonType type;
5354 std::map<std::string, Json> children;
5455 std::any value;
5556
57+ void trim_string (std::string &);
58+ std::map<std::string, Json> parse_object (std::string, size_t &);
59+ std::map<std::string, Json> parse_array (std::string, size_t &);
60+ std::string parse_string (std::string, size_t &, char );
61+ std::any parse_number (std::string, size_t &);
62+ std::any parse_boolean (std::string, size_t &);
63+ std::any parse_null (std::string, size_t &);
64+
65+ Token match_next (std::string, size_t &);
66+ void next_white_space_or_separator (std::string, size_t &);
67+ void skip_white_spaces (std::string, size_t &);
68+
69+ std::string json_object_to_string (Json);
70+ std::string json_array_to_string (Json);
71+ std::string str_replace (std::string, char , std::string);
72+
5673 public:
57- Json ();
58- Json (std::map<std::string, Json>, json_type_t );
59- Json (std::any, json_type_t );
60- Json (std::string);
61- Json (double );
62- Json (bool );
63- Json (nullptr_t );
74+ inline Json () noexcept
75+ {
76+ this ->type = JSON_OBJECT;
77+ }
78+
79+ inline Json (std::map<std::string, Json> children, JsonType type) noexcept
80+ {
81+ this ->children = children;
82+ this ->type = type;
83+ }
84+
85+ inline Json (std::any value, JsonType type) noexcept
86+ {
87+ this ->value = value;
88+ this ->type = type;
89+ }
90+
91+ inline Json (std::string str) noexcept
92+ {
93+ this ->value = str;
94+ this ->type = JSON_STRING;
95+ }
96+
97+ inline Json (double num) noexcept
98+ {
99+ this ->value = num;
100+ this ->type = JSON_NUMBER;
101+ }
102+
103+ Json (bool val)
104+ {
105+ this ->value = val;
106+ this ->type = JSON_BOOLEAN;
107+ }
108+
109+ Json (nullptr_t null)
110+ {
111+ this ->value = null;
112+ this ->type = JSON_NULL;
113+ }
114+
64115 ~Json () = default ;
65116
66117 /* *
67118 * @brief Get the type object
68- *
69- * @return json_type_t
119+ *
120+ * @return json_type_t
70121 * @since v1.0
71122 */
72- json_type_t get_type ();
123+ inline JsonType get_type ()
124+ {
125+ return this ->type ;
126+ }
73127
74128 /* *
75129 * @brief Get the value object
76- *
77- * @return std::any
130+ *
131+ * @return std::any
78132 * @since v1.0
79133 */
80- std::any get_value ();
134+ inline std::any get_value ()
135+ {
136+ return this ->value ;
137+ }
81138
82139 /* *
83140 * @brief Check if the JSON value is an array
84- *
85- * @return true
86- * @return false
141+ *
142+ * @return true
143+ * @return false
87144 * @since v1.0
88145 */
89- bool is_array ();
146+ inline bool is_array ()
147+ {
148+ return this ->type == JSON_ARRAY;
149+ }
90150
91151 /* *
92152 * @brief Check if the JSON value is an object
93- *
94- * @return true
95- * @return false
153+ *
154+ * @return true
155+ * @return false
96156 * @since v1.0
97157 */
98- bool is_object ();
158+ inline bool is_object ()
159+ {
160+ return this ->type == JSON_OBJECT;
161+ }
99162
100163 /* *
101164 * @brief Check if the JSON is an atomic string value
102- *
103- * @return true
104- * @return false
165+ *
166+ * @return true
167+ * @return false
105168 * @since v1.0
106169 */
107- bool is_string ();
170+ inline bool is_string ()
171+ {
172+ return this ->type == JSON_STRING;
173+ }
108174
109175 /* *
110176 * @brief Check if the JSON is an atomic boolean value
111- *
112- * @return true
113- * @return false
177+ *
178+ * @return true
179+ * @return false
114180 * @since v1.0
115181 */
116- bool is_boolean ();
182+ inline bool is_boolean ()
183+ {
184+ return this ->type == JSON_BOOLEAN;
185+ }
117186
118187 /* *
119188 * @brief Check if the JSON is an atomic double value
120- *
121- * @return true
122- * @return false
189+ *
190+ * @return true
191+ * @return false
123192 * @since v1.0
124193 */
125- bool is_number ();
194+ inline bool is_number ()
195+ {
196+ return this ->type == JSON_NUMBER;
197+ }
126198
127199 /* *
128200 * @brief Parse a JSON string
@@ -132,72 +204,74 @@ namespace Jpp
132204
133205 /* *
134206 * @brief Get the children object
135- *
136- * @return std::map<std::string, Json>
207+ *
208+ * @return std::map<std::string, Json>
137209 * @since v1.0
138210 */
139- std::map<std::string, Json> get_children ();
211+ inline std::map<std::string, Json> get_children ()
212+ {
213+ return this ->children ;
214+ }
140215
141216 /* *
142217 * @brief Access to a position of the array
143- * @example
218+ * @example
144219 * Jpp::Json json;
145220 * json.parse("[0, 1, 2, 3]");
146221 * json[0] // returns an any value
147- * @return Json&
222+ * @return Json&
148223 * @since v1.0
149224 */
150225 Json &operator [](size_t );
151226
152227 /* *
153228 * @brief Access to a value of the object with the given property name
154- *
155- * @return Json&
229+ *
230+ * @return Json&
156231 * @since v1.0
157232 */
158233 Json &operator [](std::string);
159234
160235 /* *
161- * @return Json&
236+ * @return Json&
162237 * @since v1.0
163238 */
164239 Json &operator =(std::string);
165240
166241 /* *
167- * @return Json&
242+ * @return Json&
168243 * @since v1.0
169244 */
170245 Json &operator =(double );
171246
172247 /* *
173- * @return Json&
248+ * @return Json&
174249 * @since v1.0
175250 */
176251 Json &operator =(int );
177252
178253 /* *
179- * @return Json&
254+ * @return Json&
180255 * @since v1.0
181256 */
182257 Json &operator =(bool );
183258
184259 /* *
185- * @return Json&
260+ * @return Json&
186261 * @since v1.0
187262 */
188263 Json &operator =(const char []);
189264
190-
191265 /* *
192266 * @brief Convert the JSON object to its JSON representation.
193- *
194- * @return std::string
267+ *
268+ * @return std::string
195269 */
196270 std::string to_string ();
197271
198272 /* *
199273 * @brief Begin iterator
200- *
274+ *
201275 * @return std::map<std::string, Json>::iterator
202276 * @since v1.1
203277 */
@@ -208,7 +282,7 @@ namespace Jpp
208282
209283 /* *
210284 * @brief End iterator
211- *
285+ *
212286 * @return std::map<std::string, Json>::iterator
213287 * @since v1.1
214288 */
@@ -219,7 +293,7 @@ namespace Jpp
219293
220294 /* *
221295 * @brief Reverse begin iterator
222- *
296+ *
223297 * @return std::map<std::string, Json>::iterator
224298 * @since v1.1
225299 */
@@ -230,7 +304,7 @@ namespace Jpp
230304
231305 /* *
232306 * @brief Reverse end iterator
233- *
307+ *
234308 * @return std::map<std::string, Json>::iterator
235309 * @since v1.1
236310 */
@@ -241,26 +315,20 @@ namespace Jpp
241315
242316 /* *
243317 * @brief Get the vector if the JSON object is an array
244- *
245- * @return std::vector<Json>
318+ *
319+ * @return std::vector<Json>
246320 * @since v1.2
247321 */
248- std::vector<Json> get_vector ();
322+ inline std::vector<Json> get_vector ()
323+ {
324+ if (type != JSON_ARRAY)
325+ throw std::runtime_error (" Cannot convert a non-array JSON to a vector" );
326+ std::vector<Jpp::Json> vct;
327+ for (auto json : children)
328+ {
329+ vct.push_back (json.second );
330+ }
331+ return vct;
332+ }
249333 };
250-
251- void trim_string (std::string &);
252- std::map<std::string, Json> parse_object (std::string, size_t &);
253- std::map<std::string, Json> parse_array (std::string, size_t &);
254- std::string parse_string (std::string, size_t &, char );
255- std::any parse_number (std::string, size_t &);
256- std::any parse_boolean (std::string, size_t &);
257- std::any parse_null (std::string, size_t &);
258-
259- Token match_next (std::string, size_t &);
260- void next_white_space_or_separator (std::string, size_t &);
261- void skip_white_spaces (std::string, size_t &);
262-
263- std::string json_object_to_string (Json);
264- std::string json_array_to_string (Json);
265- std::string str_replace (std::string, char , std::string);
266334};
0 commit comments