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
getAddstrtoobjTypeOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonObj)18 napi_value getAddstrtoobjTypeOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonObj)
19 {
20 napi_status status;
21 const napi_extended_error_info *extended_error_info;
22 const char *tag = "[KH526_cJSON_AddStringToObject]";
23 napi_value typeOut;
24 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
25 * env: 当前环境的句柄
26 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
27 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
28 */
29 status = napi_create_int32(env, jsonObj == NULL ? 0 : jsonObj->type, &typeOut);
30 if (status != napi_ok) {
31 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
32 return nullptr;
33 }
34 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
35 * env: 当前环境的句柄
36 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
37 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
38 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
39 */
40 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "type", typeOut);
41 if (status != napi_ok) {
42 /* [NAPI_GEN]: 错误处理*/
43 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
44 return nullptr;
45 }
46 return cJSON_AddStringToObjectOut;
47 }
48
getAddstrtoobjValuesintOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonObj)49 napi_value getAddstrtoobjValuesintOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonObj)
50 {
51 napi_status status;
52 const napi_extended_error_info *extended_error_info;
53 const char *tag = "[KH526_cJSON_AddStringToObject]";
54 napi_value valueintOut;
55 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
56 * env: 当前环境的句柄
57 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
58 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
59 */
60 status = napi_create_int32(env, jsonObj == NULL ? 0 : jsonObj->valueint, &valueintOut);
61 if (status != napi_ok) {
62 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
63 return nullptr;
64 }
65 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
66 * env: 当前环境的句柄
67 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
68 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
69 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
70 */
71 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "valueint", valueintOut);
72 if (status != napi_ok) {
73 /* [NAPI_GEN]: 错误处理*/
74 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
75 return nullptr;
76 }
77 return cJSON_AddStringToObjectOut;
78 }
79
getAddstrtoobjValuesdoubleOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonObj)80 napi_value getAddstrtoobjValuesdoubleOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonObj)
81 {
82 napi_status status;
83 const napi_extended_error_info *extended_error_info;
84 const char *tag = "[KH526_cJSON_AddStringToObject]";
85 napi_value valuedoubleOut;
86 /* [NAPI_GEN]: 返回值是double类型时,napi_create_double 创建一个包含双精度浮点数的js数值(Number)对象
87 * env: 当前环境的句柄
88 * value: 要传递给js的双精度浮点数值,这里以传入1.0为例,用例新增业务代码时可根据自身需求修改
89 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
90 */
91 status = napi_create_double(env, jsonObj == NULL ? 0 : jsonObj->valuedouble, &valuedoubleOut);
92 if (status != napi_ok) {
93 getErrMessage(status, env, extended_error_info, "napi_create_double", tag);
94 return nullptr;
95 }
96 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
97 * env: 当前环境的句柄
98 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
99 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
100 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
101 */
102 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "valuedouble", valuedoubleOut);
103 if (status != napi_ok) {
104 /* [NAPI_GEN]: 错误处理*/
105 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
106 return nullptr;
107 }
108 return cJSON_AddStringToObjectOut;
109 }
110
getAddstrtoobjValuestringOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonOut)111 napi_value getAddstrtoobjValuestringOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonOut)
112 {
113 napi_status status;
114 const napi_extended_error_info *extended_error_info;
115 const char *tag = "[KH526_cJSON_AddStringToObject]";
116 napi_value valuestringOut;
117 /* [NAPI_GEN]:
118 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
119 * env: 当前环境的句柄
120 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以valuestring举例,用户可根据需求修改
121 * length:
122 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
123 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
124 */
125 status = napi_create_string_utf8(env, jsonOut->valuestring == nullptr ? "" : jsonOut->valuestring, NAPI_AUTO_LENGTH,
126 &valuestringOut);
127 if (status != napi_ok) {
128 /*错误处理*/
129 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
130 return nullptr;
131 }
132 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
133 * env: 当前环境的句柄
134 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
135 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
136 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
137 */
138 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "valuestring", valuestringOut);
139 if (status != napi_ok) {
140 /* [NAPI_GEN]: 错误处理*/
141 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
142 return nullptr;
143 }
144 return cJSON_AddStringToObjectOut;
145 }
146
getAddstrtoobjStringOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonOut)147 napi_value getAddstrtoobjStringOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonOut)
148 {
149 napi_status status;
150 const napi_extended_error_info *extended_error_info;
151 const char *tag = "[KH526_cJSON_AddStringToObject]";
152 napi_value stringOut;
153 /* [NAPI_GEN]:
154 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
155 * env: 当前环境的句柄
156 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以string举例,用户可根据需求修改
157 * length:
158 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
159 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
160 */
161 status =
162 napi_create_string_utf8(env, jsonOut->string == nullptr ? "" : jsonOut->string, NAPI_AUTO_LENGTH, &stringOut);
163 if (status != napi_ok) {
164 /*错误处理*/
165 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
166 return nullptr;
167 }
168 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
169 * env: 当前环境的句柄
170 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
171 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
172 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
173 */
174 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "string", stringOut);
175 if (status != napi_ok) {
176 /* [NAPI_GEN]: 错误处理*/
177 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
178 return nullptr;
179 }
180 return cJSON_AddStringToObjectOut;
181 }
182
getAddstrtoobjNextOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonObj)183 napi_value getAddstrtoobjNextOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonObj)
184 {
185 napi_status status;
186 const napi_extended_error_info *extended_error_info;
187 const char *tag = "[KH526_cJSON_AddStringToObject]";
188 napi_value nextOut;
189 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
190 * env: 当前环境的句柄
191 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
192 */
193 status = napi_create_object(env, &nextOut);
194 if (status != napi_ok) {
195 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
196 return nullptr;
197 }
198 // 给jsonObj->next赋值
199 if (jsonObj == NULL) {
200 return NULL;
201 }
202 if (jsonObj->next != NULL) {
203 nextOut = getAddstrtoobjTypeOut(env, nextOut, jsonObj->next);
204 nextOut = getAddstrtoobjValuesdoubleOut(env, nextOut, jsonObj->next);
205 nextOut = getAddstrtoobjValuesintOut(env, nextOut, jsonObj->next);
206 nextOut = getAddstrtoobjValuestringOut(env, nextOut, jsonObj->next);
207 nextOut = getAddstrtoobjStringOut(env, nextOut, jsonObj->next);
208 if (jsonObj->next->next != NULL) {
209 cJSON *nextNext = jsonObj->next->next;
210 if (nextNext != NULL) {
211 nextOut = getAddstrtoobjNextOut(env, nextOut, jsonObj->next);
212 }
213 }
214 }
215 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
216 * env: 当前环境的句柄
217 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
218 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
219 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
220 */
221 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "next", nextOut);
222 if (status != napi_ok) {
223 /* [NAPI_GEN]: 错误处理*/
224 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
225 return nullptr;
226 }
227 return cJSON_AddStringToObjectOut;
228 }
229
getJsonobjstrChildOut(napi_env env,napi_value childOut,cJSON * jsonObj)230 napi_value getJsonobjstrChildOut(napi_env env, napi_value childOut, cJSON *jsonObj)
231 {
232 if (jsonObj->child != NULL) {
233 childOut = getAddstrtoobjTypeOut(env, childOut, jsonObj->child);
234 childOut = getAddstrtoobjValuesdoubleOut(env, childOut, jsonObj->child);
235 childOut = getAddstrtoobjValuesintOut(env, childOut, jsonObj->child);
236 childOut = getAddstrtoobjValuestringOut(env, childOut, jsonObj->child);
237 childOut = getAddstrtoobjStringOut(env, childOut, jsonObj->child);
238 if (jsonObj->child->next != NULL) {
239 cJSON *childNext = jsonObj->child->next;
240 if (childNext != NULL) {
241 childOut = getAddstrtoobjNextOut(env, childOut, jsonObj->child);
242 }
243 }
244 }
245 return childOut;
246 }
247
getAddstrtoobjChildOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonObj)248 napi_value getAddstrtoobjChildOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonObj)
249 {
250 napi_status status;
251 const napi_extended_error_info *extended_error_info;
252 const char *tag = "[KH526_cJSON_AddStringToObject]";
253 napi_value childOut;
254 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
255 * env: 当前环境的句柄
256 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
257 */
258 status = napi_create_object(env, &childOut);
259 if (status != napi_ok) {
260 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
261 return nullptr;
262 }
263 // 给jsonObj->child赋值
264 if (jsonObj != NULL) {
265 childOut = getJsonobjstrChildOut(env, childOut, jsonObj);
266 }
267 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
268 * env: 当前环境的句柄
269 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
270 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
271 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
272 */
273 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "child", childOut);
274 if (status != napi_ok) {
275 /* [NAPI_GEN]: 错误处理*/
276 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
277 return nullptr;
278 }
279 return cJSON_AddStringToObjectOut;
280 }
281
getAddstrtoobjPrevOut(napi_env env,napi_value cJSON_AddStringToObjectOut,cJSON * jsonObj)282 napi_value getAddstrtoobjPrevOut(napi_env env, napi_value cJSON_AddStringToObjectOut, cJSON *jsonObj)
283 {
284 napi_status status;
285 const napi_extended_error_info *extended_error_info;
286 const char *tag = "[KH526_cJSON_AddStringToObject]";
287 napi_value prevOut;
288 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
289 * env: 当前环境的句柄
290 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
291 */
292 status = napi_create_object(env, &prevOut);
293 if (status != napi_ok) {
294 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
295 return nullptr;
296 }
297 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
298 * env: 当前环境的句柄
299 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
300 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
301 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
302 */
303 status = napi_set_named_property(env, cJSON_AddStringToObjectOut, "prev", prevOut);
304 if (status != napi_ok) {
305 /* [NAPI_GEN]: 错误处理*/
306 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
307 return nullptr;
308 }
309 return cJSON_AddStringToObjectOut;
310 }
311
getAddstrtoobjInfoNameIn(napi_env env,napi_value propNameIn)312 char *getAddstrtoobjInfoNameIn(napi_env env, napi_value propNameIn)
313 {
314 napi_status status;
315 const napi_extended_error_info *extended_error_info;
316 const char *tag = "[KH206_cJSON_AddNumberToObject]";
317 napi_valuetype valuetypename;
318 /* [NAPI_GEN]: 获取入参类型,第1个入参
319 * env: N-API环境的句柄,表示当前的上下文
320 * value: 要检查类型的js值
321 * result: 是一个指针,指向napi_valuetype枚举的值,函数会将结果存储在这里
322 */
323 status = napi_typeof(env, propNameIn, &valuetypename);
324 if (status != napi_ok) {
325 getErrMessage(status, env, extended_error_info, "napi_typeof", tag);
326 return nullptr;
327 }
328 size_t strSize1 = 0;
329 /* [NAPI_GEN]: napi_get_value_string_utf8用于将Js字符串转换为UTF-8编码的C字符串
330 * env: N-API环境的句柄,表示当前的上下文
331 * value: 要转换的JavaScript字符串
332 * buf: 用于存储结果的字符数组的指针
333 * bufsize: 缓冲区大小,以字节为单位
334 * result: 转换后的字符串的字节长度(不包括空终止符)。若干buf是NULL,则返回所需的缓冲区大小(包括空终止符)
335 */
336 /* [NAPI_GEN]: buf参数是NULL时,用于获取所需缓冲区大小*/
337 status = napi_get_value_string_utf8(env, propNameIn, NULL, 0, &strSize1);
338 if (status != napi_ok) {
339 getErrMessage(status, env, extended_error_info, "get value string", tag);
340 return nullptr;
341 }
342 char *nameIn = new char[strSize1 + 1];
343 /* [NAPI_GEN]: 用于获取字符串*/
344 status = napi_get_value_string_utf8(env, propNameIn, nameIn, strSize1 + 1, &strSize1);
345 if (status != napi_ok) {
346 getErrMessage(status, env, extended_error_info, "get value string", tag);
347 delete[] nameIn;
348 return nullptr;
349 }
350 return nameIn;
351 }
352
getAddstrtoobjInfoValueIn(napi_env env,napi_value propValueIn)353 char *getAddstrtoobjInfoValueIn(napi_env env, napi_value propValueIn)
354 {
355 napi_status status;
356 const napi_extended_error_info *extended_error_info;
357 const char *tag = "[KH206_cJSON_AddNumberToObject]";
358 napi_valuetype valuetypestring;
359 /* [NAPI_GEN]: 获取入参类型,第2个入参
360 * env: N-API环境的句柄,表示当前的上下文
361 * value: 要检查类型的js值
362 * result: 是一个指针,指向napi_valuetype枚举的值,函数会将结果存储在这里
363 */
364 status = napi_typeof(env, propValueIn, &valuetypestring);
365 if (status != napi_ok) {
366 getErrMessage(status, env, extended_error_info, "napi_typeof", tag);
367 return nullptr;
368 }
369 size_t strSize2 = 0;
370 /* [NAPI_GEN]: napi_get_value_string_utf8用于将Js字符串转换为UTF-8编码的C字符串
371 * env: N-API环境的句柄,表示当前的上下文
372 * value: 要转换的JavaScript字符串
373 * buf: 用于存储结果的字符数组的指针
374 * bufsize: 缓冲区大小,以字节为单位
375 * result: 转换后的字符串的字节长度(不包括空终止符)。若干buf是NULL,则返回所需的缓冲区大小(包括空终止符)
376 */
377 /* [NAPI_GEN]: buf参数是NULL时,用于获取所需缓冲区大小*/
378 status = napi_get_value_string_utf8(env, propValueIn, NULL, 0, &strSize2);
379 if (status != napi_ok) {
380 getErrMessage(status, env, extended_error_info, "get value string", tag);
381 return nullptr;
382 }
383 char *stringIn = new char[strSize2 + 1];
384 /* [NAPI_GEN]: 用于获取字符串*/
385 status = napi_get_value_string_utf8(env, propValueIn, stringIn, strSize2 + 1, &strSize2);
386 if (status != napi_ok) {
387 getErrMessage(status, env, extended_error_info, "get value string", tag);
388 delete[] stringIn;
389 return nullptr;
390 }
391 return stringIn;
392 }
393
394 /* [NAPI_GEN]:对应cJSON.h中: CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name,
395 * const char * const string);的napi方法,
396 * 输入:一个cJSON对象,需要添加的item名字(字符串),需要添加的item内容(字符串)
397 * 输出:添加item后的cJSON对象
398 */
KH526_cJSON_AddStringToObject(napi_env env,napi_callback_info info)399 napi_value KH526_cJSON_AddStringToObject(napi_env env, napi_callback_info info)
400 {
401 napi_status status;
402 /* [NAPI_GEN]: Node.js在其N-API中用来提供错误的扩展信息的结构体,结构体包含以下字段
403 * error_message: 一个指向错误详细字符串的指针,提供了关于错误的文本描述
404 * engin_reserved: 一个保留给Js引擎使用的指针
405 * error_code: 错误码,指示了错误的种类,比如napi_pending_exception表示有一个JavaScript异常未被清理。
406 * engine_error_code:一个引擎特定的错误码,为引擎实现保留,具体含义依赖于使用的JavaScript引擎。
407 * error_message_len:错误消息字符串的长度。
408 */
409 const napi_extended_error_info *extended_error_info;
410 /* [NAPI_GEN]: tag: 日志打印标签*/
411 const char *tag = "[KH526_cJSON_AddStringToObject]";
412 /* [NAPI_GEN]: get function param in*/
413 /* [NAPI_GEN]: argc:js传入的参数个数 */
414 size_t argc = PARAMS3;
415 /* [NAPI_GEN]: args: 一个数组,保存js传入的参数 */
416 napi_value args[PARAMS3] = {nullptr};
417 /* [NAPI_GEN]: napi_get_cb_info用于获取JS调用该函数时所传递的参数、接收参数的个数以及'this'的值
418 * env: 当前环境的句柄,代表当前的Node.js环境
419 * info: 回调信息句柄,代表当前回调的上下文
420 * argc: 指向size_t的指针,最初应包含可接受的最大参数数量,函数返回时,它将包含实际传递的参数数量
421 * args: 一个足够大的数组,用于接收传递给回调函数的所有js参数。数组的大小应至少与argc传入的值一样大。
422 * this_arg: 如果不是NULL,则返回js回调中this的值
423 * data: 如果不是NULL,则返回与回调函数关联的任何可选数据。通常用于传递在创建函数时指定的静态数据
424 */
425 status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
426 if (status != napi_ok) {
427 /* [NAPI_GEN]: 错误处理*/
428 getErrMessage(status, env, extended_error_info, "napi_get_cb_info", tag);
429 return nullptr;
430 }
431 /* [NAPI_GEN]: 从args数组中获取入参 */
432 napi_value propNameIn = args[PARAMS1];
433 char *nameIn = getAddstrtoobjInfoNameIn(env, propNameIn);
434 napi_value propValueIn = args[PARAMS2];
435 char *stringIn = getAddstrtoobjInfoValueIn(env, propValueIn);
436
437 // Todo: add business logic. 在这前后代码为框架所生成
438 cJSON *jsonObj = cJSON_CreateObject();
439 cJSON *jsonOut = nullptr;
440
441 // init 传入的obj 初始化
442 napi_value cjsonObj = args[PARAMS0];
443 jsonObj = initCJSON_Object(env, cjsonObj, jsonObj, tag);
444 // add string to jsonObj
445 jsonOut = cJSON_AddStringToObject(jsonObj, nameIn, stringIn);
446
447 char *jsonResStr = cJSON_Print(jsonObj);
448 if (jsonResStr != NULL) {
449 std::string testaaa = jsonResStr;
450 RemoveNewlines(testaaa);
451 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "KH206_cJSON_AddNumberToObject", "jsonResStr: %s",
452 testaaa.c_str());
453 }
454
455 delete[] nameIn;
456 delete[] stringIn;
457 cJSON_Delete(jsonObj);
458 /* [NAPI_GEN]: function return value*/
459 napi_value cJSON_AddStringToObjectOut;
460 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
461 * env: 当前环境的句柄
462 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
463 */
464 status = napi_create_object(env, &cJSON_AddStringToObjectOut);
465 if (status != napi_ok) {
466 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
467 return nullptr;
468 }
469
470 cJSON_AddStringToObjectOut = getAddstrtoobjChildOut(env, cJSON_AddStringToObjectOut, jsonObj);
471 cJSON_AddStringToObjectOut = getAddstrtoobjNextOut(env, cJSON_AddStringToObjectOut, jsonObj);
472 cJSON_AddStringToObjectOut = getAddstrtoobjPrevOut(env, cJSON_AddStringToObjectOut, jsonObj);
473 cJSON_AddStringToObjectOut = getAddstrtoobjTypeOut(env, cJSON_AddStringToObjectOut, jsonObj);
474 cJSON_AddStringToObjectOut = getAddstrtoobjValuesdoubleOut(env, cJSON_AddStringToObjectOut, jsonObj);
475 cJSON_AddStringToObjectOut = getAddstrtoobjValuesintOut(env, cJSON_AddStringToObjectOut, jsonObj);
476 cJSON_AddStringToObjectOut = getAddstrtoobjValuestringOut(env, cJSON_AddStringToObjectOut, jsonObj);
477 cJSON_AddStringToObjectOut = getAddstrtoobjStringOut(env, cJSON_AddStringToObjectOut, jsonObj);
478
479 return cJSON_AddStringToObjectOut;
480 }
481