• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
getCjsonPrintRes(napi_env env,napi_value obj)18 char *getCjsonPrintRes(napi_env env, napi_value obj)
19 {
20     const char *tag = "[KH735_cJSON_Print]";
21     // 创建一个JSON对象
22     cJSON *jsonObject = cJSON_CreateObject();
23     jsonObject = initCJSON_Object(env, obj, jsonObject, tag);
24     char *genResString1 = cJSON_Print(jsonObject);
25     char *resultStr = genResString1;
26     bool isNullObj = false;
27     if (genResString1 != NULL) {
28         std::string genResStringPrint = genResString1;
29         RemoveNewlines(genResStringPrint);
30         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "KH735_cJSON_Print", "genResString1: %s",
31             genResStringPrint.c_str());
32         if (genResStringPrint == "{}") {
33             isNullObj = true;
34         }
35     }
36 
37     bool isArrNull = false;
38     cJSON *jsonArray = cJSON_CreateArray();
39     jsonArray = initCJSON_Array(env, obj, jsonArray, tag, false);
40     char *genResString2 = cJSON_Print(jsonArray);
41     if (genResString2 != NULL) {
42         std::string genResStringPrint = genResString2;
43         RemoveNewlines(genResStringPrint);
44         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "KH735_cJSON_Print", "genResString2: %s",
45             genResStringPrint.c_str());
46         if (genResStringPrint == "[]") {
47             isArrNull = true;
48         }
49     }
50 
51     bool isArrObjNull = false;
52     cJSON *jsonArrayObj = cJSON_CreateArray();
53     jsonArrayObj = initCJSON_ArrayObj(env, obj, jsonArrayObj, tag, true);
54     char *genResString3 = cJSON_Print(jsonArrayObj);
55     if (genResString3 != NULL) {
56         std::string genResStringPrint = genResString3;
57         RemoveNewlines(genResStringPrint);
58         if (genResStringPrint == "[]") {
59             isArrObjNull = true;
60         }
61     }
62     bool isArrayObject = isArrObject(env, obj, tag);
63     if (!isArrObjNull && isArrayObject) {
64         resultStr = genResString3;
65     } else if (!isArrNull) {
66         resultStr = genResString2;
67     }
68     // 清理cJSON对象
69     cJSON_Delete(jsonObject);
70     return resultStr;
71 }
72 
73 /* [NAPI_GEN]:对应cJSON.h中: CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);的napi方法,
74  * 输入一个cJSON对象
75  * 输出该对象序列化之后的字符串
76  */
KH735_cJSON_Print(napi_env env,napi_callback_info info)77 napi_value KH735_cJSON_Print(napi_env env, napi_callback_info info)
78 {
79     napi_status status;
80     /* [NAPI_GEN]: Node.js在其N-API中用来提供错误的扩展信息的结构体,结构体包含以下字段
81      * error_message: 一个指向错误详细字符串的指针,提供了关于错误的文本描述
82      * engin_reserved: 一个保留给Js引擎使用的指针
83      * error_code: 错误码,指示了错误的种类,比如napi_pending_exception表示有一个JavaScript异常未被清理。
84      * engine_error_code:一个引擎特定的错误码,为引擎实现保留,具体含义依赖于使用的JavaScript引擎。
85      * error_message_len:错误消息字符串的长度。
86      */
87     const napi_extended_error_info *extended_error_info;
88     /* [NAPI_GEN]: tag: 日志打印标签*/
89     const char *tag = "[KH735_cJSON_Print]";
90     /* [NAPI_GEN]: get function param in*/
91     /* [NAPI_GEN]: argc:js传入的参数个数 */
92     size_t argc = PARAMS1;
93     /* [NAPI_GEN]: args: 一个数组,保存js传入的参数 */
94     napi_value args[PARAMS1] = {nullptr};
95     /* [NAPI_GEN]: napi_get_cb_info用于获取JS调用该函数时所传递的参数、接收参数的个数以及'this'的值
96      * env: 当前环境的句柄,代表当前的Node.js环境
97      * info: 回调信息句柄,代表当前回调的上下文
98      * argc: 指向size_t的指针,最初应包含可接受的最大参数数量,函数返回时,它将包含实际传递的参数数量
99      * args: 一个足够大的数组,用于接收传递给回调函数的所有js参数。数组的大小应至少与argc传入的值一样大。
100      * this_arg: 如果不是NULL,则返回js回调中this的值
101      * data: 如果不是NULL,则返回与回调函数关联的任何可选数据。通常用于传递在创建函数时指定的静态数据
102      */
103     status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
104     if (status != napi_ok) {
105         /* [NAPI_GEN]: 错误处理*/
106         getErrMessage(status, env, extended_error_info, "napi_get_cb_info", tag);
107         return NULL;
108     }
109 
110     // Todo: add business logic. 在这之前代码为框架所生成
111     char *resultStr = getCjsonPrintRes(env, args[PARAMS0]);
112 
113     /* [NAPI_GEN]: function return value*/
114     napi_value cJSON_PrintOut;
115     /* [NAPI_GEN]:
116      * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
117      * env: 当前环境的句柄
118      * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以cJSON_Print举例,用户可根据需求修改
119      * length:
120      * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
121      * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
122      */
123     status = napi_create_string_utf8(env, resultStr, NAPI_AUTO_LENGTH, &cJSON_PrintOut);
124     if (status != napi_ok) {
125         /*错误处理*/
126         getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
127         return NULL;
128     }
129 
130     return cJSON_PrintOut;
131 }
132