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 jsonencoder.c Source File

jsonencoder.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 
00006 #include "jsonencoder.h"
00007 #include "azure_c_shared_utility/crt_abstractions.h"
00008 #include "azure_c_shared_utility/xlogging.h"
00009 #include "azure_c_shared_utility/strings.h"
00010 
00011 #ifdef _MSC_VER
00012 #pragma warning(disable: 4701) /* potentially uninitialized local variable 'result' used */ /* the scanner cannot track variable "i" and link it to childCount*/
00013 #endif
00014 
00015 DEFINE_ENUM_STRINGS(JSON_ENCODER_TOSTRING_RESULT, JSON_ENCODER_TOSTRING_RESULT_VALUES);
00016 DEFINE_ENUM_STRINGS(JSON_ENCODER_RESULT, JSON_ENCODER_RESULT_VALUES);
00017 
00018 JSON_ENCODER_RESULT JSONEncoder_EncodeTree(MULTITREE_HANDLE treeHandle, STRING_HANDLE destination, JSON_ENCODER_TOSTRING_FUNC toStringFunc)
00019 {
00020     JSON_ENCODER_RESULT result;
00021 
00022     size_t childCount;
00023 
00024     /* 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.] */
00025     if ((treeHandle == NULL) ||
00026         (destination == NULL) ||
00027         (toStringFunc == NULL))
00028     {
00029         result = JSON_ENCODER_INVALID_ARG;
00030         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00031     }
00032     /*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.]*/
00033     else if (MultiTree_GetChildCount(treeHandle, &childCount) != MULTITREE_OK)
00034     {
00035         result = JSON_ENCODER_MULTITREE_ERROR;
00036         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00037     }
00038     else
00039     {
00040         size_t i;
00041         /*Codes_SRS_JSON_ENCODER_99_036:[ The string "{" shall be added to the output;]*/
00042         if (STRING_concat(destination,  "{") != 0)
00043         {
00044             result = JSON_ENCODER_ERROR;
00045             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00046         }
00047         else
00048         {
00049             result = JSON_ENCODER_OK;
00050             for (i = 0; (i < childCount) && (result == JSON_ENCODER_OK); i++)
00051             {
00052                 MULTITREE_HANDLE childTreeHandle;
00053 
00054                 if ((i > 0) &&
00055                     (STRING_concat(destination, ", ") != 0))
00056                 {
00057                     result = JSON_ENCODER_ERROR;
00058                     LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00059                 }
00060                 else if (STRING_concat(destination, "\"") != 0)
00061                 {
00062                     result = JSON_ENCODER_ERROR;
00063                     LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00064                 }
00065                 else
00066                 {
00067                     STRING_HANDLE name = STRING_new();
00068                     if (name == NULL)
00069                     {
00070                         result = JSON_ENCODER_ERROR;
00071                         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00072                     }
00073                     else
00074                     {
00075                         if (MultiTree_GetChild(treeHandle, i, &childTreeHandle) != MULTITREE_OK)
00076                         {
00077                             result = JSON_ENCODER_MULTITREE_ERROR;
00078                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00079                         }
00080                         else if (MultiTree_GetName(childTreeHandle, name) != MULTITREE_OK)
00081                         {
00082                             result = JSON_ENCODER_MULTITREE_ERROR;
00083                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00084                         }
00085                         else if (STRING_concat_with_STRING(destination, name) != 0)
00086                         {
00087                             result = JSON_ENCODER_ERROR;
00088                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00089                         }
00090                         else if (STRING_concat(destination, "\":") != 0)
00091                         {
00092                             result = JSON_ENCODER_ERROR;
00093                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00094                         }
00095                         else
00096                         {
00097                             size_t innerChildCount;
00098                             if (MultiTree_GetChildCount(childTreeHandle, &innerChildCount) != MULTITREE_OK)
00099                             {
00100                                 result = JSON_ENCODER_MULTITREE_ERROR;
00101                                 LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00102                             }
00103                             else
00104                             {
00105                                 if (innerChildCount > 0)
00106                                 {
00107                                     STRING_HANDLE child = STRING_new();
00108                                     if (child == NULL)
00109                                     {
00110                                         result = JSON_ENCODER_ERROR;
00111                                         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00112                                     }
00113                                     else
00114                                     {
00115                                         if ((result = JSONEncoder_EncodeTree(childTreeHandle, child, toStringFunc)) != JSON_ENCODER_OK)
00116                                         {
00117                                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00118                                         }
00119                                         else if (STRING_concat_with_STRING(destination, child)!=0)
00120                                         {
00121                                             result = JSON_ENCODER_ERROR;
00122                                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00123                                         }
00124                                         STRING_delete(child);
00125                                     }
00126                                 }
00127                                 else
00128                                 {
00129                                     const void* value;
00130                                     if (MultiTree_GetValue(childTreeHandle, &value) != MULTITREE_OK)
00131                                     {
00132                                         result = JSON_ENCODER_MULTITREE_ERROR;
00133                                         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00134 
00135                                     }
00136                                     else
00137                                     {
00138                                         STRING_HANDLE childValue = STRING_new();
00139                                         if (childValue == NULL)
00140                                         {
00141                                             result = JSON_ENCODER_ERROR;
00142                                             LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00143                                         }
00144                                         else
00145                                         {
00146                                             if (toStringFunc(childValue, value) != JSON_ENCODER_TOSTRING_OK)
00147                                             {
00148                                                 result = JSON_ENCODER_TOSTRING_FUNCTION_ERROR;
00149                                                 LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00150                                             }
00151                                             else if (STRING_concat_with_STRING(destination, childValue)!=0)
00152                                             {
00153                                                 result = JSON_ENCODER_ERROR;
00154                                                 LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00155                                             }
00156                                             else
00157                                             {
00158                                                 /*do nothing, result = JSON_ENCODER_OK is set above at the beginning of the FOR loop*/
00159                                             }
00160                                             STRING_delete(childValue);
00161                                         }
00162                                     }
00163                                 }
00164                             }
00165                         }
00166                         STRING_delete(name);
00167                     }
00168                 }
00169             }
00170 
00171             if ((i == childCount) && (result == JSON_ENCODER_OK))
00172             {
00173                 if (STRING_concat(destination,  "}") != 0)
00174                 {
00175                     result = JSON_ENCODER_ERROR;
00176                     LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_RESULT, result));
00177                 }
00178                 else
00179                 {
00180                     /* Codes_SRS_JSON_ENCODER_99_031:[On success, JSONEncoder_EncodeTree shall return JSON_ENCODER_OK.] */
00181                     result = JSON_ENCODER_OK;
00182                 }
00183             }
00184         }
00185     }
00186 
00187     return result;
00188 #ifdef _MSC_VER
00189 #pragma warning(disable: 4701) /* potentially uninitialized local variable 'result' used */ /* the scanner cannot track variable "i" and link it to childCount*/
00190 #endif
00191 }
00192 
00193 JSON_ENCODER_TOSTRING_RESULT JSONEncoder_CharPtr_ToString(STRING_HANDLE destination, const void* value)
00194 {
00195     JSON_ENCODER_TOSTRING_RESULT result;
00196 
00197     /*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.]*/
00198     if ((destination == NULL) ||
00199         (value == NULL))
00200     {
00201         result = JSON_ENCODER_TOSTRING_INVALID_ARG;
00202         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_TOSTRING_RESULT, result));
00203     }
00204     /*Codes_SRS_JSON_ENCODER_99_048:[ JSONEncoder_CharPtr_ToString shall use strcpy_s to copy from value to destination.]*/
00205     else if (STRING_concat(destination, (const char*)value) != 0)
00206     {
00207         /*Codes_SRS_JSON_ENCODER_99_049:[ If strcpy_s fails then JSONEncoder_CharPtr_ToString shall return JSON_ENCODER_TOSTRING_ERROR.]*/
00208         result = JSON_ENCODER_TOSTRING_ERROR;
00209         LogError("(result = %s)", ENUM_TO_STRING(JSON_ENCODER_TOSTRING_RESULT, result));
00210     }
00211     else
00212     {
00213         /*Codes_SRS_JSON_ENCODER_99_050:[ If strcpy_s doesn't fail, then JSONEncoder_CharPtr_ToString shall return JSON_ENCODER_TOSTRING_OK]*/
00214         result = JSON_ENCODER_TOSTRING_OK;
00215     }
00216 
00217     return result;
00218 }