1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | #include "azure_c_shared_utility/gballoc.h"
|
---|
5 | #include "azure_c_shared_utility/crt_abstractions.h"
|
---|
6 |
|
---|
7 | #include "jsondecoder.h"
|
---|
8 | #include <stdio.h>
|
---|
9 | #include <string.h>
|
---|
10 | #include <ctype.h>
|
---|
11 | #include <stddef.h>
|
---|
12 |
|
---|
13 | #define IsWhiteSpace(A) (((A) == 0x20) || ((A) == 0x09) || ((A) == 0x0A) || ((A) == 0x0D))
|
---|
14 |
|
---|
15 | typedef struct PARSER_STATE_TAG
|
---|
16 | {
|
---|
17 | char* json;
|
---|
18 | } PARSER_STATE;
|
---|
19 |
|
---|
20 | static JSON_DECODER_RESULT ParseArray(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode);
|
---|
21 | static JSON_DECODER_RESULT ParseObject(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode);
|
---|
22 |
|
---|
23 | /* Codes_SRS_JSON_DECODER_99_049:[ JSONDecoder shall not allocate new string values for the leafs, but rather point to strings in the original JSON.] */
|
---|
24 | static void NoFreeFunction(void* value)
|
---|
25 | {
|
---|
26 | (void)value;
|
---|
27 | }
|
---|
28 |
|
---|
29 | static int NOPCloneFunction(void** destination, const void* source)
|
---|
30 | {
|
---|
31 | *destination = (void**)source;
|
---|
32 | return 0;
|
---|
33 | }
|
---|
34 |
|
---|
35 | void SkipWhiteSpaces(PARSER_STATE* parserState)
|
---|
36 | {
|
---|
37 | while ((*(parserState->json) != '\0') && IsWhiteSpace(*(parserState->json)))
|
---|
38 | {
|
---|
39 | parserState->json++;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | static JSON_DECODER_RESULT ParseString(PARSER_STATE* parserState, char** stringBegin)
|
---|
44 | {
|
---|
45 | JSON_DECODER_RESULT result = JSON_DECODER_OK;
|
---|
46 | *stringBegin = parserState->json;
|
---|
47 |
|
---|
48 | /* Codes_SRS_JSON_DECODER_99_028:[ A string begins and ends with quotation marks.] */
|
---|
49 | if (*(parserState->json) != '"')
|
---|
50 | {
|
---|
51 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
52 | result = JSON_DECODER_PARSE_ERROR;
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | parserState->json++;
|
---|
57 | while ((*(parserState->json) != '"') && (*(parserState->json) != '\0'))
|
---|
58 | {
|
---|
59 | /* Codes_SRS_JSON_DECODER_99_030:[ Any character may be escaped.] */
|
---|
60 | /* Codes_SRS_JSON_DECODER_99_033:[ Alternatively, there are two-character sequence escape representations of some popular characters. So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\\".] */
|
---|
61 | if (*(parserState->json) == '\\')
|
---|
62 | {
|
---|
63 | parserState->json++;
|
---|
64 | if (
|
---|
65 | /* Codes_SRS_JSON_DECODER_99_051:[ %x5C / ; \ reverse solidus U+005C] */
|
---|
66 | (*parserState->json == '\\') ||
|
---|
67 | /* Codes_SRS_JSON_DECODER_99_050:[ %x22 / ; " quotation mark U+0022] */
|
---|
68 | (*parserState->json == '"') ||
|
---|
69 | /* Codes_SRS_JSON_DECODER_99_052:[ %x2F / ; / solidus U+002F] */
|
---|
70 | (*parserState->json == '/') ||
|
---|
71 | /* Codes_SRS_JSON_DECODER_99_053:[ %x62 / ; b backspace U+0008] */
|
---|
72 | (*parserState->json == 'b') ||
|
---|
73 | /* Codes_SRS_JSON_DECODER_99_054:[ %x66 / ; f form feed U+000C] */
|
---|
74 | (*parserState->json == 'f') ||
|
---|
75 | /* Codes_SRS_JSON_DECODER_99_055:[ %x6E / ; n line feed U+000A] */
|
---|
76 | (*parserState->json == 'n') ||
|
---|
77 | /* Codes_SRS_JSON_DECODER_99_056:[ %x72 / ; r carriage return U+000D] */
|
---|
78 | (*parserState->json == 'r') ||
|
---|
79 | /* Codes_SRS_JSON_DECODER_99_057:[ %x74 / ; t tab U+0009] */
|
---|
80 | (*parserState->json == 't'))
|
---|
81 | {
|
---|
82 | parserState->json++;
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
87 | result = JSON_DECODER_PARSE_ERROR;
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | else
|
---|
92 | {
|
---|
93 | parserState->json++;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (*(parserState->json) != '"')
|
---|
98 | {
|
---|
99 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
100 | result = JSON_DECODER_PARSE_ERROR;
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | parserState->json++;
|
---|
105 | result = JSON_DECODER_OK;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | return result;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static JSON_DECODER_RESULT ParseNumber(PARSER_STATE* parserState)
|
---|
113 | {
|
---|
114 | JSON_DECODER_RESULT result = JSON_DECODER_OK;
|
---|
115 | size_t digitCount = 0;
|
---|
116 |
|
---|
117 | if (*(parserState->json) == '-')
|
---|
118 | {
|
---|
119 | parserState->json++;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* Codes_SRS_JSON_DECODER_99_043:[ A number contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part.] */
|
---|
123 | while (*(parserState->json) != '\0')
|
---|
124 | {
|
---|
125 | /* Codes_SRS_JSON_DECODER_99_044:[ Octal and hex forms are not allowed.] */
|
---|
126 | if (ISDIGIT(*(parserState->json)))
|
---|
127 | {
|
---|
128 | digitCount++;
|
---|
129 | /* simply continue */
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | break;
|
---|
134 | }
|
---|
135 |
|
---|
136 | parserState->json++;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if ((digitCount == 0) ||
|
---|
140 | /* Codes_SRS_JSON_DECODER_99_045:[ Leading zeros are not allowed.] */
|
---|
141 | ((digitCount > 1) && *(parserState->json - digitCount) == '0'))
|
---|
142 | {
|
---|
143 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
144 | result = JSON_DECODER_PARSE_ERROR;
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | /* Codes_SRS_JSON_DECODER_99_046:[ A fraction part is a decimal point followed by one or more digits.] */
|
---|
149 | if (*(parserState->json) == '.')
|
---|
150 | {
|
---|
151 | /* optional fractional part */
|
---|
152 | parserState->json++;
|
---|
153 | digitCount = 0;
|
---|
154 |
|
---|
155 | while (*(parserState->json) != '\0')
|
---|
156 | {
|
---|
157 | /* Codes_SRS_JSON_DECODER_99_044:[ Octal and hex forms are not allowed.] */
|
---|
158 | if (ISDIGIT(*(parserState->json)))
|
---|
159 | {
|
---|
160 | digitCount++;
|
---|
161 | /* simply continue */
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | break;
|
---|
166 | }
|
---|
167 |
|
---|
168 | parserState->json++;
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (digitCount == 0)
|
---|
172 | {
|
---|
173 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
174 | result = JSON_DECODER_PARSE_ERROR;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Codes_SRS_JSON_DECODER_99_047:[ An exponent part begins with the letter E in upper or lowercase, which may be followed by a plus or minus sign.] */
|
---|
179 | if ((*(parserState->json) == 'e') || (*(parserState->json) == 'E'))
|
---|
180 | {
|
---|
181 | parserState->json++;
|
---|
182 |
|
---|
183 | /* optional sign */
|
---|
184 | if ((*(parserState->json) == '-') || (*(parserState->json) == '+'))
|
---|
185 | {
|
---|
186 | parserState->json++;
|
---|
187 | }
|
---|
188 |
|
---|
189 | digitCount = 0;
|
---|
190 |
|
---|
191 | /* Codes_SRS_JSON_DECODER_99_048:[ The E and optional sign are followed by one or more digits.] */
|
---|
192 | while (*(parserState->json) != '\0')
|
---|
193 | {
|
---|
194 | /* Codes_SRS_JSON_DECODER_99_044:[ Octal and hex forms are not allowed.] */
|
---|
195 | if (ISDIGIT(*(parserState->json)))
|
---|
196 | {
|
---|
197 | digitCount++;
|
---|
198 | /* simply continue */
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | break;
|
---|
203 | }
|
---|
204 |
|
---|
205 | parserState->json++;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (digitCount == 0)
|
---|
209 | {
|
---|
210 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
211 | result = JSON_DECODER_PARSE_ERROR;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | return result;
|
---|
217 | }
|
---|
218 |
|
---|
219 | static JSON_DECODER_RESULT ParseValue(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode, char** stringBegin)
|
---|
220 | {
|
---|
221 | JSON_DECODER_RESULT result;
|
---|
222 |
|
---|
223 | SkipWhiteSpaces(parserState);
|
---|
224 |
|
---|
225 | if (*(parserState->json) == '"')
|
---|
226 | {
|
---|
227 | result = ParseString(parserState, stringBegin);
|
---|
228 | }
|
---|
229 | /* Codes_SRS_JSON_DECODER_99_018:[ A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true] */
|
---|
230 | /* Codes_SRS_JSON_DECODER_99_019:[ The literal names MUST be lowercase.] */
|
---|
231 | /* Codes_SRS_JSON_DECODER_99_020:[ No other literal names are allowed.] */
|
---|
232 | else if (strncmp(parserState->json, "false", 5) == 0)
|
---|
233 | {
|
---|
234 | *stringBegin = parserState->json;
|
---|
235 | parserState->json += 5;
|
---|
236 | result = JSON_DECODER_OK;
|
---|
237 | }
|
---|
238 | else if (strncmp(parserState->json, "true", 4) == 0)
|
---|
239 | {
|
---|
240 | *stringBegin = parserState->json;
|
---|
241 | parserState->json += 4;
|
---|
242 | result = JSON_DECODER_OK;
|
---|
243 | }
|
---|
244 | else if (strncmp(parserState->json, "null", 4) == 0)
|
---|
245 | {
|
---|
246 | *stringBegin = parserState->json;
|
---|
247 | parserState->json += 4;
|
---|
248 | result = JSON_DECODER_OK;
|
---|
249 | }
|
---|
250 | /* Tests_SRS_JSON_DECODER_99_018:[ A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true] */
|
---|
251 | else if (*(parserState->json) == '[')
|
---|
252 | {
|
---|
253 | result = ParseArray(parserState, currentNode);
|
---|
254 | *stringBegin = NULL;
|
---|
255 | }
|
---|
256 | else if (*(parserState->json) == '{')
|
---|
257 | {
|
---|
258 | result = ParseObject(parserState, currentNode);
|
---|
259 | *stringBegin = NULL;
|
---|
260 | }
|
---|
261 | else if (
|
---|
262 | (
|
---|
263 | ISDIGIT(*(parserState->json))
|
---|
264 | )
|
---|
265 | || (*(parserState->json) == '-'))
|
---|
266 | {
|
---|
267 | *stringBegin = parserState->json;
|
---|
268 | result = ParseNumber(parserState);
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
273 | result = JSON_DECODER_PARSE_ERROR;
|
---|
274 | }
|
---|
275 |
|
---|
276 | return result;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static JSON_DECODER_RESULT ParseColon(PARSER_STATE* parserState)
|
---|
280 | {
|
---|
281 | JSON_DECODER_RESULT result;
|
---|
282 |
|
---|
283 | SkipWhiteSpaces(parserState);
|
---|
284 | /* Codes_SRS_JSON_DECODER_99_023:[ A single colon comes after each name, separating the name from the value.] */
|
---|
285 | if (*(parserState->json) != ':')
|
---|
286 | {
|
---|
287 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
288 | result = JSON_DECODER_PARSE_ERROR;
|
---|
289 | }
|
---|
290 | else
|
---|
291 | {
|
---|
292 | parserState->json++;
|
---|
293 | result = JSON_DECODER_OK;
|
---|
294 | }
|
---|
295 |
|
---|
296 | return result;
|
---|
297 | }
|
---|
298 |
|
---|
299 | static JSON_DECODER_RESULT ParseOpenCurly(PARSER_STATE* parserState)
|
---|
300 | {
|
---|
301 | JSON_DECODER_RESULT result = JSON_DECODER_OK;
|
---|
302 |
|
---|
303 | SkipWhiteSpaces(parserState);
|
---|
304 |
|
---|
305 | /* Codes_SRS_JSON_DECODER_99_021:[ An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members).] */
|
---|
306 | if (*(parserState->json) != '{')
|
---|
307 | {
|
---|
308 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
309 | result = JSON_DECODER_PARSE_ERROR;
|
---|
310 | }
|
---|
311 | else
|
---|
312 | {
|
---|
313 | parserState->json++;
|
---|
314 | result = JSON_DECODER_OK;
|
---|
315 | }
|
---|
316 |
|
---|
317 | return result;
|
---|
318 | }
|
---|
319 |
|
---|
320 | static JSON_DECODER_RESULT ParseNameValuePair(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
|
---|
321 | {
|
---|
322 | JSON_DECODER_RESULT result;
|
---|
323 | char* memberNameBegin;
|
---|
324 |
|
---|
325 | SkipWhiteSpaces(parserState);
|
---|
326 |
|
---|
327 | /* Codes_SRS_JSON_DECODER_99_022:[ A name is a string.] */
|
---|
328 | result = ParseString(parserState, &memberNameBegin);
|
---|
329 | if (result == JSON_DECODER_OK)
|
---|
330 | {
|
---|
331 | char* valueBegin;
|
---|
332 | MULTITREE_HANDLE childNode;
|
---|
333 | *(parserState->json - 1) = 0;
|
---|
334 |
|
---|
335 | result = ParseColon(parserState);
|
---|
336 | if (result == JSON_DECODER_OK)
|
---|
337 | {
|
---|
338 | /* Codes_SRS_JSON_DECODER_99_025:[ The names within an object SHOULD be unique.] */
|
---|
339 | /* Multi Tree takes care of not having 2 children with the same name */
|
---|
340 | /* Codes_SRS_JSON_DECODER_99_002:[ JSONDecoder_JSON_To_MultiTree shall use the MultiTree APIs to create the multi tree and add leafs to the multi tree.] */
|
---|
341 | /* Codes_SRS_JSON_DECODER_99_003:[ When a JSON element is decoded from the JSON object then a leaf shall be added to the MultiTree.] */
|
---|
342 | /* Codes_SRS_JSON_DECODER_99_004:[ The leaf node name in the multi tree shall be the JSON element name.] */
|
---|
343 | if (MultiTree_AddChild(currentNode, memberNameBegin + 1, &childNode) != MULTITREE_OK)
|
---|
344 | {
|
---|
345 | /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
|
---|
346 | result = JSON_DECODER_MULTITREE_FAILED;
|
---|
347 | }
|
---|
348 | else
|
---|
349 | {
|
---|
350 | result = ParseValue(parserState, childNode, &valueBegin);
|
---|
351 | if ((result == JSON_DECODER_OK) && (valueBegin != NULL))
|
---|
352 | {
|
---|
353 | /* Codes_SRS_JSON_DECODER_99_005:[ The leaf node added in the multi tree shall have the value the string value of the JSON element as parsed from the JSON object.] */
|
---|
354 | if (MultiTree_SetValue(childNode, valueBegin) != MULTITREE_OK)
|
---|
355 | {
|
---|
356 | /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
|
---|
357 | result = JSON_DECODER_MULTITREE_FAILED;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | return result;
|
---|
365 | }
|
---|
366 |
|
---|
367 | static JSON_DECODER_RESULT ParseObject(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
|
---|
368 | {
|
---|
369 | JSON_DECODER_RESULT result = ParseOpenCurly(parserState);
|
---|
370 | if (result == JSON_DECODER_OK)
|
---|
371 | {
|
---|
372 | char jsonChar;
|
---|
373 |
|
---|
374 | SkipWhiteSpaces(parserState);
|
---|
375 |
|
---|
376 | jsonChar = *(parserState->json);
|
---|
377 | while ((jsonChar != '}') && (jsonChar != '\0'))
|
---|
378 | {
|
---|
379 | char* valueEnd;
|
---|
380 |
|
---|
381 | /* decode each value */
|
---|
382 | result = ParseNameValuePair(parserState, currentNode);
|
---|
383 | if (result != JSON_DECODER_OK)
|
---|
384 | {
|
---|
385 | break;
|
---|
386 | }
|
---|
387 |
|
---|
388 | valueEnd = parserState->json;
|
---|
389 |
|
---|
390 | SkipWhiteSpaces(parserState);
|
---|
391 | jsonChar = *(parserState->json);
|
---|
392 | *valueEnd = 0;
|
---|
393 |
|
---|
394 | /* Codes_SRS_JSON_DECODER_99_024:[ A single comma separates a value from a following name.] */
|
---|
395 | if (jsonChar == ',')
|
---|
396 | {
|
---|
397 | parserState->json++;
|
---|
398 | /* get the next name/value pair */
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | if (result != JSON_DECODER_OK)
|
---|
403 | {
|
---|
404 | /* already have error */
|
---|
405 | }
|
---|
406 | else
|
---|
407 | {
|
---|
408 | if (jsonChar != '}')
|
---|
409 | {
|
---|
410 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
411 | result = JSON_DECODER_PARSE_ERROR;
|
---|
412 | }
|
---|
413 | else
|
---|
414 | {
|
---|
415 | parserState->json++;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | return result;
|
---|
421 | }
|
---|
422 |
|
---|
423 | static JSON_DECODER_RESULT ParseArray(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
|
---|
424 | {
|
---|
425 | JSON_DECODER_RESULT result = JSON_DECODER_OK;
|
---|
426 |
|
---|
427 | SkipWhiteSpaces(parserState);
|
---|
428 |
|
---|
429 | /* Codes_SRS_JSON_DECODER_99_026:[ An array structure is represented as square brackets surrounding zero or more values (or elements).] */
|
---|
430 | if (*(parserState->json) != '[')
|
---|
431 | {
|
---|
432 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
433 | result = JSON_DECODER_PARSE_ERROR;
|
---|
434 | }
|
---|
435 | else
|
---|
436 | {
|
---|
437 | char* stringBegin;
|
---|
438 | char jsonChar;
|
---|
439 | int arrayIndex = 0;
|
---|
440 | result = JSON_DECODER_OK;
|
---|
441 |
|
---|
442 | parserState->json++;
|
---|
443 |
|
---|
444 | SkipWhiteSpaces(parserState);
|
---|
445 |
|
---|
446 | jsonChar = *parserState->json;
|
---|
447 | while ((jsonChar != ']') && (jsonChar != '\0'))
|
---|
448 | {
|
---|
449 | char arrayIndexStr[22];
|
---|
450 | MULTITREE_HANDLE childNode;
|
---|
451 |
|
---|
452 | /* Codes_SRS_JSON_DECODER_99_039:[ For array elements the multi tree node name shall be the string representation of the array index.] */
|
---|
453 | if (sprintf(arrayIndexStr, "%d", arrayIndex++) < 0)
|
---|
454 | {
|
---|
455 | result = JSON_DECODER_ERROR;
|
---|
456 | break;
|
---|
457 | }
|
---|
458 | /* Codes_SRS_JSON_DECODER_99_003:[ When a JSON element is decoded from the JSON object then a leaf shall be added to the MultiTree.] */
|
---|
459 | else if (MultiTree_AddChild(currentNode, arrayIndexStr, &childNode) != MULTITREE_OK)
|
---|
460 | {
|
---|
461 | /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
|
---|
462 | result = JSON_DECODER_MULTITREE_FAILED;
|
---|
463 | }
|
---|
464 | else
|
---|
465 | {
|
---|
466 | char* valueEnd;
|
---|
467 |
|
---|
468 | /* decode each value */
|
---|
469 | result = ParseValue(parserState, childNode, &stringBegin);
|
---|
470 | if (result != JSON_DECODER_OK)
|
---|
471 | {
|
---|
472 | break;
|
---|
473 | }
|
---|
474 |
|
---|
475 | if (stringBegin != NULL)
|
---|
476 | {
|
---|
477 | /* Codes_SRS_JSON_DECODER_99_005:[ The leaf node added in the multi tree shall have the value the string value of the JSON element as parsed from the JSON object.] */
|
---|
478 | if (MultiTree_SetValue(childNode, stringBegin) != MULTITREE_OK)
|
---|
479 | {
|
---|
480 | /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
|
---|
481 | result = JSON_DECODER_MULTITREE_FAILED;
|
---|
482 | break;
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | valueEnd = parserState->json;
|
---|
487 |
|
---|
488 | SkipWhiteSpaces(parserState);
|
---|
489 | jsonChar = *(parserState->json);
|
---|
490 | *valueEnd = 0;
|
---|
491 |
|
---|
492 | /* Codes_SRS_JSON_DECODER_99_027:[ Elements are separated by commas.] */
|
---|
493 | if (jsonChar == ',')
|
---|
494 | {
|
---|
495 | parserState->json++;
|
---|
496 | /* get the next value pair */
|
---|
497 | }
|
---|
498 | else if (jsonChar == ']')
|
---|
499 | {
|
---|
500 | break;
|
---|
501 | }
|
---|
502 | else
|
---|
503 | {
|
---|
504 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
505 | result = JSON_DECODER_PARSE_ERROR;
|
---|
506 | break;
|
---|
507 | }
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | if (result != JSON_DECODER_OK)
|
---|
512 | {
|
---|
513 | /* already have error */
|
---|
514 | }
|
---|
515 | else
|
---|
516 | {
|
---|
517 | if (jsonChar != ']')
|
---|
518 | {
|
---|
519 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
520 | result = JSON_DECODER_PARSE_ERROR;
|
---|
521 | }
|
---|
522 | else
|
---|
523 | {
|
---|
524 | parserState->json++;
|
---|
525 | SkipWhiteSpaces(parserState);
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | return result;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /* Codes_SRS_JSON_DECODER_99_012:[ A JSON text is a serialized object or array.] */
|
---|
534 | static JSON_DECODER_RESULT ParseObjectOrArray(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
|
---|
535 | {
|
---|
536 | JSON_DECODER_RESULT result = JSON_DECODER_PARSE_ERROR;
|
---|
537 |
|
---|
538 | SkipWhiteSpaces(parserState);
|
---|
539 |
|
---|
540 | if (*(parserState->json) == '{')
|
---|
541 | {
|
---|
542 | result = ParseObject(parserState, currentNode);
|
---|
543 | SkipWhiteSpaces(parserState);
|
---|
544 | }
|
---|
545 | else if (*(parserState->json) == '[')
|
---|
546 | {
|
---|
547 | result = ParseArray(parserState, currentNode);
|
---|
548 | SkipWhiteSpaces(parserState);
|
---|
549 | }
|
---|
550 | else
|
---|
551 | {
|
---|
552 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
553 | result = JSON_DECODER_PARSE_ERROR;
|
---|
554 | }
|
---|
555 |
|
---|
556 | if ((result == JSON_DECODER_OK) &&
|
---|
557 | (*(parserState->json) != '\0'))
|
---|
558 | {
|
---|
559 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
560 | result = JSON_DECODER_PARSE_ERROR;
|
---|
561 | }
|
---|
562 |
|
---|
563 | return result;
|
---|
564 | }
|
---|
565 |
|
---|
566 | static JSON_DECODER_RESULT ParseJSON(char* json, MULTITREE_HANDLE currentNode)
|
---|
567 | {
|
---|
568 | /* Codes_SRS_JSON_DECODER_99_009:[ On success, JSONDecoder_JSON_To_MultiTree shall return a handle to the multi tree it created in the multiTreeHandle argument and it shall return JSON_DECODER_OK.] */
|
---|
569 | PARSER_STATE parseState;
|
---|
570 | parseState.json = json;
|
---|
571 | return ParseObjectOrArray(&parseState, currentNode);
|
---|
572 | }
|
---|
573 |
|
---|
574 | JSON_DECODER_RESULT JSONDecoder_JSON_To_MultiTree(char* json, MULTITREE_HANDLE* multiTreeHandle)
|
---|
575 | {
|
---|
576 | JSON_DECODER_RESULT result;
|
---|
577 |
|
---|
578 | if ((json == NULL) ||
|
---|
579 | (multiTreeHandle == NULL))
|
---|
580 | {
|
---|
581 | /* Codes_SRS_JSON_DECODER_99_001:[ If any of the parameters passed to the JSONDecoder_JSON_To_MultiTree function is NULL then the function call shall return JSON_DECODER_INVALID_ARG.] */
|
---|
582 | result = JSON_DECODER_INVALID_ARG;
|
---|
583 | }
|
---|
584 | else if (*json == '\0')
|
---|
585 | {
|
---|
586 | /* Codes_SRS_JSON_DECODER_99_007:[ If parsing the JSON fails due to the JSON string being malformed, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_PARSE_ERROR.] */
|
---|
587 | result = JSON_DECODER_PARSE_ERROR;
|
---|
588 | }
|
---|
589 | else
|
---|
590 | {
|
---|
591 | /* Codes_SRS_JSON_DECODER_99_008:[ JSONDecoder_JSON_To_MultiTree shall create a multi tree based on the json string argument.] */
|
---|
592 | /* Codes_SRS_JSON_DECODER_99_002:[ JSONDecoder_JSON_To_MultiTree shall use the MultiTree APIs to create the multi tree and add leafs to the multi tree.] */
|
---|
593 | /* Codes_SRS_JSON_DECODER_99_009:[ On success, JSONDecoder_JSON_To_MultiTree shall return a handle to the multi tree it created in the multiTreeHandle argument and it shall return JSON_DECODER_OK.] */
|
---|
594 | *multiTreeHandle = MultiTree_Create(NOPCloneFunction, NoFreeFunction);
|
---|
595 | if (*multiTreeHandle == NULL)
|
---|
596 | {
|
---|
597 | /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
|
---|
598 | result = JSON_DECODER_MULTITREE_FAILED;
|
---|
599 | }
|
---|
600 | else if (strcmp(json, "null") == 0) {
|
---|
601 | result = JSON_DECODER_OK;
|
---|
602 | }
|
---|
603 | else
|
---|
604 | {
|
---|
605 | result = ParseJSON(json, *multiTreeHandle);
|
---|
606 | if (result != JSON_DECODER_OK)
|
---|
607 | {
|
---|
608 | MultiTree_Destroy(*multiTreeHandle);
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | return result;
|
---|
614 | }
|
---|