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