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
jsonencoder.c@11:b1327861f5e0, 2016-04-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Sun Apr 24 16:40:59 2016 -0700
- Revision:
- 11:b1327861f5e0
- Parent:
- 10:c2aee3965a83
- Child:
- 13:16e88f0cfa5f
1.0.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 0:1f9b2707ec7d | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 0:1f9b2707ec7d | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 0:1f9b2707ec7d | 3 | |
AzureIoTClient | 0:1f9b2707ec7d | 4 | #include <stdlib.h> |
AzureIoTClient | 0:1f9b2707ec7d | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 0:1f9b2707ec7d | 6 | #include <crtdbg.h> |
AzureIoTClient | 0:1f9b2707ec7d | 7 | #endif |
Azure.IoT Build | 10:c2aee3965a83 | 8 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 0:1f9b2707ec7d | 9 | |
AzureIoTClient | 0:1f9b2707ec7d | 10 | #include "jsonencoder.h" |
Azure.IoT Build | 10:c2aee3965a83 | 11 | #include "azure_c_shared_utility/crt_abstractions.h" |
Azure.IoT Build | 10:c2aee3965a83 | 12 | #include "azure_c_shared_utility/iot_logging.h" |
Azure.IoT Build | 10:c2aee3965a83 | 13 | #include "azure_c_shared_utility/strings.h" |
AzureIoTClient | 0:1f9b2707ec7d | 14 | |
AzureIoTClient | 0:1f9b2707ec7d | 15 | #ifdef _MSC_VER |
AzureIoTClient | 0:1f9b2707ec7d | 16 | #pragma warning(disable: 4701) /* potentially uninitialized local variable 'result' used */ /* the scanner cannot track variable "i" and link it to childCount*/ |
AzureIoTClient | 0:1f9b2707ec7d | 17 | #endif |
AzureIoTClient | 0:1f9b2707ec7d | 18 | |
AzureIoTClient | 0:1f9b2707ec7d | 19 | DEFINE_ENUM_STRINGS(JSON_ENCODER_TOSTRING_RESULT, JSON_ENCODER_TOSTRING_RESULT_VALUES); |
AzureIoTClient | 0:1f9b2707ec7d | 20 | DEFINE_ENUM_STRINGS(JSON_ENCODER_RESULT, JSON_ENCODER_RESULT_VALUES); |
AzureIoTClient | 0:1f9b2707ec7d | 21 | |
AzureIoTClient | 0:1f9b2707ec7d | 22 | JSON_ENCODER_RESULT JSONEncoder_EncodeTree(MULTITREE_HANDLE treeHandle, STRING_HANDLE destination, JSON_ENCODER_TOSTRING_FUNC toStringFunc) |
AzureIoTClient | 0:1f9b2707ec7d | 23 | { |
AzureIoTClient | 0:1f9b2707ec7d | 24 | JSON_ENCODER_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 25 | |
AzureIoTClient | 0:1f9b2707ec7d | 26 | size_t childCount; |
AzureIoTClient | 0:1f9b2707ec7d | 27 | |
AzureIoTClient | 0:1f9b2707ec7d | 28 | /* Codes_SRS_JSON_ENCODER_99_032:[If any of the arguments passed to JSONEncoder_EncodeTree is NULL then JSON_ENCODER_INVALID_ARG shall be returned.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 29 | if ((treeHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 30 | (destination == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 31 | (toStringFunc == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 32 | { |
AzureIoTClient | 0:1f9b2707ec7d | 33 | result = JSON_ENCODER_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 34 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 35 | } |
AzureIoTClient | 0:1f9b2707ec7d | 36 | /*Codes_SRS_JSON_ENCODER_99_035:[ JSON encoder shall inquire the number of child nodes (further called childCount) of the current node (given by parameter treeHandle.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 37 | else if (MultiTree_GetChildCount(treeHandle, &childCount) != MULTITREE_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 38 | { |
AzureIoTClient | 0:1f9b2707ec7d | 39 | result = JSON_ENCODER_MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 40 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 41 | } |
AzureIoTClient | 0:1f9b2707ec7d | 42 | else |
AzureIoTClient | 0:1f9b2707ec7d | 43 | { |
AzureIoTClient | 0:1f9b2707ec7d | 44 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 45 | /*Codes_SRS_JSON_ENCODER_99_036:[ The string "{" shall be added to the output;]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 46 | if (STRING_concat(destination, "{") != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 47 | { |
AzureIoTClient | 0:1f9b2707ec7d | 48 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 49 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 50 | } |
AzureIoTClient | 0:1f9b2707ec7d | 51 | else |
AzureIoTClient | 0:1f9b2707ec7d | 52 | { |
AzureIoTClient | 0:1f9b2707ec7d | 53 | result = JSON_ENCODER_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 54 | for (i = 0; (i < childCount) && (result == JSON_ENCODER_OK); i++) |
AzureIoTClient | 0:1f9b2707ec7d | 55 | { |
AzureIoTClient | 0:1f9b2707ec7d | 56 | MULTITREE_HANDLE childTreeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 57 | |
AzureIoTClient | 0:1f9b2707ec7d | 58 | if ((i > 0) && |
AzureIoTClient | 0:1f9b2707ec7d | 59 | (STRING_concat(destination, ", ") != 0)) |
AzureIoTClient | 0:1f9b2707ec7d | 60 | { |
AzureIoTClient | 0:1f9b2707ec7d | 61 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 62 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 63 | } |
AzureIoTClient | 0:1f9b2707ec7d | 64 | else if (STRING_concat(destination, "\"") != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 65 | { |
AzureIoTClient | 0:1f9b2707ec7d | 66 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 67 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 68 | } |
AzureIoTClient | 0:1f9b2707ec7d | 69 | else |
AzureIoTClient | 0:1f9b2707ec7d | 70 | { |
AzureIoTClient | 0:1f9b2707ec7d | 71 | STRING_HANDLE name = STRING_new(); |
AzureIoTClient | 0:1f9b2707ec7d | 72 | if (name == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 73 | { |
AzureIoTClient | 0:1f9b2707ec7d | 74 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 75 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 76 | } |
AzureIoTClient | 0:1f9b2707ec7d | 77 | else |
AzureIoTClient | 0:1f9b2707ec7d | 78 | { |
AzureIoTClient | 0:1f9b2707ec7d | 79 | if (MultiTree_GetChild(treeHandle, i, &childTreeHandle) != MULTITREE_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 80 | { |
AzureIoTClient | 0:1f9b2707ec7d | 81 | result = JSON_ENCODER_MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 82 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 83 | } |
AzureIoTClient | 0:1f9b2707ec7d | 84 | else if (MultiTree_GetName(childTreeHandle, name) != MULTITREE_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 85 | { |
AzureIoTClient | 0:1f9b2707ec7d | 86 | result = JSON_ENCODER_MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 87 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 88 | } |
AzureIoTClient | 0:1f9b2707ec7d | 89 | else if (STRING_concat_with_STRING(destination, name) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 90 | { |
AzureIoTClient | 0:1f9b2707ec7d | 91 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 92 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 93 | } |
AzureIoTClient | 0:1f9b2707ec7d | 94 | else if (STRING_concat(destination, "\":") != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 95 | { |
AzureIoTClient | 0:1f9b2707ec7d | 96 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 97 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 98 | } |
AzureIoTClient | 0:1f9b2707ec7d | 99 | else |
AzureIoTClient | 0:1f9b2707ec7d | 100 | { |
AzureIoTClient | 0:1f9b2707ec7d | 101 | size_t innerChildCount; |
AzureIoTClient | 0:1f9b2707ec7d | 102 | if (MultiTree_GetChildCount(childTreeHandle, &innerChildCount) != MULTITREE_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 103 | { |
AzureIoTClient | 0:1f9b2707ec7d | 104 | result = JSON_ENCODER_MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 105 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 106 | } |
AzureIoTClient | 0:1f9b2707ec7d | 107 | else |
AzureIoTClient | 0:1f9b2707ec7d | 108 | { |
AzureIoTClient | 0:1f9b2707ec7d | 109 | if (innerChildCount > 0) |
AzureIoTClient | 0:1f9b2707ec7d | 110 | { |
AzureIoTClient | 0:1f9b2707ec7d | 111 | STRING_HANDLE child = STRING_new(); |
AzureIoTClient | 0:1f9b2707ec7d | 112 | if (child == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 113 | { |
AzureIoTClient | 0:1f9b2707ec7d | 114 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 115 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 116 | } |
AzureIoTClient | 0:1f9b2707ec7d | 117 | else |
AzureIoTClient | 0:1f9b2707ec7d | 118 | { |
AzureIoTClient | 0:1f9b2707ec7d | 119 | if ((result = JSONEncoder_EncodeTree(childTreeHandle, child, toStringFunc)) != JSON_ENCODER_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 120 | { |
AzureIoTClient | 11:b1327861f5e0 | 121 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 122 | } |
AzureIoTClient | 0:1f9b2707ec7d | 123 | else if (STRING_concat_with_STRING(destination, child)!=0) |
AzureIoTClient | 0:1f9b2707ec7d | 124 | { |
AzureIoTClient | 0:1f9b2707ec7d | 125 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 126 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 127 | } |
AzureIoTClient | 0:1f9b2707ec7d | 128 | STRING_delete(child); |
AzureIoTClient | 0:1f9b2707ec7d | 129 | } |
AzureIoTClient | 0:1f9b2707ec7d | 130 | } |
AzureIoTClient | 0:1f9b2707ec7d | 131 | else |
AzureIoTClient | 0:1f9b2707ec7d | 132 | { |
AzureIoTClient | 0:1f9b2707ec7d | 133 | const void* value; |
AzureIoTClient | 0:1f9b2707ec7d | 134 | if (MultiTree_GetValue(childTreeHandle, &value) != MULTITREE_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 135 | { |
AzureIoTClient | 0:1f9b2707ec7d | 136 | result = JSON_ENCODER_MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 137 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 138 | |
AzureIoTClient | 0:1f9b2707ec7d | 139 | } |
AzureIoTClient | 0:1f9b2707ec7d | 140 | else |
AzureIoTClient | 0:1f9b2707ec7d | 141 | { |
AzureIoTClient | 0:1f9b2707ec7d | 142 | STRING_HANDLE childValue = STRING_new(); |
AzureIoTClient | 0:1f9b2707ec7d | 143 | if (childValue == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 144 | { |
AzureIoTClient | 0:1f9b2707ec7d | 145 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 146 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 147 | } |
AzureIoTClient | 0:1f9b2707ec7d | 148 | else |
AzureIoTClient | 0:1f9b2707ec7d | 149 | { |
AzureIoTClient | 0:1f9b2707ec7d | 150 | if (toStringFunc(childValue, value) != JSON_ENCODER_TOSTRING_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 151 | { |
AzureIoTClient | 0:1f9b2707ec7d | 152 | result = JSON_ENCODER_TOSTRING_FUNCTION_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 153 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 154 | } |
AzureIoTClient | 0:1f9b2707ec7d | 155 | else if (STRING_concat_with_STRING(destination, childValue)!=0) |
AzureIoTClient | 0:1f9b2707ec7d | 156 | { |
AzureIoTClient | 0:1f9b2707ec7d | 157 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 158 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 159 | } |
AzureIoTClient | 0:1f9b2707ec7d | 160 | else |
AzureIoTClient | 0:1f9b2707ec7d | 161 | { |
AzureIoTClient | 0:1f9b2707ec7d | 162 | /*do nothing, result = JSON_ENCODER_OK is set above at the beginning of the FOR loop*/ |
AzureIoTClient | 0:1f9b2707ec7d | 163 | } |
AzureIoTClient | 0:1f9b2707ec7d | 164 | STRING_delete(childValue); |
AzureIoTClient | 0:1f9b2707ec7d | 165 | } |
AzureIoTClient | 0:1f9b2707ec7d | 166 | } |
AzureIoTClient | 0:1f9b2707ec7d | 167 | } |
AzureIoTClient | 0:1f9b2707ec7d | 168 | } |
AzureIoTClient | 0:1f9b2707ec7d | 169 | } |
AzureIoTClient | 0:1f9b2707ec7d | 170 | STRING_delete(name); |
AzureIoTClient | 0:1f9b2707ec7d | 171 | } |
AzureIoTClient | 0:1f9b2707ec7d | 172 | } |
AzureIoTClient | 0:1f9b2707ec7d | 173 | } |
AzureIoTClient | 0:1f9b2707ec7d | 174 | |
AzureIoTClient | 0:1f9b2707ec7d | 175 | if ((i == childCount) && (result == JSON_ENCODER_OK)) |
AzureIoTClient | 0:1f9b2707ec7d | 176 | { |
AzureIoTClient | 0:1f9b2707ec7d | 177 | if (STRING_concat(destination, "}") != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 178 | { |
AzureIoTClient | 0:1f9b2707ec7d | 179 | result = JSON_ENCODER_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 180 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 181 | } |
AzureIoTClient | 0:1f9b2707ec7d | 182 | else |
AzureIoTClient | 0:1f9b2707ec7d | 183 | { |
AzureIoTClient | 0:1f9b2707ec7d | 184 | /* Codes_SRS_JSON_ENCODER_99_031:[On success, JSONEncoder_EncodeTree shall return JSON_ENCODER_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 185 | result = JSON_ENCODER_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 186 | } |
AzureIoTClient | 0:1f9b2707ec7d | 187 | } |
AzureIoTClient | 0:1f9b2707ec7d | 188 | } |
AzureIoTClient | 0:1f9b2707ec7d | 189 | } |
AzureIoTClient | 0:1f9b2707ec7d | 190 | |
AzureIoTClient | 0:1f9b2707ec7d | 191 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 192 | #ifdef _MSC_VER |
AzureIoTClient | 0:1f9b2707ec7d | 193 | #pragma warning(disable: 4701) /* potentially uninitialized local variable 'result' used */ /* the scanner cannot track variable "i" and link it to childCount*/ |
AzureIoTClient | 0:1f9b2707ec7d | 194 | #endif |
AzureIoTClient | 0:1f9b2707ec7d | 195 | } |
AzureIoTClient | 0:1f9b2707ec7d | 196 | |
AzureIoTClient | 0:1f9b2707ec7d | 197 | JSON_ENCODER_TOSTRING_RESULT JSONEncoder_CharPtr_ToString(STRING_HANDLE destination, const void* value) |
AzureIoTClient | 0:1f9b2707ec7d | 198 | { |
AzureIoTClient | 0:1f9b2707ec7d | 199 | JSON_ENCODER_TOSTRING_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 200 | |
AzureIoTClient | 0:1f9b2707ec7d | 201 | /*Coes_SRS_JSON_ENCODER_99_047:[ JSONEncoder_CharPtr_ToString shall return JSON_ENCODER_TOSTRING_INVALID_ARG if destination or value parameters passed to it are NULL.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 202 | if ((destination == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 203 | (value == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 204 | { |
AzureIoTClient | 0:1f9b2707ec7d | 205 | result = JSON_ENCODER_TOSTRING_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 206 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_TOSTRING_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 207 | } |
AzureIoTClient | 0:1f9b2707ec7d | 208 | /*Codes_SRS_JSON_ENCODER_99_048:[ JSONEncoder_CharPtr_ToString shall use strcpy_s to copy from value to destination.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 209 | else if (STRING_concat(destination, (const char*)value) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 210 | { |
AzureIoTClient | 0:1f9b2707ec7d | 211 | /*Codes_SRS_JSON_ENCODER_99_049:[ If strcpy_s fails then JSONEncoder_CharPtr_ToString shall return JSON_ENCODER_TOSTRING_ERROR.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 212 | result = JSON_ENCODER_TOSTRING_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 213 | LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_TOSTRING_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 214 | } |
AzureIoTClient | 0:1f9b2707ec7d | 215 | else |
AzureIoTClient | 0:1f9b2707ec7d | 216 | { |
AzureIoTClient | 0:1f9b2707ec7d | 217 | /*Codes_SRS_JSON_ENCODER_99_050:[ If strcpy_s doesn't fail, then JSONEncoder_CharPtr_ToString shall return JSON_ENCODER_TOSTRING_OK]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 218 | result = JSON_ENCODER_TOSTRING_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 219 | } |
AzureIoTClient | 0:1f9b2707ec7d | 220 | |
AzureIoTClient | 0:1f9b2707ec7d | 221 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 222 | } |