This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJsonTest.cpp
More file actions
130 lines (116 loc) · 3.21 KB
/
Copy pathJsonTest.cpp
File metadata and controls
130 lines (116 loc) · 3.21 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
// This file is UTF-8
// The program uses WCHAR Unicode representation (either UTF-32 or UTF-16 on Windows)
#include <Extra/Json.hpp>
#include <Io.hpp>
#include <cassert>
#include <cstdlib>
using namespace hsd::string_view_literals;
static constexpr auto s_test_string =
LR"json({
"Cow": "milk",
"gold": 5000,
"primes": [2, 3, 5, 7, 11, 13, 23],
"Unicode": "蛋",
"MontyPython": "import pickle",
"LoS": [
{
"name": "A",
"age": 10
},
{
"name": "B",
"age": 20
}
],
"SoA": {
"name": ["A", "B"],
"age": [10, 20]
},
"primitives": [null, true, false]
})json"_sv;
void print_prefix(hsd::vector<hsd::wstring_view>& path);
void print_value(hsd::vector<hsd::wstring_view>& path, hsd::JsonValue& v);
int main()
{
hsd::JsonStream<hsd::wchar> lexer;
// Lex the string
lexer.lex(s_test_string).unwrap();
lexer.push_eot().unwrap();
hsd::JsonParser parser(lexer);
// Parse the string
auto value = parser.parse_all().unwrap();
// Assert that the whole object is complete
assert(value->is_complete());
// Traverse the object
hsd::vector<hsd::wstring_view> path;
print_value(path, *value);
}
void print_prefix(hsd::vector<hsd::wstring_view>& path)
{
if (path.size() == 0)
hsd_print(L"<root>");
else
{
for (auto part : path)
hsd_print(L"{}:", part);
}
hsd_print(L" ");
}
void print_value(hsd::vector<hsd::wstring_view>& path, hsd::JsonValue& v)
{
print_prefix(path);
hsd::JsonValueType type = v.type();
switch (type)
{
case hsd::JsonValueType::Null:
hsd_print(L"null");
break;
case hsd::JsonValueType::True:
hsd_print(L"true");
break;
case hsd::JsonValueType::False:
hsd_print(L"false");
break;
case hsd::JsonValueType::Number:
hsd_print(L"{}", v.as_num<hsd::i32>().unwrap());
break;
case hsd::JsonValueType::String:
// TODO: Escape?
hsd_print(L"\"{}\"", v.as_str<hsd::wchar>().unwrap());
break;
case hsd::JsonValueType::Array:
hsd_print(L"array");
break;
case hsd::JsonValueType::Object:
hsd_print(L"object");
break;
case hsd::JsonValueType::StreamingArray:
case hsd::JsonValueType::StreamingObject:
// Uh, oh
hsd_println_err("Unexcpected streaming object");
abort();
}
hsd_println(L"");
if (type == hsd::JsonValueType::Array)
{
auto& vec = v.as_array();
for (hsd::usize idx = 0; auto& it : vec)
{
auto s = hsd::to_wstring(idx);
path.push_back(static_cast<hsd::wstring_view>(s));
print_value(path, *it);
path.pop_back();
idx++;
}
}
else if (type == hsd::JsonValueType::Object)
{
auto& map = v.as_object<hsd::wchar>();
for (auto& kv : map)
{
path.push_back(static_cast<hsd::wstring_view>(kv.first));
print_value(path, *kv.second);
path.pop_back();
}
}
}