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