1 /*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "cJsonNapiH/cjsonnapi.h"
17
getAdditemtoarrInfoNum(napi_env env,napi_value itemObj)18 double getAdditemtoarrInfoNum(napi_env env, napi_value itemObj)
19 {
20 napi_status status;
21 const napi_extended_error_info *extended_error_info;
22 const char *tag = "[KH802_cJSON_AddItemToArray]";
23 napi_value paramValuedoubleIn;
24 status = napi_get_named_property(env, itemObj, "valuedouble", ¶mValuedoubleIn); // 读取属性值
25 if (status != napi_ok) {
26 getErrMessage(status, env, extended_error_info, "get named property", tag);
27 return NULL;
28 }
29
30 // 从 cJSON *item中取出值
31 double arrayItemValuedoubleIn = 0;
32 status = napi_get_value_double(env, paramValuedoubleIn, &arrayItemValuedoubleIn);
33 if (status != napi_ok) {
34 getErrMessage(status, env, extended_error_info, "napi_get_value_double", tag);
35 return NULL;
36 }
37
38 return arrayItemValuedoubleIn;
39 }
40
getAdditemtoarrInfoStr(napi_env env,napi_value itemObj)41 char *getAdditemtoarrInfoStr(napi_env env, napi_value itemObj)
42 {
43 napi_status status;
44 const napi_extended_error_info *extended_error_info;
45 const char *tag = "[KH802_cJSON_AddItemToArray]";
46 napi_value paramValuestringIn;
47 status = napi_get_named_property(env, itemObj, "valuestring", ¶mValuestringIn); // 读取属性值
48 if (status != napi_ok) {
49 getErrMessage(status, env, extended_error_info, "get named property", tag);
50 return NULL;
51 }
52 size_t strSize = 0;
53 /* [NAPI_GEN]: napi_get_value_string_utf8用于将Js字符串转换为UTF-8编码的C字符串
54 * env: N-API环境的句柄,表示当前的上下文
55 * value: 要转换的JavaScript字符串
56 * buf: 用于存储结果的字符数组的指针
57 * bufsize: 缓冲区大小,以字节为单位
58 * result: 转换后的字符串的字节长度(不包括空终止符)。若干buf是NULL,则返回所需的缓冲区大小(包括空终止符)
59 */
60 /* [NAPI_GEN]: buf参数是NULL时,用于获取所需缓冲区大小*/
61 status = napi_get_value_string_utf8(env, paramValuestringIn, NULL, 0, &strSize);
62 if (status != napi_ok) {
63 getErrMessage(status, env, extended_error_info, "get value string", tag);
64 return nullptr;
65 }
66 char *arrayItemValuestringIn = new char[strSize + 1];
67 /* [NAPI_GEN]: 用于获取字符串*/
68 status = napi_get_value_string_utf8(env, paramValuestringIn, arrayItemValuestringIn, strSize + 1, &strSize);
69 if (status != napi_ok) {
70 getErrMessage(status, env, extended_error_info, "get value string", tag);
71 delete[] arrayItemValuestringIn;
72 return nullptr;
73 }
74 return arrayItemValuestringIn;
75 }
76
addItemToArray(double arrayItemValuedoubleIn,char * arrayItemValuestringIn)77 cJSON_bool addItemToArray(double arrayItemValuedoubleIn, char *arrayItemValuestringIn)
78 {
79 // 创建一个空的 JSON 数组
80 cJSON *jsonArray = cJSON_CreateArray();
81 // 检查是否创建成功
82 if (jsonArray == NULL) {
83 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "cJSON_CreateArray failed.");
84 return NULL;
85 }
86 // 创建一个要添加到数组的 JSON 对象,例如一个数字,一个布尔值,一个字符串
87 cJSON *numberItem = cJSON_CreateNumber(arrayItemValuedoubleIn);
88 cJSON *booleanItem = cJSON_CreateTrue();
89 cJSON *stringItem = cJSON_CreateString(arrayItemValuestringIn);
90 // 检查是否创建成功
91 if (numberItem == NULL) {
92 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "cJSON_CreatNumber failed.");
93 return NULL;
94 }
95 if (booleanItem == NULL) {
96 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "cJSON_CreateTrue failed.");
97 return NULL;
98 }
99 if (stringItem == NULL) {
100 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "cJSON_CreateString failed.");
101 return NULL;
102 }
103 // 将创建的数字对象添加到数组中
104 cJSON_bool success = cJSON_AddItemToArray(jsonArray, numberItem);
105 if (!success) {
106 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray",
107 "cJSON add numberItem to array failed.");
108 return NULL;
109 }
110 success = cJSON_AddItemToArray(jsonArray, booleanItem);
111 if (!success) {
112 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray",
113 "cJSON add booleanItem to array failed.");
114 return NULL;
115 }
116 success = cJSON_AddItemToArray(jsonArray, stringItem);
117 if (!success) {
118 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray",
119 "cJSON add stringItem to array failed.");
120 return NULL;
121 }
122 // 打印数组
123 char *jsonResStr = cJSON_Print(jsonArray);
124 if (jsonResStr != NULL) {
125 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "jsonResStr: %s", jsonResStr);
126 }
127 // 最后,不要忘记删除 cJSON 对象以防内存泄露
128 cJSON_Delete(jsonArray);
129 return success;
130 }
131
getAdditemtoarrTypeOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonObj)132 napi_value getAdditemtoarrTypeOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonObj)
133 {
134 napi_status status;
135 const napi_extended_error_info *extended_error_info;
136 const char *tag = "[KH203_cJSON_CreateIntArray]";
137 napi_value typeOut;
138 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
139 * env: 当前环境的句柄
140 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
141 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
142 */
143 status = napi_create_int32(env, jsonObj == NULL ? 0 : jsonObj->type, &typeOut);
144 if (status != napi_ok) {
145 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
146 return nullptr;
147 }
148 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
149 * env: 当前环境的句柄
150 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
151 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
152 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
153 */
154 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "type", typeOut);
155 if (status != napi_ok) {
156 /* [NAPI_GEN]: 错误处理*/
157 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
158 return nullptr;
159 }
160 return cJSON_AddItemToArrayOut;
161 }
162
getAdditemtoarrValuesintOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonObj)163 napi_value getAdditemtoarrValuesintOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonObj)
164 {
165 napi_status status;
166 const napi_extended_error_info *extended_error_info;
167 const char *tag = "[KH203_cJSON_CreateIntArray]";
168 napi_value valueintOut;
169 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
170 * env: 当前环境的句柄
171 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
172 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
173 */
174 status = napi_create_int32(env, jsonObj == NULL ? 0 : jsonObj->valueint, &valueintOut);
175 if (status != napi_ok) {
176 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
177 return nullptr;
178 }
179 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
180 * env: 当前环境的句柄
181 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
182 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
183 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
184 */
185 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "valueint", valueintOut);
186 if (status != napi_ok) {
187 /* [NAPI_GEN]: 错误处理*/
188 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
189 return nullptr;
190 }
191 return cJSON_AddItemToArrayOut;
192 }
193
getAdditemtoarrValuesdoubleOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonObj)194 napi_value getAdditemtoarrValuesdoubleOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonObj)
195 {
196 napi_status status;
197 const napi_extended_error_info *extended_error_info;
198 const char *tag = "[KH203_cJSON_CreateIntArray]";
199 napi_value valuedoubleOut;
200 /* [NAPI_GEN]: 返回值是double类型时,napi_create_double 创建一个包含双精度浮点数的js数值(Number)对象
201 * env: 当前环境的句柄
202 * value: 要传递给js的双精度浮点数值,这里以传入1.0为例,用例新增业务代码时可根据自身需求修改
203 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
204 */
205 status = napi_create_double(env, jsonObj == NULL ? 0 : jsonObj->valuedouble, &valuedoubleOut);
206 if (status != napi_ok) {
207 getErrMessage(status, env, extended_error_info, "napi_create_double", tag);
208 return nullptr;
209 }
210 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
211 * env: 当前环境的句柄
212 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
213 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
214 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
215 */
216 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "valuedouble", valuedoubleOut);
217 if (status != napi_ok) {
218 /* [NAPI_GEN]: 错误处理*/
219 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
220 return nullptr;
221 }
222 return cJSON_AddItemToArrayOut;
223 }
224
getAdditemtoarrValuestringOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonOut)225 napi_value getAdditemtoarrValuestringOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonOut)
226 {
227 napi_status status;
228 const napi_extended_error_info *extended_error_info;
229 const char *tag = "[KH203_cJSON_CreateIntArray]";
230 napi_value valuestringOut;
231 /* [NAPI_GEN]:
232 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
233 * env: 当前环境的句柄
234 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以valuestring举例,用户可根据需求修改
235 * length:
236 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
237 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
238 */
239 status = napi_create_string_utf8(env, jsonOut->valuestring == nullptr ? "" : jsonOut->valuestring, NAPI_AUTO_LENGTH,
240 &valuestringOut);
241 if (status != napi_ok) {
242 /*错误处理*/
243 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
244 return nullptr;
245 }
246 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
247 * env: 当前环境的句柄
248 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
249 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
250 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
251 */
252 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "valuestring", valuestringOut);
253 if (status != napi_ok) {
254 /* [NAPI_GEN]: 错误处理*/
255 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
256 return nullptr;
257 }
258 return cJSON_AddItemToArrayOut;
259 }
260
getAdditemtoarrStringOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonOut)261 napi_value getAdditemtoarrStringOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonOut)
262 {
263 napi_status status;
264 const napi_extended_error_info *extended_error_info;
265 const char *tag = "[KH203_cJSON_CreateIntArray]";
266 napi_value stringOut;
267 /* [NAPI_GEN]:
268 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
269 * env: 当前环境的句柄
270 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以string举例,用户可根据需求修改
271 * length:
272 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
273 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
274 */
275 status =
276 napi_create_string_utf8(env, jsonOut->string == nullptr ? "" : jsonOut->string, NAPI_AUTO_LENGTH, &stringOut);
277 if (status != napi_ok) {
278 /*错误处理*/
279 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
280 return nullptr;
281 }
282 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
283 * env: 当前环境的句柄
284 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
285 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
286 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
287 */
288 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "string", stringOut);
289 if (status != napi_ok) {
290 /* [NAPI_GEN]: 错误处理*/
291 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
292 return nullptr;
293 }
294 return cJSON_AddItemToArrayOut;
295 }
296
getAdditemtoarrNextOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonObj)297 napi_value getAdditemtoarrNextOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonObj)
298 {
299 napi_status status;
300 const napi_extended_error_info *extended_error_info;
301 const char *tag = "[KH203_cJSON_CreateIntArray]";
302 napi_value nextOut;
303 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
304 * env: 当前环境的句柄
305 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
306 */
307 status = napi_create_object(env, &nextOut);
308 if (status != napi_ok) {
309 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
310 return nullptr;
311 }
312 // 给jsonObj->next赋值
313 if (jsonObj == NULL) {
314 return NULL;
315 }
316 if (jsonObj->next != NULL) {
317 nextOut = getAdditemtoarrTypeOut(env, nextOut, jsonObj->next);
318 nextOut = getAdditemtoarrValuesdoubleOut(env, nextOut, jsonObj->next);
319 nextOut = getAdditemtoarrValuesintOut(env, nextOut, jsonObj->next);
320 nextOut = getAdditemtoarrValuestringOut(env, nextOut, jsonObj->next);
321 nextOut = getAdditemtoarrStringOut(env, nextOut, jsonObj->next);
322 if (jsonObj->next->next != NULL) {
323 cJSON *nextNext = jsonObj->next->next;
324 if (nextNext != NULL) {
325 nextOut = getAdditemtoarrNextOut(env, nextOut, jsonObj->next);
326 }
327 }
328 if (jsonObj->next->child != NULL) {
329 cJSON *nextChild = jsonObj->next->child;
330 if (nextChild != NULL) {
331 nextOut = getAdditemtoarrChildOut(env, nextOut, jsonObj->next);
332 }
333 }
334 }
335 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
336 * env: 当前环境的句柄
337 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
338 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
339 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
340 */
341 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "next", nextOut);
342 if (status != napi_ok) {
343 /* [NAPI_GEN]: 错误处理*/
344 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
345 return nullptr;
346 }
347 return cJSON_AddItemToArrayOut;
348 }
349
getJsonarrayitemChildOut(napi_env env,napi_value childOut,cJSON * jsonObj)350 napi_value getJsonarrayitemChildOut(napi_env env, napi_value childOut, cJSON *jsonObj)
351 {
352 if (jsonObj->child != NULL) {
353 childOut = getAdditemtoarrTypeOut(env, childOut, jsonObj->child);
354 childOut = getAdditemtoarrValuesdoubleOut(env, childOut, jsonObj->child);
355 childOut = getAdditemtoarrValuesintOut(env, childOut, jsonObj->child);
356 childOut = getAdditemtoarrValuestringOut(env, childOut, jsonObj->child);
357 childOut = getAdditemtoarrStringOut(env, childOut, jsonObj->child);
358 if (jsonObj->child->child != NULL) {
359 cJSON *childChild = jsonObj->child->child;
360 if (childChild != NULL) {
361 childOut = getAdditemtoarrChildOut(env, childOut, jsonObj->child);
362 }
363 }
364 if (jsonObj->child->next != NULL) {
365 cJSON *childNext = jsonObj->child->next;
366 if (childNext != NULL) {
367 childOut = getAdditemtoarrNextOut(env, childOut, jsonObj->child);
368 }
369 }
370 }
371 return childOut;
372 }
373
374 /* 将C++ cJSON对象返回js层
375 * 输入:待返回的js对象,c++ cJSON对象
376 * 输出:返回js的对象
377 */
getAdditemtoarrChildOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonObj)378 napi_value getAdditemtoarrChildOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonObj)
379 {
380 napi_status status;
381 const napi_extended_error_info *extended_error_info;
382 const char *tag = "[KH203_cJSON_CreateIntArray]";
383 napi_value childOut;
384 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
385 * env: 当前环境的句柄
386 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
387 */
388 status = napi_create_object(env, &childOut);
389 if (status != napi_ok) {
390 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
391 return nullptr;
392 }
393 // 给jsonObj->child赋值
394 if (jsonObj != NULL) {
395 childOut = getJsonarrayitemChildOut(env, childOut, jsonObj);
396 }
397 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
398 * env: 当前环境的句柄
399 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
400 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
401 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
402 */
403 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "child", childOut);
404 if (status != napi_ok) {
405 /* [NAPI_GEN]: 错误处理*/
406 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
407 return nullptr;
408 }
409 return cJSON_AddItemToArrayOut;
410 }
411
getAdditemtoarrPrevOut(napi_env env,napi_value cJSON_AddItemToArrayOut,cJSON * jsonObj)412 napi_value getAdditemtoarrPrevOut(napi_env env, napi_value cJSON_AddItemToArrayOut, cJSON *jsonObj)
413 {
414 napi_status status;
415 const napi_extended_error_info *extended_error_info;
416 const char *tag = "[KH203_cJSON_CreateIntArray]";
417 napi_value prevOut;
418 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
419 * env: 当前环境的句柄
420 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
421 */
422 status = napi_create_object(env, &prevOut);
423 if (status != napi_ok) {
424 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
425 return nullptr;
426 }
427 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
428 * env: 当前环境的句柄
429 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
430 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
431 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
432 */
433 status = napi_set_named_property(env, cJSON_AddItemToArrayOut, "prev", prevOut);
434 if (status != napi_ok) {
435 /* [NAPI_GEN]: 错误处理*/
436 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
437 return nullptr;
438 }
439 return cJSON_AddItemToArrayOut;
440 }
441
getCjsonAddItemToArrRes(napi_env env,napi_value cjsonObj,napi_value cjsonArrobj)442 cJSON *getCjsonAddItemToArrRes(napi_env env, napi_value cjsonObj, napi_value cjsonArrobj)
443 {
444 const char *tag = "[KH802_cJSON_AddItemToArray]";
445 cJSON *jsonArray = cJSON_CreateArray();
446 // init传入的obj: 初始化数组
447 jsonArray = initCJSON_ArrayObj(env, cjsonObj, jsonArray, tag, true);
448 cJSON *jsonArrayItem = cJSON_CreateArray();
449 jsonArrayItem = initCJSON_Array(env, cjsonArrobj, jsonArrayItem, tag, false);
450 char *arrayString0 = cJSON_Print(jsonArrayItem);
451 bool jsonArrNull = false;
452 if (arrayString0 == NULL) {
453 cJSON_free(arrayString0); // 释放 cJSON_Print 分配的内存
454 return NULL;
455 } else {
456 std::string printRootStr = arrayString0;
457 RemoveNewlines(printRootStr);
458 if (printRootStr == "[]") {
459 jsonArrNull = true;
460 }
461 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "arrayString1: %s",
462 printRootStr.c_str());
463 cJSON_free(arrayString0); // 释放 cJSON_Print 分配的内存
464 }
465
466 cJSON *jsonObject = cJSON_CreateObject();
467 jsonObject = initCJSON_Object(env, cjsonArrobj, jsonObject, tag);
468 bool jsonObjectNull = false;
469 char *arrayString2 = cJSON_Print(jsonObject);
470 if (arrayString2 != NULL) {
471 std::string printRootStr = arrayString2;
472 RemoveNewlines(printRootStr);
473 if (printRootStr == "{}") {
474 jsonObjectNull = true;
475 }
476 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "KH802_cJSON_AddItemToArray", "arrayString2: %s",
477 printRootStr.c_str());
478 } else {
479 cJSON_free(arrayString2); // 释放 cJSON_Print 分配的内存
480 return NULL;
481 }
482
483 if (!jsonObjectNull) {
484 cJSON *objItem = cJSON_CreateObject();
485 cJSON_AddItemToArray(jsonArray, objItem);
486 objItem = initCJSON_Object(env, cjsonArrobj, objItem, tag);
487 } else if (!jsonArrNull) {
488 cJSON_AddItemToArray(jsonArray, jsonArrayItem);
489 }
490
491 return jsonArray;
492 }
493
494 /* [NAPI_GEN]:对应cJSON.h中: CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); 的napi方法
495 * 输入:一个cJSON数组对象,想要添加到cJSON数组中的cJSON元素
496 * 输出:添加item后的cJSON对象
497 */
KH802_cJSON_AddItemToArray(napi_env env,napi_callback_info info)498 napi_value KH802_cJSON_AddItemToArray(napi_env env, napi_callback_info info)
499 {
500 napi_status status;
501 /* [NAPI_GEN]: Node.js在其N-API中用来提供错误的扩展信息的结构体,结构体包含以下字段
502 * error_message: 一个指向错误详细字符串的指针,提供了关于错误的文本描述
503 * engin_reserved: 一个保留给Js引擎使用的指针
504 * error_code: 错误码,指示了错误的种类,比如napi_pending_exception表示有一个JavaScript异常未被清理。
505 * engine_error_code:一个引擎特定的错误码,为引擎实现保留,具体含义依赖于使用的JavaScript引擎。
506 * error_message_len:错误消息字符串的长度。
507 */
508 const napi_extended_error_info *extended_error_info;
509 /* [NAPI_GEN]: tag: 日志打印标签*/
510 const char *tag = "[KH802_cJSON_AddItemToArray]";
511 /* [NAPI_GEN]: get function param in*/
512 /* [NAPI_GEN]: argc:js传入的参数个数 */
513 size_t argc = PARAMS2;
514 /* [NAPI_GEN]: args: 一个数组,保存js传入的参数 */
515 napi_value args[PARAMS2] = {nullptr};
516 /* [NAPI_GEN]: napi_get_cb_info用于获取JS调用该函数时所传递的参数、接收参数的个数以及'this'的值
517 * env: 当前环境的句柄,代表当前的Node.js环境
518 * info: 回调信息句柄,代表当前回调的上下文
519 * argc: 指向size_t的指针,最初应包含可接受的最大参数数量,函数返回时,它将包含实际传递的参数数量
520 * args: 一个足够大的数组,用于接收传递给回调函数的所有js参数。数组的大小应至少与argc传入的值一样大。
521 * this_arg: 如果不是NULL,则返回js回调中this的值
522 * data: 如果不是NULL,则返回与回调函数关联的任何可选数据。通常用于传递在创建函数时指定的静态数据
523 */
524 status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
525 if (status != napi_ok) {
526 /* [NAPI_GEN]: 错误处理*/
527 getErrMessage(status, env, extended_error_info, "napi_get_cb_info", tag);
528 return nullptr;
529 }
530
531 // Todo: add business logic. 在这前后代码为框架所生成
532 cJSON *jsonArray = NULL;
533 jsonArray = getCjsonAddItemToArrRes(env, args[PARAMS0], args[PARAMS1]);
534
535 /* [NAPI_GEN]: function return value*/
536 napi_value cJSON_AddItemToArrayOut;
537 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
538 * env: 当前环境的句柄
539 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
540 */
541 status = napi_create_object(env, &cJSON_AddItemToArrayOut);
542 if (status != napi_ok) {
543 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
544 return nullptr;
545 }
546
547 cJSON_AddItemToArrayOut = getAdditemtoarrChildOut(env, cJSON_AddItemToArrayOut, jsonArray);
548 cJSON_AddItemToArrayOut = getAdditemtoarrNextOut(env, cJSON_AddItemToArrayOut, jsonArray);
549 cJSON_AddItemToArrayOut = getAdditemtoarrPrevOut(env, cJSON_AddItemToArrayOut, jsonArray);
550 cJSON_AddItemToArrayOut = getAdditemtoarrTypeOut(env, cJSON_AddItemToArrayOut, jsonArray);
551 cJSON_AddItemToArrayOut = getAdditemtoarrValuesdoubleOut(env, cJSON_AddItemToArrayOut, jsonArray);
552 cJSON_AddItemToArrayOut = getAdditemtoarrValuesintOut(env, cJSON_AddItemToArrayOut, jsonArray);
553 cJSON_AddItemToArrayOut = getAdditemtoarrValuestringOut(env, cJSON_AddItemToArrayOut, jsonArray);
554 cJSON_AddItemToArrayOut = getAdditemtoarrStringOut(env, cJSON_AddItemToArrayOut, jsonArray);
555
556 cJSON_Delete(jsonArray);
557 return cJSON_AddItemToArrayOut;
558 }
559