source: azure_iot_hub_f767zi/trunk/azure_iot_sdk/serializer/src/jsonencoder.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

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