Skip to content

Commit 165134d

Browse files
committed
1.4: literals and updated doc
1 parent 857cc5c commit 165134d

File tree

4 files changed

+222
-8
lines changed

4 files changed

+222
-8
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This library allows you to parse JSON strings
1212
In this step we will see how to parse a simple JSON string
1313

1414
```c++
15-
#include "json.hh"
15+
#include "jpp.hh"
1616
#include <iostream>
1717

1818
int main(int argc, char **argv)
@@ -31,7 +31,7 @@ int main(int argc, char **argv)
3131
Now we will see how to access to our json object
3232
3333
```c++
34-
#include "json.hh"
34+
#include "jpp.hh"
3535
#include <iostream>
3636
3737
int main(int argc, char **argv)
@@ -60,7 +60,7 @@ int main(int argc, char **argv)
6060
In this step we will see how to create a JSON object from zero
6161

6262
```c++
63-
#include "json.hh"
63+
#include "jpp.hh"
6464
#include <iostream>
6565

6666
int main(int argc, char **argv)
@@ -72,6 +72,27 @@ int main(int argc, char **argv)
7272
car["model"] = "Model1";
7373

7474
car_collection["favoriteCar"] = car;
75+
std::cout << car_collection.to_string() << std::endl;
7576
return 0;
7677
}
78+
```
79+
80+
## Step 4
81+
82+
In this final test we will see how to create a JSON object from literals
83+
84+
```c++
85+
#include "jpp.hh"
86+
#include <iostream>
87+
88+
int main(int argc, char **argv)
89+
{
90+
Jpp::Json my_object = l_object{
91+
{"title", "Der Process"},
92+
{"year", 1925},
93+
{"author": "Franz Kafka"}
94+
};
95+
96+
Jpp::Json my_array = l_array{0, 1, 2, 10, 123.43, 2322.111};
97+
}
7798
```

include/jpp.hh

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json.hh
33
* @author Simone Ancona
44
* @brief A JSON parser for C++
5-
* @version 1.3.2
5+
* @version 1.4
66
* @date 2023-07-23
77
*
88
* @copyright Copyright (c) 2023
@@ -18,6 +18,9 @@
1818
#include <cctype>
1919
#include <vector>
2020

