-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (124 loc) · 7.6 KB
/
main.cpp
File metadata and controls
139 lines (124 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "jujson.hpp"
#include <string>
#include <iostream>
// >:3 Custom trait
struct custom_trait {
public:
typedef char char_type;
public:
[[nodiscard]] constexpr static char_type get_string_litteral_begin() noexcept {return '<';}
[[nodiscard]] constexpr static char_type get_string_litteral_end() noexcept {return '>';}
[[nodiscard]] static bool is_string_litteral_begin(char_type c) noexcept {return c == get_string_litteral_begin();}
[[nodiscard]] static bool is_string_litteral_end(char_type c) noexcept {return c == get_string_litteral_end();}
[[nodiscard]] constexpr static char_type get_array_begin() noexcept {return '[';}
[[nodiscard]] constexpr static char_type get_array_end() noexcept {return ']';}
[[nodiscard]] static bool is_array_begin(char_type c) noexcept {return c == get_array_begin();}
[[nodiscard]] static bool is_array_end(char_type c) noexcept {return c == get_array_end();}
[[nodiscard]] constexpr static char_type get_object_begin() noexcept {return '{';}
[[nodiscard]] constexpr static char_type get_object_end() noexcept {return '}';}
[[nodiscard]] static bool is_object_begin(char_type c) noexcept {return c == get_object_begin();}
[[nodiscard]] static bool is_object_end(char_type c) noexcept {return c == get_object_end();}
[[nodiscard]] constexpr static char_type get_comma() noexcept {return ',';}
[[nodiscard]] static bool is_comma(char_type c) noexcept {return c == get_comma();}
[[nodiscard]] constexpr static char_type get_colon() noexcept {return ':';}
[[nodiscard]] static bool is_colon(char_type c) noexcept {return c == get_colon();}
[[nodiscard]] static bool is_new_line(char_type c) noexcept {return c == '\n';}
[[nodiscard]] static bool is_escape_spec(char_type c) noexcept {return c == '\\';}
[[nodiscard]] static bool is_minus(char_type c) noexcept {return c == '-';}
[[nodiscard]] static bool is_exp_symbol(char_type c) noexcept {return (c == 'e') || (c == 'E');}
[[nodiscard]] static bool is_dec_separator_symbol(char_type c) noexcept {return (c == '.');}
template<class StringT_>
[[nodiscard]] static jujson::json_value_kind value_kind_from_string(const StringT_& str) {
if (str == "null") { return jujson::JUSJON_JSON_VALUE_NULL;
} else if (str == "true") { return jujson::JUSJON_JSON_VALUE_TRUE;
} else if (str == "false") { return jujson::JUSJON_JSON_VALUE_FALSE;
} else { return jujson::JUSJON_JSON_VALUE_INVALID;
}
return jujson::JUSJON_JSON_VALUE_INVALID;}
};
bool default_test() {
std::cout << "basic test\n";
std::string data = R"({"name":"JohnDoe","age":30,"isEmployed":true,"address":{"street":"123MainSt","city":"Anytown","state":"CA","postalCode":"12345"},"phoneNumbers":[{"type":"home","number":"555-1234"},{"type":"work","number":"555-5678"}],"children":["Jane","Doe"],"spouse":null,"salary":50000.50,"projects":[{"name":"ProjectAlpha","status":"completed","team":["Alice","Bob","Charlie"]},{"name":"ProjectBeta","status":"inprogress","team":["David","Eve"]}]})";
jujson::json_parser<std::string> parser(data.begin(), data.end());
auto val = parser.parse_json_object_expected();
auto nameValue = val.find("name");
std::string name = ((nameValue != val.get_childs().end()) ? nameValue->value().data() : "ERROR");
std::cout << name << "\n\n";
return (val.to_string() == data) && (name == "JohnDoe");
}
bool wchar_test() {
std::cout << "wchar test\n";
std::wstring data = LR"({"name":"JohnDoe","age":30,"isEmployed":true,"address":{"street":"123MainSt","city":"Anytown","state":"CA","postalCode":"12345"},"phoneNumbers":[{"type":"home","number":"555-1234"},{"type":"work","number":"555-5678"}],"children":["Jane","Doe"],"spouse":null,"salary":50000.50,"projects":[{"name":"ProjectAlpha","status":"completed","team":["Alice","Bob","Charlie"]},{"name":"ProjectBeta","status":"inprogress","team":["David","Eve"]}]})";
jujson::json_parser<std::wstring> parser(data.begin(), data.end());
auto val = parser.parse_json_object_expected();
auto nameValue = val.find(L"name");
std::wstring name = ((nameValue != val.get_childs().end()) ? nameValue->value().data() : L"ERROR");
std::wcout << name << L"\n\n";
return (val.to_string() == data) && (name == L"JohnDoe");
}
bool accessing() {
std::cout << "accessing test\n";
std::string jsonStr = R"({"name": "John", "age": 30, "isStudent": false, "courses": ["Math", "Science"]})";
jujson::json_parser<std::string> parser(jsonStr);
jujson::json_value<std::string> jsonObj = parser.parse_json_object_expected();
if (jsonObj.is_valid()) {
auto nameKV = jsonObj.find("name");
if (nameKV != jsonObj.get_childs().end()) {
std::cout << "Name: " << nameKV->value().data() << '\n';
}
auto ageKV = jsonObj.find("age");
if (ageKV != jsonObj.get_childs().end()) {
std::cout << "Age: " << ageKV->value().data() << '\n';
}
auto isStudentKV = jsonObj.find("isStudent");
if (isStudentKV != jsonObj.get_childs().end()) {
std::cout << "Is Student: " << isStudentKV->value().data() << '\n';
}
auto coursesKV = jsonObj.find("courses");
if (coursesKV != jsonObj.get_childs().end()) {
for (const auto& courseKVs : coursesKV->value().get_childs()) {
std::cout << "Course: " << courseKVs.data() << '\n';
}
}
std::cout << '\n';
return true;
} else {
std::cerr << parser.line() << ':' << parser.column() << ':' << " error parsing JSON: " << parser.get_last_error() << '\n';
return false;
}
}
bool custom_type_test() {
std::cout << "custom type test\n";
std::string jsonStr = "{<name>: <John>, <age>: 30, <isStudent>: false, <courses>: [<Math - <about geometry>>, <Science>]}";
jujson::json_parser<std::string, custom_trait> parser(jsonStr);
jujson::json_value<std::string, custom_trait> jsonObj = parser.parse_json_object_expected();
if (jsonObj.is_valid()) {
auto nameKV = jsonObj.find("name");
if (nameKV != jsonObj.get_childs().end()) {
std::cout << "Name: " << nameKV->value().data() << '\n';
}
auto ageKV = jsonObj.find("age");
if (ageKV != jsonObj.get_childs().end()) {
std::cout << "Age: " << ageKV->value().data() << '\n';
}
auto isStudentKV = jsonObj.find("isStudent");
if (isStudentKV != jsonObj.get_childs().end()) {
std::cout << "Is Student: " << isStudentKV->value().data() << '\n';
}
auto coursesKV = jsonObj.find("courses");
if (coursesKV != jsonObj.get_childs().end()) {
for (const auto& courseKVs : coursesKV->value().get_childs()) {
std::cout << "Course: " << courseKVs.data() << '\n';
}
}
std::cout << '\n';
return true;
} else {
std::cerr << parser.line() << ':' << parser.column() << ':' << " error parsing JSON: " << parser.get_last_error() << "\n\n";
return false;
}
}
int main() {
std::cout << (int)default_test() + (int)wchar_test() + (int)accessing() + (int)custom_type_test();
return 0;
}