• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 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 "cjsonsample.h"
17 #include "nodeapi.h"
18 #include "cJsonNapiH/cjsonnapi.h"
19 
cJsonCaseInit(napi_env env,napi_value exports)20 napi_value cJsonCaseInit(napi_env env, napi_value exports)
21 {
22     napi_property_descriptor desc[] = {
23         {"KH418_cJSON_Parse", nullptr, KH418_CJSON_Parse, nullptr, nullptr, nullptr, napi_default, nullptr},
24         {"KH373_cJSON_GetArraySize", nullptr, KH373_cJSON_GetArraySize, nullptr, nullptr, nullptr, napi_default,
25          nullptr},
26         {"KH735_cJSON_Print", nullptr, KH735_cJSON_Print, nullptr, nullptr, nullptr, napi_default, nullptr},
27         {"KH361_cJSON_CreateObject", nullptr, KH361_cJSON_CreateObject, nullptr, nullptr, nullptr, napi_default,
28          nullptr},
29         {"KH515_cJSON_CreateString", nullptr, KH515_cJSON_CreateString, nullptr, nullptr, nullptr, napi_default,
30          nullptr},
31         {"KH526_cJSON_AddStringToObject", nullptr, KH526_cJSON_AddStringToObject, nullptr, nullptr, nullptr,
32          napi_default, nullptr},
33         {"KH206_cJSON_AddNumberToObject", nullptr, KH206_cJSON_AddNumberToObject, nullptr, nullptr, nullptr,
34          napi_default, nullptr},
35         {"KH545_cJSON_AddFalseToObject", nullptr, KH545_cJSON_AddFalseToObject, nullptr, nullptr, nullptr, napi_default,
36          nullptr},
37         {"KH180_cJSON_AddItemToObject", nullptr, KH180_cJSON_AddItemToObject, nullptr, nullptr, nullptr, napi_default,
38          nullptr},
39         {"KH386_cJSON_CreateArray", nullptr, KH386_cJSON_CreateArray, nullptr, nullptr, nullptr, napi_default, nullptr},
40         {"KH203_cJSON_CreateIntArray", nullptr, KH203_cJSON_CreateIntArray, nullptr, nullptr, nullptr, napi_default,
41          nullptr},
42         {"KH802_cJSON_AddItemToArray", nullptr, KH802_cJSON_AddItemToArray, nullptr, nullptr, nullptr, napi_default,
43          nullptr},
44     };
45     napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc);
46     return exports;
47 }
48 
createTctCJsonInstance(napi_env env)49 napi_value createTctCJsonInstance(napi_env env)
50 {
51     napi_status status;
52     napi_value instance;
53 
54     OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, "basesample", "Add in");
55 
56     status = napi_create_object(env, &instance);
57     if (status != napi_ok) {
58         napi_throw_error(env, nullptr, "Failed to create object");
59         return nullptr;
60     }
61 
62     napi_value pname;
63     status = napi_create_string_utf8(env, "tct_cjson", strlen("tct_cjson"), &pname);
64     if (status != napi_ok) {
65         napi_throw_error(env, nullptr, "Invalid argument: name must be a string");
66         return nullptr;
67     }
68 
69     status = napi_set_named_property(env, instance, "name", pname);
70     if (status != napi_ok) {
71         napi_throw_error(env, nullptr, "Failed to set property");
72         return nullptr;
73     }
74 
75     cJsonCaseInit(env, instance);
76     napi_property_descriptor properties[] = {
77         {"cjson_version", nullptr, cJSONVersion, nullptr, nullptr, nullptr, napi_default, nullptr},
78     };
79 
80     status = napi_define_properties(env, instance, sizeof(properties) / sizeof(napi_property_descriptor), properties);
81     if (status != napi_ok) {
82         napi_throw_error(env, nullptr, "Failed to define properties");
83         return nullptr;
84     }
85     return instance;
86 }
87