1#include <base/dbg.h>
2#include <base/str.h>
3
4#include <engine/shared/json.h>
5
6#include <vector>
7
8static bool JsonValidateUtf8(const json_value *pRoot)
9{
10 std::vector<const json_value *> vpValues;
11 vpValues.push_back(x: pRoot);
12 while(!vpValues.empty())
13 {
14 const json_value *pValue = vpValues.back();
15 vpValues.pop_back();
16 switch(pValue->type)
17 {
18 case json_string:
19 if(!str_utf8_check(str: *pValue))
20 {
21 return false;
22 }
23 break;
24 case json_array:
25 for(unsigned i = 0; i < pValue->u.array.length; i++)
26 {
27 vpValues.push_back(x: pValue->u.array.values[i]);
28 }
29 break;
30 case json_object:
31 for(unsigned i = 0; i < pValue->u.object.length; i++)
32 {
33 if(!str_utf8_check(str: pValue->u.object.values[i].name))
34 {
35 return false;
36 }
37 vpValues.push_back(x: pValue->u.object.values[i].value);
38 }
39 break;
40 default:
41 break;
42 }
43 }
44 return true;
45}
46
47json_value *JsonParse(const json_char *pJson, size_t Length)
48{
49 json_value *pValue = json_parse(json: pJson, length: Length);
50 if(pValue == nullptr)
51 {
52 return nullptr;
53 }
54 if(!JsonValidateUtf8(pRoot: pValue))
55 {
56 json_value_free(pValue);
57 return nullptr;
58 }
59 return pValue;
60}
61
62json_value *JsonParseEx(json_settings *pSettings, const json_char *pJson, size_t Length, char *pError)
63{
64 json_value *pValue = json_parse_ex(settings: pSettings, json: pJson, length: Length, error: pError);
65 if(pValue == nullptr)
66 {
67 return nullptr;
68 }
69 if(!JsonValidateUtf8(pRoot: pValue))
70 {
71 if(pError)
72 {
73 str_copy(dst: pError, src: "invalid utf-8 in string literal", json_error_max);
74 }
75 json_value_free(pValue);
76 return nullptr;
77 }
78 return pValue;
79}
80
81const struct _json_value *json_object_get(const json_value *pObject, const char *pIndex)
82{
83 unsigned int i;
84
85 if(pObject->type != json_object)
86 return &json_value_none;
87
88 for(i = 0; i < pObject->u.object.length; ++i)
89 if(!str_comp(a: pObject->u.object.values[i].name, b: pIndex))
90 return pObject->u.object.values[i].value;
91
92 return &json_value_none;
93}
94
95const struct _json_value *json_array_get(const json_value *pArray, int Index)
96{
97 if(pArray->type != json_array || Index >= (int)pArray->u.array.length)
98 return &json_value_none;
99
100 return pArray->u.array.values[Index];
101}
102
103int json_array_length(const json_value *pArray)
104{
105 return pArray->u.array.length;
106}
107
108const char *json_string_get(const json_value *pString)
109{
110 return pString->u.string.ptr;
111}
112
113int json_int_get(const json_value *pInteger)
114{
115 return pInteger->u.integer;
116}
117
118int json_boolean_get(const json_value *pBoolean)
119{
120 return pBoolean->u.boolean != 0;
121}
122
123char EscapeJsonChar(char c)
124{
125 switch(c)
126 {
127 case '\"': return '\"';
128 case '\\': return '\\';
129 case '\b': return 'b';
130 case '\n': return 'n';
131 case '\r': return 'r';
132 case '\t': return 't';
133 // Don't escape '\f', who uses that. :)
134 default: return 0;
135 }
136}
137
138char *EscapeJson(char *pBuffer, int BufferSize, const char *pString)
139{
140 dbg_assert(BufferSize > 0, "can't null-terminate the string");
141 dbg_assert(str_utf8_check(pString), "invalid UTF-8 in string");
142 // Subtract the space for null termination early.
143 BufferSize--;
144
145 char *pResult = pBuffer;
146 while(BufferSize && *pString)
147 {
148 char c = *pString;
149 pString++;
150 char Escaped = EscapeJsonChar(c);
151 if(Escaped)
152 {
153 if(BufferSize < 2)
154 {
155 break;
156 }
157 *pBuffer++ = '\\';
158 *pBuffer++ = Escaped;
159 BufferSize -= 2;
160 }
161 // Assuming ASCII/UTF-8, "if control character".
162 else if((unsigned char)c < 0x20)
163 {
164 // \uXXXX
165 if(BufferSize < 6)
166 {
167 break;
168 }
169 // +1 for null termination or this would be truncated with `BufferSize == 6`.
170 // We know this fits because space for null termination is reserved early.
171 str_format(buffer: pBuffer, buffer_size: BufferSize + 1, format: "\\u%04x", c);
172 pBuffer += 6;
173 BufferSize -= 6;
174 }
175 else
176 {
177 *pBuffer++ = c;
178 BufferSize--;
179 }
180 }
181 *pBuffer = 0;
182 return pResult;
183}
184
185const char *JsonBool(bool Bool)
186{
187 if(Bool)
188 {
189 return "true";
190 }
191 else
192 {
193 return "false";
194 }
195}
196