1 /*
2 * Copyright (c) 2024 Huawei Device 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 "pt_json.h"
17
18 #include "log.h"
19
20 namespace OHOS {
21 namespace AppPackingTool {
CreateObject()22 std::unique_ptr<PtJson> PtJson::CreateObject()
23 {
24 return std::make_unique<PtJson>(cJSON_CreateObject());
25 }
26
CreateArray()27 std::unique_ptr<PtJson> PtJson::CreateArray()
28 {
29 return std::make_unique<PtJson>(cJSON_CreateArray());
30 }
31
ReleaseRoot()32 void PtJson::ReleaseRoot()
33 {
34 if (object_ != nullptr) {
35 cJSON_Delete(object_);
36 object_ = nullptr;
37 }
38 }
39
Parse(const std::string & data)40 std::unique_ptr<PtJson> PtJson::Parse(const std::string &data)
41 {
42 cJSON *value = cJSON_ParseWithOpts(data.c_str(), nullptr, true);
43 if (value == nullptr) {
44 return nullptr;
45 }
46 return std::make_unique<PtJson>(value);
47 }
48
Stringify() const49 std::string PtJson::Stringify() const
50 {
51 if (object_ == nullptr) {
52 return "";
53 }
54
55 char *str = cJSON_PrintUnformatted(object_);
56 if (str == nullptr) {
57 return "";
58 }
59
60 std::string result(str);
61 cJSON_free(str);
62 return result;
63 }
64
Add(const char * key,bool value) const65 bool PtJson::Add(const char *key, bool value) const
66 {
67 if (key == nullptr || Contains(key)) {
68 return false;
69 }
70
71 cJSON *node = cJSON_CreateBool(value);
72 if (node == nullptr) {
73 return false;
74 }
75
76 cJSON_bool ret = cJSON_AddItemToObject(object_, key, node);
77 if (ret == 0) {
78 cJSON_Delete(node);
79 return false;
80 }
81
82 return true;
83 }
84
Add(const char * key,int32_t value) const85 bool PtJson::Add(const char *key, int32_t value) const
86 {
87 return Add(key, static_cast<double>(value));
88 }
89
Add(const char * key,int64_t value) const90 bool PtJson::Add(const char *key, int64_t value) const
91 {
92 return Add(key, static_cast<double>(value));
93 }
94
Add(const char * key,uint32_t value) const95 bool PtJson::Add(const char *key, uint32_t value) const
96 {
97 return Add(key, static_cast<double>(value));
98 }
99
Add(const char * key,double value) const100 bool PtJson::Add(const char *key, double value) const
101 {
102 if (key == nullptr || Contains(key)) {
103 return false;
104 }
105
106 cJSON *node = cJSON_CreateNumber(value);
107 if (node == nullptr) {
108 return false;
109 }
110
111 cJSON_bool ret = cJSON_AddItemToObject(object_, key, node);
112 if (ret == 0) {
113 cJSON_Delete(node);
114 return false;
115 }
116
117 return true;
118 }
119
Add(const char * key,const char * value) const120 bool PtJson::Add(const char *key, const char *value) const
121 {
122 if (key == nullptr || Contains(key)) {
123 return false;
124 }
125
126 cJSON *node = cJSON_CreateString(value);
127 if (node == nullptr) {
128 return false;
129 }
130
131 cJSON_bool ret = cJSON_AddItemToObject(object_, key, node);
132 if (ret == 0) {
133 cJSON_Delete(node);
134 return false;
135 }
136
137 return true;
138 }
139
Add(const char * key,const std::unique_ptr<PtJson> & value) const140 bool PtJson::Add(const char *key, const std::unique_ptr<PtJson> &value) const
141 {
142 if (key == nullptr || Contains(key)) {
143 return false;
144 }
145
146 cJSON *node = value->GetJson();
147 if (node == nullptr) {
148 return false;
149 }
150
151 cJSON_bool ret = cJSON_AddItemToObject(object_, key, node);
152 if (ret == 0) {
153 return false;
154 }
155
156 return true;
157 }
158
Push(bool value) const159 bool PtJson::Push(bool value) const
160 {
161 return Push(cJSON_CreateBool(value));
162 }
163
Push(int32_t value) const164 bool PtJson::Push(int32_t value) const
165 {
166 return Push(cJSON_CreateNumber(value));
167 }
168
Push(int64_t value) const169 bool PtJson::Push(int64_t value) const
170 {
171 return Push(cJSON_CreateNumber(value));
172 }
173
Push(uint32_t value) const174 bool PtJson::Push(uint32_t value) const
175 {
176 return Push(cJSON_CreateNumber(value));
177 }
178
Push(double value) const179 bool PtJson::Push(double value) const
180 {
181 return Push(cJSON_CreateNumber(value));
182 }
183
Push(const char * value) const184 bool PtJson::Push(const char *value) const
185 {
186 return Push(cJSON_CreateString(value));
187 }
188
Push(cJSON * node) const189 bool PtJson::Push(cJSON *node) const
190 {
191 if (node == nullptr) {
192 return false;
193 }
194
195 cJSON_bool ret = cJSON_AddItemToArray(object_, node);
196 if (ret == 0) {
197 cJSON_Delete(node);
198 return false;
199 }
200
201 return true;
202 }
203
Push(const std::unique_ptr<PtJson> & value) const204 bool PtJson::Push(const std::unique_ptr<PtJson> &value) const
205 {
206 if (value == nullptr) {
207 return false;
208 }
209
210 return Push(value->GetJson());
211 }
212
Remove(const char * key) const213 bool PtJson::Remove(const char *key) const
214 {
215 if (key == nullptr || !Contains(key)) {
216 return false;
217 }
218
219 cJSON_DeleteItemFromObject(object_, key);
220 return true;
221 }
222
Contains(const char * key) const223 bool PtJson::Contains(const char *key) const
224 {
225 cJSON *node = cJSON_GetObjectItemCaseSensitive(object_, key);
226 return node != nullptr;
227 }
228
GetKey() const229 std::string PtJson::GetKey() const
230 {
231 if (object_ == nullptr || object_->string == nullptr) {
232 return "";
233 }
234
235 return std::string(object_->string);
236 }
237
GetJson() const238 cJSON *PtJson::GetJson() const
239 {
240 return object_;
241 }
242
IsBool() const243 bool PtJson::IsBool() const
244 {
245 return cJSON_IsBool(object_) != 0;
246 }
247
IsNumber() const248 bool PtJson::IsNumber() const
249 {
250 return cJSON_IsNumber(object_) != 0;
251 }
252
IsString() const253 bool PtJson::IsString() const
254 {
255 return cJSON_IsString(object_) != 0;
256 }
257
IsObject() const258 bool PtJson::IsObject() const
259 {
260 return cJSON_IsObject(object_) != 0;
261 }
262
IsArray() const263 bool PtJson::IsArray() const
264 {
265 return cJSON_IsArray(object_) != 0;
266 }
267
IsNull() const268 bool PtJson::IsNull() const
269 {
270 return cJSON_IsNull(object_) != 0;
271 }
272
GetBool(bool defaultValue) const273 bool PtJson::GetBool(bool defaultValue) const
274 {
275 if (!IsBool()) {
276 return defaultValue;
277 }
278
279 return cJSON_IsTrue(object_) != 0;
280 }
281
GetInt(int32_t defaultValue) const282 int32_t PtJson::GetInt(int32_t defaultValue) const
283 {
284 if (!IsNumber()) {
285 return defaultValue;
286 }
287
288 return static_cast<int32_t>(object_->valuedouble);
289 }
290
GetInt64(int64_t defaultValue) const291 int64_t PtJson::GetInt64(int64_t defaultValue) const
292 {
293 if (!IsNumber()) {
294 return defaultValue;
295 }
296
297 return static_cast<int64_t>(object_->valuedouble);
298 }
299
GetUInt(uint32_t defaultValue) const300 uint32_t PtJson::GetUInt(uint32_t defaultValue) const
301 {
302 if (!IsNumber()) {
303 return defaultValue;
304 }
305
306 return static_cast<uint32_t>(object_->valuedouble);
307 }
308
GetUInt64(uint64_t defaultValue) const309 uint64_t PtJson::GetUInt64(uint64_t defaultValue) const
310 {
311 if (!IsNumber()) {
312 return defaultValue;
313 }
314
315 return static_cast<uint64_t>(object_->valuedouble);
316 }
317
GetDouble(double defaultValue) const318 double PtJson::GetDouble(double defaultValue) const
319 {
320 if (!IsNumber()) {
321 return defaultValue;
322 }
323
324 return object_->valuedouble;
325 }
326
GetString() const327 std::string PtJson::GetString() const
328 {
329 if (!IsString()) {
330 return "";
331 }
332
333 return std::string(object_->valuestring);
334 }
335
GetSize() const336 int32_t PtJson::GetSize() const
337 {
338 return cJSON_GetArraySize(object_);
339 }
340
Get(int32_t index) const341 std::unique_ptr<PtJson> PtJson::Get(int32_t index) const
342 {
343 return std::make_unique<PtJson>(cJSON_GetArrayItem(object_, index));
344 }
345
GetBool(const char * key,bool * value) const346 Result PtJson::GetBool(const char *key, bool *value) const
347 {
348 cJSON *item = cJSON_GetObjectItem(object_, key);
349 if (item == nullptr) {
350 return Result::NOT_EXIST;
351 }
352 if (cJSON_IsBool(item) == 0) {
353 return Result::TYPE_ERROR;
354 }
355
356 *value = cJSON_IsTrue(item) != 0;
357 return Result::SUCCESS;
358 }
359
SetBool(const char * key,const bool & value)360 Result PtJson::SetBool(const char *key, const bool& value)
361 {
362 cJSON *item = cJSON_GetObjectItem(object_, key);
363 if (item == nullptr) {
364 return Result::NOT_EXIST;
365 }
366 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateBool(value));
367 return Result::SUCCESS;
368 }
369
GetInt(const char * key,int32_t * value) const370 Result PtJson::GetInt(const char *key, int32_t *value) const
371 {
372 double result;
373 Result ret = GetDouble(key, &result);
374 if (ret == Result::SUCCESS) {
375 *value = static_cast<int32_t>(result);
376 }
377 return ret;
378 }
379
SetInt(const char * key,const int32_t & value)380 Result PtJson::SetInt(const char *key, const int32_t& value)
381 {
382 cJSON *item = cJSON_GetObjectItem(object_, key);
383 if (item == nullptr) {
384 return Result::NOT_EXIST;
385 }
386 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
387 return Result::SUCCESS;
388 }
389
GetInt64(const char * key,int64_t * value) const390 Result PtJson::GetInt64(const char *key, int64_t *value) const
391 {
392 double result;
393 Result ret = GetDouble(key, &result);
394 if (ret == Result::SUCCESS) {
395 *value = static_cast<int64_t>(result);
396 }
397 return ret;
398 }
399
SetInt64(const char * key,const int64_t & value)400 Result PtJson::SetInt64(const char *key, const int64_t& value)
401 {
402 cJSON *item = cJSON_GetObjectItem(object_, key);
403 if (item == nullptr) {
404 return Result::NOT_EXIST;
405 }
406 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
407 return Result::SUCCESS;
408 }
409
GetUInt(const char * key,uint32_t * value) const410 Result PtJson::GetUInt(const char *key, uint32_t *value) const
411 {
412 double result;
413 Result ret = GetDouble(key, &result);
414 if (ret == Result::SUCCESS) {
415 *value = static_cast<uint32_t>(result);
416 }
417 return ret;
418 }
419
SetUInt(const char * key,const uint32_t & value)420 Result PtJson::SetUInt(const char *key, const uint32_t& value)
421 {
422 cJSON *item = cJSON_GetObjectItem(object_, key);
423 if (item == nullptr) {
424 return Result::NOT_EXIST;
425 }
426 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
427 return Result::SUCCESS;
428 }
429
GetUInt64(const char * key,uint64_t * value) const430 Result PtJson::GetUInt64(const char *key, uint64_t *value) const
431 {
432 double result;
433 Result ret = GetDouble(key, &result);
434 if (ret == Result::SUCCESS) {
435 *value = static_cast<uint64_t>(result);
436 }
437 return ret;
438 }
439
SetUInt64(const char * key,const uint64_t & value)440 Result PtJson::SetUInt64(const char *key, const uint64_t& value)
441 {
442 cJSON *item = cJSON_GetObjectItem(object_, key);
443 if (item == nullptr) {
444 return Result::NOT_EXIST;
445 }
446 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
447 return Result::SUCCESS;
448 }
449
GetDouble(const char * key,double * value) const450 Result PtJson::GetDouble(const char *key, double *value) const
451 {
452 cJSON *item = cJSON_GetObjectItem(object_, key);
453 if (item == nullptr) {
454 return Result::NOT_EXIST;
455 }
456 if (cJSON_IsNumber(item) == 0) {
457 return Result::TYPE_ERROR;
458 }
459
460 *value = item->valuedouble;
461 return Result::SUCCESS;
462 }
463
SetDouble(const char * key,const double & value)464 Result PtJson::SetDouble(const char *key, const double& value)
465 {
466 cJSON *item = cJSON_GetObjectItem(object_, key);
467 if (item == nullptr) {
468 return Result::NOT_EXIST;
469 }
470 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
471 return Result::SUCCESS;
472 }
473
GetString(const char * key,std::string * value) const474 Result PtJson::GetString(const char *key, std::string *value) const
475 {
476 cJSON *item = cJSON_GetObjectItem(object_, key);
477 if (item == nullptr) {
478 return Result::NOT_EXIST;
479 }
480 if (cJSON_IsString(item) == 0) {
481 return Result::TYPE_ERROR;
482 }
483
484 *value = item->valuestring;
485 return Result::SUCCESS;
486 }
487
SetString(const char * key,const std::string & value)488 Result PtJson::SetString(const char *key, const std::string& value)
489 {
490 cJSON *item = cJSON_GetObjectItem(object_, key);
491 if (item == nullptr) {
492 return Result::NOT_EXIST;
493 }
494 if (cJSON_IsString(item) == 0) {
495 return Result::TYPE_ERROR;
496 }
497 cJSON_SetValuestring(item, value.c_str());
498 return Result::SUCCESS;
499 }
500
GetObject(const char * key,std::unique_ptr<PtJson> * value) const501 Result PtJson::GetObject(const char *key, std::unique_ptr<PtJson> *value) const
502 {
503 cJSON *item = cJSON_GetObjectItem(object_, key);
504 if (item == nullptr) {
505 return Result::NOT_EXIST;
506 }
507 if (cJSON_IsObject(item) == 0) {
508 return Result::TYPE_ERROR;
509 }
510
511 *value = std::make_unique<PtJson>(item);
512 return Result::SUCCESS;
513 }
514
GetArray(const char * key,std::unique_ptr<PtJson> * value) const515 Result PtJson::GetArray(const char *key, std::unique_ptr<PtJson> *value) const
516 {
517 cJSON *item = cJSON_GetObjectItem(object_, key);
518 if (item == nullptr) {
519 return Result::NOT_EXIST;
520 }
521 if (cJSON_IsArray(item) == 0) {
522 return Result::TYPE_ERROR;
523 }
524
525 *value = std::make_unique<PtJson>(item);
526 return Result::SUCCESS;
527 }
528
GetAny(const char * key,std::unique_ptr<PtJson> * value) const529 Result PtJson::GetAny(const char *key, std::unique_ptr<PtJson> *value) const
530 {
531 cJSON *item = cJSON_GetObjectItem(object_, key);
532 if (item == nullptr) {
533 return Result::NOT_EXIST;
534 }
535
536 *value = std::make_unique<PtJson>(item);
537 return Result::SUCCESS;
538 }
539 } // namespace AppPackingTool
540 } // namespace OHOS
541