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
Add(const char * key,const std::list<std::string> & values) const159 bool PtJson::Add(const char *key, const std::list<std::string>& values) const
160 {
161 if (key == nullptr || Contains(key)) {
162 return false;
163 }
164
165 cJSON *node = cJSON_CreateArray();
166 if (node == nullptr) {
167 return false;
168 }
169
170 for (const auto &value : values) {
171 cJSON_AddItemToArray(node, cJSON_CreateString(value.c_str()));
172 }
173
174 cJSON_bool ret = cJSON_AddItemToObject(object_, key, node);
175 if (ret == 0) {
176 cJSON_Delete(node);
177 return false;
178 }
179
180 return true;
181 }
182
Push(bool value) const183 bool PtJson::Push(bool value) const
184 {
185 return Push(cJSON_CreateBool(value));
186 }
187
Push(int32_t value) const188 bool PtJson::Push(int32_t value) const
189 {
190 return Push(cJSON_CreateNumber(value));
191 }
192
Push(int64_t value) const193 bool PtJson::Push(int64_t value) const
194 {
195 return Push(cJSON_CreateNumber(value));
196 }
197
Push(uint32_t value) const198 bool PtJson::Push(uint32_t value) const
199 {
200 return Push(cJSON_CreateNumber(value));
201 }
202
Push(double value) const203 bool PtJson::Push(double value) const
204 {
205 return Push(cJSON_CreateNumber(value));
206 }
207
Push(const char * value) const208 bool PtJson::Push(const char *value) const
209 {
210 return Push(cJSON_CreateString(value));
211 }
212
Push(cJSON * node) const213 bool PtJson::Push(cJSON *node) const
214 {
215 if (node == nullptr) {
216 return false;
217 }
218
219 cJSON_bool ret = cJSON_AddItemToArray(object_, node);
220 if (ret == 0) {
221 cJSON_Delete(node);
222 return false;
223 }
224
225 return true;
226 }
227
Push(const std::unique_ptr<PtJson> & value) const228 bool PtJson::Push(const std::unique_ptr<PtJson> &value) const
229 {
230 if (value == nullptr) {
231 return false;
232 }
233
234 return Push(value->GetJson());
235 }
236
Remove(const char * key) const237 bool PtJson::Remove(const char *key) const
238 {
239 if (key == nullptr || !Contains(key)) {
240 return false;
241 }
242
243 cJSON_DeleteItemFromObject(object_, key);
244 return true;
245 }
246
Contains(const char * key) const247 bool PtJson::Contains(const char *key) const
248 {
249 cJSON *node = cJSON_GetObjectItemCaseSensitive(object_, key);
250 return node != nullptr;
251 }
252
GetKey() const253 std::string PtJson::GetKey() const
254 {
255 if (object_ == nullptr || object_->string == nullptr) {
256 return "";
257 }
258
259 return std::string(object_->string);
260 }
261
GetJson() const262 cJSON *PtJson::GetJson() const
263 {
264 return object_;
265 }
266
IsBool() const267 bool PtJson::IsBool() const
268 {
269 return cJSON_IsBool(object_) != 0;
270 }
271
IsNumber() const272 bool PtJson::IsNumber() const
273 {
274 return cJSON_IsNumber(object_) != 0;
275 }
276
IsString() const277 bool PtJson::IsString() const
278 {
279 return cJSON_IsString(object_) != 0;
280 }
281
IsObject() const282 bool PtJson::IsObject() const
283 {
284 return cJSON_IsObject(object_) != 0;
285 }
286
IsArray() const287 bool PtJson::IsArray() const
288 {
289 return cJSON_IsArray(object_) != 0;
290 }
291
IsNull() const292 bool PtJson::IsNull() const
293 {
294 return cJSON_IsNull(object_) != 0;
295 }
296
GetBool(bool defaultValue) const297 bool PtJson::GetBool(bool defaultValue) const
298 {
299 if (!IsBool()) {
300 return defaultValue;
301 }
302
303 return cJSON_IsTrue(object_) != 0;
304 }
305
GetInt(int32_t defaultValue) const306 int32_t PtJson::GetInt(int32_t defaultValue) const
307 {
308 if (!IsNumber()) {
309 return defaultValue;
310 }
311
312 return static_cast<int32_t>(object_->valuedouble);
313 }
314
GetInt64(int64_t defaultValue) const315 int64_t PtJson::GetInt64(int64_t defaultValue) const
316 {
317 if (!IsNumber()) {
318 return defaultValue;
319 }
320
321 return static_cast<int64_t>(object_->valuedouble);
322 }
323
GetUInt(uint32_t defaultValue) const324 uint32_t PtJson::GetUInt(uint32_t defaultValue) const
325 {
326 if (!IsNumber()) {
327 return defaultValue;
328 }
329
330 return static_cast<uint32_t>(object_->valuedouble);
331 }
332
GetUInt64(uint64_t defaultValue) const333 uint64_t PtJson::GetUInt64(uint64_t defaultValue) const
334 {
335 if (!IsNumber()) {
336 return defaultValue;
337 }
338
339 return static_cast<uint64_t>(object_->valuedouble);
340 }
341
GetDouble(double defaultValue) const342 double PtJson::GetDouble(double defaultValue) const
343 {
344 if (!IsNumber()) {
345 return defaultValue;
346 }
347
348 return object_->valuedouble;
349 }
350
GetString() const351 std::string PtJson::GetString() const
352 {
353 if (!IsString()) {
354 return "";
355 }
356
357 return std::string(object_->valuestring);
358 }
359
GetSize() const360 int32_t PtJson::GetSize() const
361 {
362 return cJSON_GetArraySize(object_);
363 }
364
Get(int32_t index) const365 std::unique_ptr<PtJson> PtJson::Get(int32_t index) const
366 {
367 return std::make_unique<PtJson>(cJSON_GetArrayItem(object_, index));
368 }
369
GetBool(const char * key,bool * value) const370 Result PtJson::GetBool(const char *key, bool *value) const
371 {
372 cJSON *item = cJSON_GetObjectItem(object_, key);
373 if (item == nullptr) {
374 return Result::NOT_EXIST;
375 }
376 if (cJSON_IsBool(item) == 0) {
377 return Result::TYPE_ERROR;
378 }
379
380 *value = cJSON_IsTrue(item) != 0;
381 return Result::SUCCESS;
382 }
383
SetBool(const char * key,const bool & value)384 Result PtJson::SetBool(const char *key, const bool& value)
385 {
386 cJSON *item = cJSON_GetObjectItem(object_, key);
387 if (item == nullptr) {
388 return Result::NOT_EXIST;
389 }
390 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateBool(value));
391 return Result::SUCCESS;
392 }
393
GetInt(const char * key,int32_t * value) const394 Result PtJson::GetInt(const char *key, int32_t *value) const
395 {
396 double result;
397 Result ret = GetDouble(key, &result);
398 if (ret == Result::SUCCESS) {
399 *value = static_cast<int32_t>(result);
400 }
401 return ret;
402 }
403
SetInt(const char * key,const int32_t & value)404 Result PtJson::SetInt(const char *key, const int32_t& value)
405 {
406 cJSON *item = cJSON_GetObjectItem(object_, key);
407 if (item == nullptr) {
408 return Result::NOT_EXIST;
409 }
410 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
411 return Result::SUCCESS;
412 }
413
GetInt64(const char * key,int64_t * value) const414 Result PtJson::GetInt64(const char *key, int64_t *value) const
415 {
416 double result;
417 Result ret = GetDouble(key, &result);
418 if (ret == Result::SUCCESS) {
419 *value = static_cast<int64_t>(result);
420 }
421 return ret;
422 }
423
SetInt64(const char * key,const int64_t & value)424 Result PtJson::SetInt64(const char *key, const int64_t& value)
425 {
426 cJSON *item = cJSON_GetObjectItem(object_, key);
427 if (item == nullptr) {
428 return Result::NOT_EXIST;
429 }
430 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
431 return Result::SUCCESS;
432 }
433
GetUInt(const char * key,uint32_t * value) const434 Result PtJson::GetUInt(const char *key, uint32_t *value) const
435 {
436 double result;
437 Result ret = GetDouble(key, &result);
438 if (ret == Result::SUCCESS) {
439 *value = static_cast<uint32_t>(result);
440 }
441 return ret;
442 }
443
SetUInt(const char * key,const uint32_t & value)444 Result PtJson::SetUInt(const char *key, const uint32_t& value)
445 {
446 cJSON *item = cJSON_GetObjectItem(object_, key);
447 if (item == nullptr) {
448 return Result::NOT_EXIST;
449 }
450 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
451 return Result::SUCCESS;
452 }
453
GetUInt64(const char * key,uint64_t * value) const454 Result PtJson::GetUInt64(const char *key, uint64_t *value) const
455 {
456 double result;
457 Result ret = GetDouble(key, &result);
458 if (ret == Result::SUCCESS) {
459 *value = static_cast<uint64_t>(result);
460 }
461 return ret;
462 }
463
SetUInt64(const char * key,const uint64_t & value)464 Result PtJson::SetUInt64(const char *key, const uint64_t& 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
GetDouble(const char * key,double * value) const474 Result PtJson::GetDouble(const char *key, double *value) const
475 {
476 cJSON *item = cJSON_GetObjectItem(object_, key);
477 if (item == nullptr) {
478 return Result::NOT_EXIST;
479 }
480 if (cJSON_IsNumber(item) == 0) {
481 return Result::TYPE_ERROR;
482 }
483
484 *value = item->valuedouble;
485 return Result::SUCCESS;
486 }
487
SetDouble(const char * key,const double & value)488 Result PtJson::SetDouble(const char *key, const double& value)
489 {
490 cJSON *item = cJSON_GetObjectItem(object_, key);
491 if (item == nullptr) {
492 return Result::NOT_EXIST;
493 }
494 cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value));
495 return Result::SUCCESS;
496 }
497
GetString(const char * key,std::string * value) const498 Result PtJson::GetString(const char *key, std::string *value) const
499 {
500 cJSON *item = cJSON_GetObjectItem(object_, key);
501 if (item == nullptr) {
502 return Result::NOT_EXIST;
503 }
504 if (cJSON_IsString(item) == 0) {
505 return Result::TYPE_ERROR;
506 }
507
508 *value = item->valuestring;
509 return Result::SUCCESS;
510 }
511
SetString(const char * key,const std::string & value)512 Result PtJson::SetString(const char *key, const std::string& value)
513 {
514 cJSON *item = cJSON_GetObjectItem(object_, key);
515 if (item == nullptr) {
516 return Result::NOT_EXIST;
517 }
518 if (cJSON_IsString(item) == 0) {
519 return Result::TYPE_ERROR;
520 }
521 cJSON_SetValuestring(item, value.c_str());
522 return Result::SUCCESS;
523 }
524
GetObject(const char * key,std::unique_ptr<PtJson> * value) const525 Result PtJson::GetObject(const char *key, std::unique_ptr<PtJson> *value) const
526 {
527 cJSON *item = cJSON_GetObjectItem(object_, key);
528 if (item == nullptr) {
529 return Result::NOT_EXIST;
530 }
531 if (cJSON_IsObject(item) == 0) {
532 return Result::TYPE_ERROR;
533 }
534
535 *value = std::make_unique<PtJson>(item);
536 return Result::SUCCESS;
537 }
538
GetArray(const char * key,std::unique_ptr<PtJson> * value) const539 Result PtJson::GetArray(const char *key, std::unique_ptr<PtJson> *value) const
540 {
541 cJSON *item = cJSON_GetObjectItem(object_, key);
542 if (item == nullptr) {
543 return Result::NOT_EXIST;
544 }
545 if (cJSON_IsArray(item) == 0) {
546 return Result::TYPE_ERROR;
547 }
548
549 *value = std::make_unique<PtJson>(item);
550 return Result::SUCCESS;
551 }
552
SetArray(const char * key,const std::list<std::string> & value)553 Result PtJson::SetArray(const char *key, const std::list<std::string>& value)
554 {
555 cJSON *item = cJSON_GetObjectItem(object_, key);
556 if (item == nullptr) {
557 return Result::NOT_EXIST;
558 }
559 if (cJSON_IsArray(item) == 0) {
560 return Result::TYPE_ERROR;
561 }
562 cJSON *newArray = cJSON_CreateArray();
563 if (newArray == nullptr) {
564 return Result::TYPE_ERROR;
565 }
566 for (const auto& str : value) {
567 cJSON *strItem = cJSON_CreateString(str.c_str());
568 if (strItem == nullptr) {
569 cJSON_Delete(newArray);
570 return Result::TYPE_ERROR;
571 }
572 cJSON_AddItemToArray(newArray, strItem);
573 }
574 cJSON_ReplaceItemInObject(object_, key, newArray);
575 return Result::SUCCESS;
576 }
577
GetAny(const char * key,std::unique_ptr<PtJson> * value) const578 Result PtJson::GetAny(const char *key, std::unique_ptr<PtJson> *value) const
579 {
580 cJSON *item = cJSON_GetObjectItem(object_, key);
581 if (item == nullptr) {
582 return Result::NOT_EXIST;
583 }
584
585 *value = std::make_unique<PtJson>(item);
586 return Result::SUCCESS;
587 }
588 } // namespace AppPackingTool
589 } // namespace OHOS
590