Azure IoT / serializer

Dependents:   sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jsondecoder.c Source File

jsondecoder.c

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include "azure_c_shared_utility/gballoc.h"
00005 #include "azure_c_shared_utility/crt_abstractions.h"
00006 
00007 #include "jsondecoder.h"
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <ctype.h>
00011 #include <stddef.h>
00012 
00013 #define IsWhiteSpace(A) (((A) == 0x20) || ((A) == 0x09) || ((A) == 0x0A) || ((A) == 0x0D))
00014 
00015 typedef struct PARSER_STATE_TAG
00016 {
00017     char* json;
00018 } PARSER_STATE;
00019 
00020 static JSON_DECODER_RESULT ParseArray(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode);
00021 static JSON_DECODER_RESULT ParseObject(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode);
00022 
00023 /* 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.] */
00024 static void NoFreeFunction(void* value)
00025 {
00026     (void)value;
00027 }
00028 
00029 static int NOPCloneFunction(void** destination, const void* source)
00030 {
00031     *destination = (void**)source;
00032     return 0;
00033 }
00034 
00035 void SkipWhiteSpaces(PARSER_STATE* parserState)
00036 {
00037     while ((*(parserState->json) != '\0') && IsWhiteSpace(*(parserState->json)))
00038     {
00039         parserState->json++;
00040     }
00041 }
00042 
00043 static JSON_DECODER_RESULT ParseString(PARSER_STATE* parserState, char** stringBegin)
00044 {
00045     JSON_DECODER_RESULT result = JSON_DECODER_OK;
00046     *stringBegin = parserState->json;
00047 
00048     /* Codes_SRS_JSON_DECODER_99_028:[ A string begins and ends with quotation marks.] */
00049     if (*(parserState->json) != '"')
00050     {
00051         /* 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.] */
00052         result = JSON_DECODER_PARSE_ERROR;
00053     }
00054     else
00055     {
00056         parserState->json++;
00057         while ((*(parserState->json) != '"') && (*(parserState->json) != '\0'))
00058         {
00059             /* Codes_SRS_JSON_DECODER_99_030:[ Any character may be escaped.]  */
00060             /* 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 "\\".] */
00061             if (*(parserState->json) == '\\')
00062             {
00063                 parserState->json++;
00064                 if (
00065                     /* Codes_SRS_JSON_DECODER_99_051:[ %x5C /          ; \    reverse solidus U+005C] */
00066                     (*parserState->json == '\\') ||
00067                     /* Codes_SRS_JSON_DECODER_99_050:[ %x22 /          ; "    quotation mark  U+0022] */
00068                     (*parserState->json == '"') ||
00069                     /* Codes_SRS_JSON_DECODER_99_052:[ %x2F /          ; /    solidus         U+002F] */
00070                     (*parserState->json == '/') ||
00071                     /* Codes_SRS_JSON_DECODER_99_053:[ %x62 /          ; b    backspace       U+0008] */
00072                     (*parserState->json == 'b') ||
00073                     /* Codes_SRS_JSON_DECODER_99_054:[ %x66 /          ; f    form feed       U+000C] */
00074                     (*parserState->json == 'f') ||
00075                     /* Codes_SRS_JSON_DECODER_99_055:[ %x6E /          ; n    line feed       U+000A] */
00076                     (*parserState->json == 'n') ||
00077                     /* Codes_SRS_JSON_DECODER_99_056:[ %x72 /          ; r    carriage return U+000D] */
00078                     (*parserState->json == 'r') ||
00079                     /* Codes_SRS_JSON_DECODER_99_057:[ %x74 /          ; t    tab             U+0009] */
00080                     (*parserState->json == 't'))
00081                 {
00082                     parserState->json++;
00083                 }
00084                 else
00085                 {
00086                     /* 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.] */
00087                     result = JSON_DECODER_PARSE_ERROR;
00088                     break;
00089                 }
00090             }
00091             else
00092             {
00093                 parserState->json++;
00094             }
00095         }
00096 
00097         if (*(parserState->json) != '"')
00098         {
00099             /* 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.] */
00100             result = JSON_DECODER_PARSE_ERROR;
00101         }
00102         else
00103         {
00104             parserState->json++;
00105             result = JSON_DECODER_OK;
00106         }
00107     }
00108 
00109     return result;
00110 }
00111 
00112 static JSON_DECODER_RESULT ParseNumber(PARSER_STATE* parserState)
00113 {
00114     JSON_DECODER_RESULT result = JSON_DECODER_OK;
00115     size_t digitCount = 0;
00116 
00117     if (*(parserState->json) == '-')
00118     {
00119         parserState->json++;
00120     }
00121 
00122     /* 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.] */
00123     while (*(parserState->json) != '\0')
00124     {
00125         /* Codes_SRS_JSON_DECODER_99_044:[ Octal and hex forms are not allowed.] */
00126         if (ISDIGIT(*(parserState->json)))
00127         {
00128             digitCount++;
00129             /* simply continue */
00130         }
00131         else
00132         {
00133             break;
00134         }
00135 
00136         parserState->json++;
00137     }
00138 
00139     if ((digitCount == 0) ||
00140         /* Codes_SRS_JSON_DECODER_99_045:[ Leading zeros are not allowed.] */
00141         ((digitCount > 1) && *(parserState->json - digitCount) == '0'))
00142     {
00143         /* 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.] */
00144         result = JSON_DECODER_PARSE_ERROR;
00145     }
00146     else
00147     {
00148         /* Codes_SRS_JSON_DECODER_99_046:[ A fraction part is a decimal point followed by one or more digits.] */
00149         if (*(parserState->json) == '.')
00150         {
00151             /* optional fractional part */
00152             parserState->json++;
00153             digitCount = 0;
00154 
00155             while (*(parserState->json) != '\0')
00156             {
00157                 /* Codes_SRS_JSON_DECODER_99_044:[ Octal and hex forms are not allowed.] */
00158                 if (ISDIGIT(*(parserState->json)))
00159                 {
00160                     digitCount++;
00161                     /* simply continue */
00162                 }
00163                 else
00164                 {
00165                     break;
00166                 }
00167 
00168                 parserState->json++;
00169             }
00170 
00171             if (digitCount == 0)
00172             {
00173                 /* 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.] */
00174                 result = JSON_DECODER_PARSE_ERROR;
00175             }
00176         }
00177 
00178         /* 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.] */
00179         if ((*(parserState->json) == 'e') || (*(parserState->json) == 'E'))
00180         {
00181             parserState->json++;
00182 
00183             /* optional sign */
00184             if ((*(parserState->json) == '-') || (*(parserState->json) == '+'))
00185             {
00186                 parserState->json++;
00187             }
00188 
00189             digitCount = 0;
00190 
00191             /* Codes_SRS_JSON_DECODER_99_048:[ The E and optional sign are followed by one or more digits.] */
00192             while (*(parserState->json) != '\0')
00193             {
00194                 /* Codes_SRS_JSON_DECODER_99_044:[ Octal and hex forms are not allowed.] */
00195                 if (ISDIGIT(*(parserState->json)))
00196                 {
00197                     digitCount++;
00198                     /* simply continue */
00199                 }
00200                 else
00201                 {
00202                     break;
00203                 }
00204 
00205                 parserState->json++;
00206             }
00207 
00208             if (digitCount == 0)
00209             {
00210                 /* 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.] */
00211                 result = JSON_DECODER_PARSE_ERROR;
00212             }
00213         }
00214     }
00215 
00216     return result;
00217 }
00218 
00219 static JSON_DECODER_RESULT ParseValue(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode, char** stringBegin)
00220 {
00221     JSON_DECODER_RESULT result;
00222 
00223     SkipWhiteSpaces(parserState);
00224 
00225     if (*(parserState->json) == '"')
00226     {
00227         result = ParseString(parserState, stringBegin);
00228     }
00229     /* 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] */
00230     /* Codes_SRS_JSON_DECODER_99_019:[ The literal names MUST be lowercase.] */
00231     /* Codes_SRS_JSON_DECODER_99_020:[ No other literal names are allowed.] */
00232     else if (strncmp(parserState->json, "false", 5) == 0)
00233     {
00234         *stringBegin = parserState->json;
00235         parserState->json += 5;
00236         result = JSON_DECODER_OK;
00237     }
00238     else if (strncmp(parserState->json, "true", 4) == 0)
00239     {
00240         *stringBegin = parserState->json;
00241         parserState->json += 4;
00242         result = JSON_DECODER_OK;
00243     }
00244     else if (strncmp(parserState->json, "null", 4) == 0)
00245     {
00246         *stringBegin = parserState->json;
00247         parserState->json += 4;
00248         result = JSON_DECODER_OK;
00249     }
00250     /* 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] */
00251     else if (*(parserState->json) == '[')
00252     {
00253         result = ParseArray(parserState, currentNode);
00254         *stringBegin = NULL;
00255     }
00256     else if (*(parserState->json) == '{')
00257     {
00258         result = ParseObject(parserState, currentNode);
00259         *stringBegin = NULL;
00260     }
00261     else if (
00262         (
00263             ISDIGIT(*(parserState->json))
00264         )
00265         || (*(parserState->json) == '-'))
00266     {
00267         *stringBegin = parserState->json;
00268         result = ParseNumber(parserState);
00269     }
00270     else
00271     {
00272         /* 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.] */
00273         result = JSON_DECODER_PARSE_ERROR;
00274     }
00275 
00276     return result;
00277 }
00278 
00279 static JSON_DECODER_RESULT ParseColon(PARSER_STATE* parserState)
00280 {
00281     JSON_DECODER_RESULT result;
00282 
00283     SkipWhiteSpaces(parserState);
00284     /* Codes_SRS_JSON_DECODER_99_023:[  A single colon comes after each name, separating the name from the value.] */
00285     if (*(parserState->json) != ':')
00286     {
00287         /* 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.] */
00288         result = JSON_DECODER_PARSE_ERROR;
00289     }
00290     else
00291     {
00292         parserState->json++;
00293         result = JSON_DECODER_OK;
00294     }
00295 
00296     return result;
00297 }
00298 
00299 static JSON_DECODER_RESULT ParseOpenCurly(PARSER_STATE* parserState)
00300 {
00301     JSON_DECODER_RESULT result = JSON_DECODER_OK;
00302 
00303     SkipWhiteSpaces(parserState);
00304 
00305     /* 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).] */
00306     if (*(parserState->json) != '{')
00307     {
00308         /* 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.] */
00309         result = JSON_DECODER_PARSE_ERROR;
00310     }
00311     else
00312     {
00313         parserState->json++;
00314         result = JSON_DECODER_OK;
00315     }
00316 
00317     return result;
00318 }
00319 
00320 static JSON_DECODER_RESULT ParseNameValuePair(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
00321 {
00322     JSON_DECODER_RESULT result;
00323     char* memberNameBegin;
00324 
00325     SkipWhiteSpaces(parserState);
00326 
00327     /* Codes_SRS_JSON_DECODER_99_022:[ A name is a string.] */
00328     result = ParseString(parserState, &memberNameBegin);
00329     if (result == JSON_DECODER_OK)
00330     {
00331         char* valueBegin;
00332         MULTITREE_HANDLE childNode;
00333         *(parserState->json - 1) = 0;
00334 
00335         result = ParseColon(parserState);
00336         if (result == JSON_DECODER_OK)
00337         {
00338             /* Codes_SRS_JSON_DECODER_99_025:[ The names within an object SHOULD be unique.] */
00339             /* Multi Tree takes care of not having 2 children with the same name */
00340             /* 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.] */
00341             /* 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.] */
00342             /* Codes_SRS_JSON_DECODER_99_004:[ The leaf node name in the multi tree shall be the JSON element name.] */
00343             if (MultiTree_AddChild(currentNode, memberNameBegin + 1, &childNode) != MULTITREE_OK)
00344             {
00345                 /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
00346                 result = JSON_DECODER_MULTITREE_FAILED;
00347             }
00348             else
00349             {
00350                 result = ParseValue(parserState, childNode, &valueBegin);
00351                 if ((result == JSON_DECODER_OK) && (valueBegin != NULL))
00352                 {
00353                     /* 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.] */
00354                     if (MultiTree_SetValue(childNode, valueBegin) != MULTITREE_OK)
00355                     {
00356                         /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
00357                         result = JSON_DECODER_MULTITREE_FAILED;
00358                     }
00359                 }
00360             }
00361         }
00362     }
00363 
00364     return result;
00365 }
00366 
00367 static JSON_DECODER_RESULT ParseObject(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
00368 {
00369     JSON_DECODER_RESULT result = ParseOpenCurly(parserState);
00370     if (result == JSON_DECODER_OK)
00371     {
00372         char jsonChar;
00373 
00374         SkipWhiteSpaces(parserState);
00375 
00376         jsonChar = *(parserState->json);
00377         while ((jsonChar != '}') && (jsonChar != '\0'))
00378         {
00379             char* valueEnd;
00380 
00381             /* decode each value */
00382             result = ParseNameValuePair(parserState, currentNode);
00383             if (result != JSON_DECODER_OK)
00384             {
00385                 break;
00386             }
00387 
00388             valueEnd = parserState->json;
00389 
00390             SkipWhiteSpaces(parserState);
00391             jsonChar = *(parserState->json);
00392             *valueEnd = 0;
00393 
00394             /* Codes_SRS_JSON_DECODER_99_024:[ A single comma separates a value from a following name.] */
00395             if (jsonChar == ',')
00396             {
00397                 parserState->json++;
00398                 /* get the next name/value pair */
00399             }
00400         }
00401 
00402         if (result != JSON_DECODER_OK)
00403         {
00404             /* already have error */
00405         }
00406         else
00407         {
00408             if (jsonChar != '}')
00409             {
00410                 /* 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.] */
00411                 result = JSON_DECODER_PARSE_ERROR;
00412             }
00413             else
00414             {
00415                 parserState->json++;
00416             }
00417         }
00418     }
00419 
00420     return result;
00421 }
00422 
00423 static JSON_DECODER_RESULT ParseArray(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
00424 {
00425     JSON_DECODER_RESULT result = JSON_DECODER_OK;
00426 
00427     SkipWhiteSpaces(parserState);
00428 
00429     /* Codes_SRS_JSON_DECODER_99_026:[ An array structure is represented as square brackets surrounding zero or more values (or elements).] */
00430     if (*(parserState->json) != '[')
00431     {
00432         /* 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.] */
00433         result = JSON_DECODER_PARSE_ERROR;
00434     }
00435     else
00436     {
00437         char* stringBegin;
00438         char jsonChar;
00439         int arrayIndex = 0;
00440         result = JSON_DECODER_OK;
00441 
00442         parserState->json++;
00443 
00444         SkipWhiteSpaces(parserState);
00445 
00446         jsonChar = *parserState->json;
00447         while ((jsonChar != ']') && (jsonChar != '\0'))
00448         {
00449             char arrayIndexStr[22];
00450             MULTITREE_HANDLE childNode;
00451 
00452             /* Codes_SRS_JSON_DECODER_99_039:[ For array elements the multi tree node name shall be the string representation of the array index.] */
00453             if (sprintf(arrayIndexStr, "%d", arrayIndex++) < 0)
00454             {
00455                 result = JSON_DECODER_ERROR;
00456                 break;
00457             }
00458             /* 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.] */
00459             else if (MultiTree_AddChild(currentNode, arrayIndexStr, &childNode) != MULTITREE_OK)
00460             {
00461                 /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
00462                 result = JSON_DECODER_MULTITREE_FAILED;
00463             }
00464             else
00465             {
00466                 char* valueEnd;
00467 
00468                 /* decode each value */
00469                 result = ParseValue(parserState, childNode, &stringBegin);
00470                 if (result != JSON_DECODER_OK)
00471                 {
00472                     break;
00473                 }
00474 
00475                 if (stringBegin != NULL)
00476                 {
00477                     /* 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.] */
00478                     if (MultiTree_SetValue(childNode, stringBegin) != MULTITREE_OK)
00479                     {
00480                         /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
00481                         result = JSON_DECODER_MULTITREE_FAILED;
00482                         break;
00483                     }
00484                 }
00485 
00486                 valueEnd = parserState->json;
00487 
00488                 SkipWhiteSpaces(parserState);
00489                 jsonChar = *(parserState->json);
00490                 *valueEnd = 0;
00491 
00492                 /* Codes_SRS_JSON_DECODER_99_027:[ Elements are separated by commas.] */
00493                 if (jsonChar == ',')
00494                 {
00495                     parserState->json++;
00496                     /* get the next value pair */
00497                 }
00498                 else if (jsonChar == ']')
00499                 {
00500                     break;
00501                 }
00502                 else
00503                 {
00504                     /* 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.] */
00505                     result = JSON_DECODER_PARSE_ERROR;
00506                     break;
00507                 }
00508             }
00509         }
00510 
00511         if (result != JSON_DECODER_OK)
00512         {
00513             /* already have error */
00514         }
00515         else
00516         {
00517             if (jsonChar != ']')
00518             {
00519                 /* 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.] */
00520                 result = JSON_DECODER_PARSE_ERROR;
00521             }
00522             else
00523             {
00524                 parserState->json++;
00525                 SkipWhiteSpaces(parserState);
00526             }
00527         }
00528     }
00529 
00530     return result;
00531 }
00532 
00533 /* Codes_SRS_JSON_DECODER_99_012:[ A JSON text is a serialized object or array.] */
00534 static JSON_DECODER_RESULT ParseObjectOrArray(PARSER_STATE* parserState, MULTITREE_HANDLE currentNode)
00535 {
00536     JSON_DECODER_RESULT result = JSON_DECODER_PARSE_ERROR;
00537 
00538     SkipWhiteSpaces(parserState);
00539 
00540     if (*(parserState->json) == '{')
00541     {
00542         result = ParseObject(parserState, currentNode);
00543         SkipWhiteSpaces(parserState);
00544     }
00545     else if (*(parserState->json) == '[')
00546     {
00547         result = ParseArray(parserState, currentNode);
00548         SkipWhiteSpaces(parserState);
00549     }
00550     else
00551     {
00552         /* 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.] */
00553         result = JSON_DECODER_PARSE_ERROR;
00554     }
00555 
00556     if ((result == JSON_DECODER_OK) &&
00557         (*(parserState->json) != '\0'))
00558     {
00559         /* 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.] */
00560         result = JSON_DECODER_PARSE_ERROR;
00561     }
00562 
00563     return result;
00564 }
00565 
00566 static JSON_DECODER_RESULT ParseJSON(char* json, MULTITREE_HANDLE currentNode)
00567 {
00568     /* 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.] */
00569     PARSER_STATE parseState;
00570     parseState.json = json;
00571     return ParseObjectOrArray(&parseState, currentNode);
00572 }
00573 
00574 JSON_DECODER_RESULT JSONDecoder_JSON_To_MultiTree(char* json, MULTITREE_HANDLE* multiTreeHandle)
00575 {
00576     JSON_DECODER_RESULT result;
00577 
00578     if ((json == NULL) ||
00579         (multiTreeHandle == NULL))
00580     {
00581         /* 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.] */
00582         result = JSON_DECODER_INVALID_ARG;
00583     }
00584     else if (*json == '\0')
00585     {
00586         /* 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.] */
00587         result = JSON_DECODER_PARSE_ERROR;
00588     }
00589     else
00590     {
00591         /* Codes_SRS_JSON_DECODER_99_008:[ JSONDecoder_JSON_To_MultiTree shall create a multi tree based on the json string argument.] */
00592         /* 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.] */
00593         /* 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.] */
00594         *multiTreeHandle = MultiTree_Create(NOPCloneFunction, NoFreeFunction);
00595         if (*multiTreeHandle == NULL)
00596         {
00597             /* Codes_SRS_JSON_DECODER_99_038:[ If any MultiTree API fails, JSONDecoder_JSON_To_MultiTree shall return JSON_DECODER_MULTITREE_FAILED.] */
00598             result = JSON_DECODER_MULTITREE_FAILED;
00599         }
00600         else
00601         {
00602             result = ParseJSON(json, *multiTreeHandle);
00603             if (result != JSON_DECODER_OK)
00604             {
00605                 MultiTree_Destroy(*multiTreeHandle);
00606             }
00607         }
00608     }
00609 
00610     return result;
00611 }