-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
183 lines (163 loc) · 3.35 KB
/
Copy pathjson.cpp
File metadata and controls
183 lines (163 loc) · 3.35 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "net/json.hpp"
namespace droidcli::net {
core::String escape_json_string(const core::String& input)
{
core::String escaped;
escaped.reserve(input.size() + 8);
for (const char character : input)
{
switch (character)
{
case '\\':
escaped += "\\\\";
break;
case '"':
escaped += "\\\"";
break;
case '\n':
escaped += "\\n";
break;
case '\r':
escaped += "\\r";
break;
case '\t':
escaped += "\\t";
break;
default:
escaped += character;
break;
}
}
return escaped;
}
core::String json_string_field(const core::String& key, const core::String& value)
{
return "\"" + key + "\":\"" + escape_json_string(value) + "\"";
}
core::String json_bool_field(const core::String& key, const bool value)
{
return "\"" + key + "\":" + (value ? "true" : "false");
}
core::String extract_json_string_field(
const core::String& json,
const core::String& field_name,
const size_t search_from)
{
const core::String needle = "\"" + field_name + "\":";
const size_t field_index = json.find(needle, search_from);
if (field_index == core::String::npos)
{
return {};
}
size_t cursor = field_index + needle.size();
while (cursor < json.size() && (json[cursor] == ' ' || json[cursor] == '\t'))
{
++cursor;
}
if (cursor >= json.size() || json[cursor] != '"')
{
return {};
}
++cursor;
core::String value;
while (cursor < json.size())
{
const char character = json[cursor++];
if (character == '\\' && cursor < json.size())
{
const char escaped = json[cursor++];
switch (escaped)
{
case '"':
value += '"';
break;
case '\\':
value += '\\';
break;
case 'n':
value += '\n';
break;
case 'r':
value += '\r';
break;
case 't':
value += '\t';
break;
default:
value += escaped;
break;
}
continue;
}
if (character == '"')
{
break;
}
value += character;
}
return value;
}
bool extract_json_bool_field(
const core::String& json,
const core::String& field_name,
bool& out_value,
const size_t search_from)
{
const core::String needle = "\"" + field_name + "\":";
const size_t field_index = json.find(needle, search_from);
if (field_index == core::String::npos)
{
return false;
}
size_t cursor = field_index + needle.size();
while (cursor < json.size() && (json[cursor] == ' ' || json[cursor] == '\t'))
{
++cursor;
}
if (json.compare(cursor, 4, "true") == 0)
{
out_value = true;
return true;
}
if (json.compare(cursor, 5, "false") == 0)
{
out_value = false;
return true;
}
return false;
}
bool extract_json_int_field(
const core::String& json,
const core::String& field_name,
int64_t& out_value,
const size_t search_from)
{
const core::String needle = "\"" + field_name + "\":";
const size_t field_index = json.find(needle, search_from);
if (field_index == core::String::npos)
{
return false;
}
size_t cursor = field_index + needle.size();
while (cursor < json.size() && (json[cursor] == ' ' || json[cursor] == '\t'))
{
++cursor;
}
const size_t start = cursor;
if (cursor < json.size() && json[cursor] == '-')
{
++cursor;
}
const size_t digits_start = cursor;
while (cursor < json.size() && json[cursor] >= '0' && json[cursor] <= '9')
{
++cursor;
}
if (cursor == digits_start)
{
return false;
}
out_value = std::stoll(json.substr(start, cursor - start));
return true;
}
} // namespace droidcli::net