21+
#define l_object std::vector<std::pair<std::string, std::any>>
22+
#define l_array std::vector<std::any>
23+
2124
namespace Jpp
2225
{
2326
enum JsonType
@@ -130,47 +133,170 @@ namespace Jpp
130133
Json get_unresolved_object(std::string_view, size_t &, bool);
131134

132135
public:
136+
/**
137+
* @brief Construct a new Json object
138+
* @since v1.0
139+
*/
133140
inline Json() noexcept
134141
{
135142
this->type = JSON_OBJECT;
136143
this->is_resolved = true;
137144
}
138145

146+
/**
147+
* @brief Construct a new Json object
148+
*
149+
* @param children
150+
* @param type
151+
* @since v1.0
152+
*/
139153
inline Json(std::map<std::string, Json> children, JsonType type) noexcept
140154
{
141155
this->children = children;
142156
this->type = type;
143157
this->is_resolved = true;
144158
}
145159

160+
/**
161+
* @brief Construct a new Json object
162+
*
163+
* @param value
164+
* @param type
165+
* @since v1.0
166+
*/
146167
inline Json(std::any value, JsonType type) noexcept
147168
{
148169
this->value = value;
149170
this->type = type;
150171
this->is_resolved = true;
151172
}
152173

174+
/**
175+
* @brief Construct a new Json object
176+
*
177+
* @param values
178+
* @since v1.4
179+
*/
180+
inline Json(std::vector<std::any> values)
181+
{
182+
this->type = JSON_ARRAY;
183+
this->is_resolved = true;
184+
for (size_t i = 0; i < values.size(); ++i)
185+
{
186+
this->children.emplace(std::to_string(i), Json(values[i]));
187+
}
188+
}
189+
190+
/**
191+
* @brief Construct a new Json object
192+
*
193+
* @param key_values
194+
* @since v1.4
195+
*/
196+
inline Json(std::vector<std::pair<std::string, std::any>> key_values)
197+
{
198+
this->type = JSON_OBJECT;
199+
this->is_resolved = true;
200+
for (size_t i = 0; i < key_values.size(); ++i)
201+
{
202+
this->children.emplace(key_values[i].first, Json(key_values[i].second));
203+
}
204+
}
205+
206+
/**
207+
* @brief Construct a new Json object
208+
*
209+
* @param value
210+
* @since v1.4
211+
*/
212+
inline Json(std::any value)
213+
{
214+
this->is_resolved = true;
215+
size_t hash = value.type().hash_code();
216+
if (hash == typeid(int).hash_code())
217+
{
218+
this->value = static_cast<double>(std::any_cast<int>(value));
219+
this->type = JSON_NUMBER;
220+
return;
221+
}
222+
if (hash == typeid(const char *).hash_code())
223+
{
224+
this->value = std::string(std::any_cast<const char *>(value));
225+
this->type = JSON_STRING;
226+
return;
227+
}
228+
if (hash == typeid(std::string).hash_code())
229+
{
230+
this->value = value;
231+
this->type = JSON_STRING;
232+
return;
233+
}
234+
if (hash == typeid(bool).hash_code())
235+
{
236+
this->value = value;
237+
this->type = JSON_BOOLEAN;
238+
return;
239+
}
240+
if (hash == typeid(double).hash_code())
241+
{
242+
this->value = value;
243+
this->type = JSON_NUMBER;
244+
return;
245+
}
246+
if (hash == typeid(nullptr_t).hash_code())
247+
{
248+
this->value = value;
249+
this->type = JSON_NULL;
250+
return;
251+
}
252+
throw std::runtime_error("Unknown type: " + std::string(value.type().name()));
253+
}
254+
255+
/**
256+
* @brief Construct a new Json object
257+
*
258+
* @param str
259+
* @since v1.0
260+
*/
153261
inline Json(std::string str) noexcept
154262
{
155263
this->value = str;
156264
this->type = JSON_STRING;
157265
this->is_resolved = true;
158266
}
159267

268+
/**
269+
* @brief Construct a new Json object
270+
*
271+
* @param num
272+
* @since v1.0
273+
*/
160274
inline Json(double num) noexcept
161275
{
162276
this->value = num;
163277
this->type = JSON_NUMBER;
164278
this->is_resolved = true;
165279
}
166280

281+
/**
282+
* @brief Construct a new Json object
283+
*
284+
* @param val
285+
* @since v1.0
286+
*/
167287
inline Json(bool val) noexcept
168288
{
169289
this->value = val;
170290
this->type = JSON_BOOLEAN;
171291
this->is_resolved = true;
172292
}
173293

294+
/**
295+
* @brief Construct a new Json object
296+
*
297+
* @param null
298+
* @since v1.0
299+
*/
174300
inline Json(nullptr_t null) noexcept
175301
{
176302
this->value = null;
@@ -266,7 +392,7 @@ namespace Jpp
266392
* @brief Parse a JSON string
267393
* @since v1.0
268394
*/
269-
void parse(const std::string&);
395+
void parse(const std::string &);
270396

271397
/**
272398
* @brief Get the children object
@@ -330,6 +456,18 @@ namespace Jpp
330456
*/
331457
Json &operator=(const char[]);
332458

459+
/**
460+
* @return Json&
461+
* @since v1.4
462+
*/
463+
Json &operator=(std::vector<std::any>);
464+
465+
/**
466+
* @return Json&
467+
* @since v1.4
468+
*/
469+
Json &operator=(std::vector<std::pair<std::string, std::any>>);
470+
333471
/**
334472
* @brief Convert the JSON object to its JSON representation.
335473
*

src/jpp.cc

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json.cc
33
* @author Simone Ancona
44
* @brief
5-
* @version 1.3.2
5+
* @version 1.4
66
* @date 2023-07-23
77
*
88
* @copyright Copyright (c) 2023
@@ -33,6 +33,8 @@ Jpp::Json &Jpp::Json::operator[](const std::string &property)
3333

3434
Jpp::Json &Jpp::Json::operator=(const std::string &str)
3535
{
36+
this->children.clear();
37+
this->is_resolved = true;
3638
this->type = Jpp::JSON_STRING;
3739
this->value = str;
3840

@@ -41,6 +43,8 @@ Jpp::Json &Jpp::Json::operator=(const std::string &str)
4143

4244
Jpp::Json &Jpp::Json::operator=(const char str[])
4345
{
46+
this->children.clear();
47+
this->is_resolved = true;
4448
this->type = Jpp::JSON_STRING;
4549
this->value = std::string(str);
4650

@@ -49,6 +53,8 @@ Jpp::Json &Jpp::Json::operator=(const char str[])
4953

5054
Jpp::Json &Jpp::Json::operator=(bool val)
5155
{
56+
this->children.clear();
57+
this->is_resolved = true;
5258
this->type = Jpp::JSON_BOOLEAN;
5359
this->value = val;
5460

@@ -57,6 +63,8 @@ Jpp::Json &Jpp::Json::operator=(bool val)
5763

5864
Jpp::Json &Jpp::Json::operator=(double num)
5965
{
66+
this->children.clear();
67+
this->is_resolved = true;
6068
this->type = Jpp::JSON_NUMBER;
6169
this->value = num;
6270

@@ -65,25 +73,53 @@ Jpp::Json &Jpp::Json::operator=(double num)
6573

6674
Jpp::Json &Jpp::Json::operator=(int num)
6775
{
76+
this->children.clear();
77+
this->is_resolved = true;
6878
this->type = Jpp::JSON_NUMBER;
6979
this->value = static_cast<double>(num);
7080

7181
return *this;
7282
}
7383

84+
Jpp::Json &Jpp::Json::operator=(std::vector<std::any> array)
85+
{
86+
this->children.clear();
87+
this->type = Jpp::JSON_ARRAY;
88+
this->is_resolved = true;
89+
for (size_t i = 0; i < array.size(); ++i)
90+
{
91+
this->children.emplace(std::to_string(i), Json(array[i]));
92+
}
93+
return *this;
94+
}
95+
96+
Jpp::Json &Jpp::Json::operator=(std::vector<std::pair<std::string, std::any>> object)
97+
{
98+
this->children.clear();
99+
this->type = Jpp::JSON_OBJECT;
100+
this->is_resolved = true;
101+
for (size_t i = 0; i < object.size(); ++i)
102+
{
103+
this->children.emplace(object[i].first, Json(object[i].second));
104+
}
105+
return *this;
106+
}
107+
74108
void Jpp::Json::parse(const std::string &json_string)
75109
{
76110
size_t start = 0;
77111
this->is_resolved = true;
78112
if (json_string[start] == '{')
79113
{
80114
this->children = parse_object(json_string, start);
115+
this->unresolved_string = "";
81116
this->type = Jpp::JSON_OBJECT;
82117
return;
83118
}
84119
if (json_string[start] == '[')
85120
{
86121
this->children = parse_array(json_string, start);
122+
this->unresolved_string = "";
87123
this->type = Jpp::JSON_ARRAY;
88124
return;
89125
}
@@ -108,7 +144,7 @@ Jpp::Json Jpp::Json::get_unresolved_object(std::string_view str, size_t &index,
108144
index++;
109145
if (index >= str.length())
110146
throw std::runtime_error("Unexpected end of the string");
111-
147+
112148
switch (str[index])
113149
{
114150
case '"':
@@ -157,7 +193,7 @@ Jpp::Json Jpp::Json::get_unresolved_object(std::string_view str, size_t &index,
157193
case ']':
158194
if (str[index] != end)
159195
break;
160-
if (!is_string )
196+
if (!is_string)
161197
{
162198
if (level == 0)
163199
cycle = false;

0 commit comments

Comments
 (0)