123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "base/json/json_util.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "cJSON.h"
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_cinamespace OHOS::Ace {
2123b3eb3cSopenharmony_ci
2223b3eb3cSopenharmony_ciJsonValue::JsonValue(JsonObject* object) : object_(object) {}
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_ciJsonValue::JsonValue(JsonObject* object, bool isRoot) : object_(object), isRoot_(isRoot) {}
2523b3eb3cSopenharmony_ci
2623b3eb3cSopenharmony_ciJsonValue::~JsonValue()
2723b3eb3cSopenharmony_ci{
2823b3eb3cSopenharmony_ci    if (object_ != nullptr && isRoot_) {
2923b3eb3cSopenharmony_ci        cJSON_Delete(object_);
3023b3eb3cSopenharmony_ci    }
3123b3eb3cSopenharmony_ci    object_ = nullptr;
3223b3eb3cSopenharmony_ci}
3323b3eb3cSopenharmony_ci
3423b3eb3cSopenharmony_cibool JsonValue::IsBool() const
3523b3eb3cSopenharmony_ci{
3623b3eb3cSopenharmony_ci    return cJSON_IsBool(object_);
3723b3eb3cSopenharmony_ci}
3823b3eb3cSopenharmony_ci
3923b3eb3cSopenharmony_cibool JsonValue::IsNumber() const
4023b3eb3cSopenharmony_ci{
4123b3eb3cSopenharmony_ci    return cJSON_IsNumber(object_);
4223b3eb3cSopenharmony_ci}
4323b3eb3cSopenharmony_ci
4423b3eb3cSopenharmony_cibool JsonValue::IsString() const
4523b3eb3cSopenharmony_ci{
4623b3eb3cSopenharmony_ci    return cJSON_IsString(object_);
4723b3eb3cSopenharmony_ci}
4823b3eb3cSopenharmony_ci
4923b3eb3cSopenharmony_cibool JsonValue::IsArray() const
5023b3eb3cSopenharmony_ci{
5123b3eb3cSopenharmony_ci    return cJSON_IsArray(object_);
5223b3eb3cSopenharmony_ci}
5323b3eb3cSopenharmony_ci
5423b3eb3cSopenharmony_cibool JsonValue::IsObject() const
5523b3eb3cSopenharmony_ci{
5623b3eb3cSopenharmony_ci    return cJSON_IsObject(object_);
5723b3eb3cSopenharmony_ci}
5823b3eb3cSopenharmony_ci
5923b3eb3cSopenharmony_cibool JsonValue::IsValid() const
6023b3eb3cSopenharmony_ci{
6123b3eb3cSopenharmony_ci    return (object_ != nullptr) && !cJSON_IsInvalid(object_);
6223b3eb3cSopenharmony_ci}
6323b3eb3cSopenharmony_ci
6423b3eb3cSopenharmony_cibool JsonValue::IsNull() const
6523b3eb3cSopenharmony_ci{
6623b3eb3cSopenharmony_ci    return (object_ == nullptr) || cJSON_IsNull(object_);
6723b3eb3cSopenharmony_ci}
6823b3eb3cSopenharmony_ci
6923b3eb3cSopenharmony_cibool JsonValue::Contains(const std::string& key) const
7023b3eb3cSopenharmony_ci{
7123b3eb3cSopenharmony_ci    return cJSON_HasObjectItem(object_, key.c_str());
7223b3eb3cSopenharmony_ci}
7323b3eb3cSopenharmony_ci
7423b3eb3cSopenharmony_cibool JsonValue::GetBool() const
7523b3eb3cSopenharmony_ci{
7623b3eb3cSopenharmony_ci    return cJSON_IsTrue(object_) != 0;
7723b3eb3cSopenharmony_ci}
7823b3eb3cSopenharmony_ci
7923b3eb3cSopenharmony_cibool JsonValue::GetBool(const std::string& key, bool defaultValue) const
8023b3eb3cSopenharmony_ci{
8123b3eb3cSopenharmony_ci    if (Contains(key) && GetValue(key)->IsBool()) {
8223b3eb3cSopenharmony_ci        return GetValue(key)->GetBool();
8323b3eb3cSopenharmony_ci    }
8423b3eb3cSopenharmony_ci    return defaultValue;
8523b3eb3cSopenharmony_ci}
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ciint32_t JsonValue::GetInt() const
8823b3eb3cSopenharmony_ci{
8923b3eb3cSopenharmony_ci    return static_cast<int32_t>((object_ == nullptr) ? 0 : object_->valuedouble);
9023b3eb3cSopenharmony_ci}
9123b3eb3cSopenharmony_ci
9223b3eb3cSopenharmony_ciuint32_t JsonValue::GetUInt() const
9323b3eb3cSopenharmony_ci{
9423b3eb3cSopenharmony_ci    return static_cast<uint32_t>((object_ == nullptr) ? 0 : object_->valuedouble);
9523b3eb3cSopenharmony_ci}
9623b3eb3cSopenharmony_ci
9723b3eb3cSopenharmony_ciint64_t JsonValue::GetInt64() const
9823b3eb3cSopenharmony_ci{
9923b3eb3cSopenharmony_ci    return static_cast<int64_t>((object_ == nullptr) ? 0 : object_->valuedouble);
10023b3eb3cSopenharmony_ci}
10123b3eb3cSopenharmony_ci
10223b3eb3cSopenharmony_cidouble JsonValue::GetDouble() const
10323b3eb3cSopenharmony_ci{
10423b3eb3cSopenharmony_ci    return (object_ == nullptr) ? 0.0 : object_->valuedouble;
10523b3eb3cSopenharmony_ci}
10623b3eb3cSopenharmony_ci
10723b3eb3cSopenharmony_cidouble JsonValue::GetDouble(const std::string& key, double defaultVal) const
10823b3eb3cSopenharmony_ci{
10923b3eb3cSopenharmony_ci    auto value = GetValue(key);
11023b3eb3cSopenharmony_ci    if (value && value->IsNumber()) {
11123b3eb3cSopenharmony_ci        return value->GetDouble();
11223b3eb3cSopenharmony_ci    }
11323b3eb3cSopenharmony_ci    return defaultVal;
11423b3eb3cSopenharmony_ci}
11523b3eb3cSopenharmony_ci
11623b3eb3cSopenharmony_cistd::string JsonValue::GetString() const
11723b3eb3cSopenharmony_ci{
11823b3eb3cSopenharmony_ci    return ((object_ == nullptr) || (object_->valuestring == nullptr)) ? "" : std::string(object_->valuestring);
11923b3eb3cSopenharmony_ci}
12023b3eb3cSopenharmony_ci
12123b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonValue::GetNext() const
12223b3eb3cSopenharmony_ci{
12323b3eb3cSopenharmony_ci    if (object_ == nullptr) {
12423b3eb3cSopenharmony_ci        return std::make_unique<JsonValue>(nullptr);
12523b3eb3cSopenharmony_ci    }
12623b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(object_->next);
12723b3eb3cSopenharmony_ci}
12823b3eb3cSopenharmony_ci
12923b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonValue::GetChild() const
13023b3eb3cSopenharmony_ci{
13123b3eb3cSopenharmony_ci    if (object_ == nullptr) {
13223b3eb3cSopenharmony_ci        return std::make_unique<JsonValue>(nullptr);
13323b3eb3cSopenharmony_ci    }
13423b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(object_->child);
13523b3eb3cSopenharmony_ci}
13623b3eb3cSopenharmony_ci
13723b3eb3cSopenharmony_cistd::string JsonValue::GetKey() const
13823b3eb3cSopenharmony_ci{
13923b3eb3cSopenharmony_ci    return ((object_ == nullptr) || (object_->string == nullptr)) ? "" : std::string(object_->string);
14023b3eb3cSopenharmony_ci}
14123b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonValue::GetValue(const std::string& key) const
14223b3eb3cSopenharmony_ci{
14323b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(cJSON_GetObjectItem(object_, key.c_str()));
14423b3eb3cSopenharmony_ci}
14523b3eb3cSopenharmony_ci
14623b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonValue::GetObject(const std::string& key) const
14723b3eb3cSopenharmony_ci{
14823b3eb3cSopenharmony_ci    if (Contains(key) && GetValue(key)->IsObject()) {
14923b3eb3cSopenharmony_ci        return GetValue(key);
15023b3eb3cSopenharmony_ci    }
15123b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>();
15223b3eb3cSopenharmony_ci}
15323b3eb3cSopenharmony_ci
15423b3eb3cSopenharmony_ciint32_t JsonValue::GetArraySize() const
15523b3eb3cSopenharmony_ci{
15623b3eb3cSopenharmony_ci    return cJSON_GetArraySize(object_);
15723b3eb3cSopenharmony_ci}
15823b3eb3cSopenharmony_ci
15923b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonValue::GetArrayItem(int32_t index) const
16023b3eb3cSopenharmony_ci{
16123b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(cJSON_GetArrayItem(object_, index));
16223b3eb3cSopenharmony_ci}
16323b3eb3cSopenharmony_ci
16423b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, const char* value)
16523b3eb3cSopenharmony_ci{
16623b3eb3cSopenharmony_ci    if (!value || !key) {
16723b3eb3cSopenharmony_ci        return false;
16823b3eb3cSopenharmony_ci    }
16923b3eb3cSopenharmony_ci
17023b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateString(value);
17123b3eb3cSopenharmony_ci    if (child == nullptr) {
17223b3eb3cSopenharmony_ci        return false;
17323b3eb3cSopenharmony_ci    }
17423b3eb3cSopenharmony_ci    cJSON_AddItemToObject(object_, key, child);
17523b3eb3cSopenharmony_ci    return true;
17623b3eb3cSopenharmony_ci}
17723b3eb3cSopenharmony_ci
17823b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, const char* value,
17923b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
18023b3eb3cSopenharmony_ci{
18123b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
18223b3eb3cSopenharmony_ci        return Put(key, value);
18323b3eb3cSopenharmony_ci    }
18423b3eb3cSopenharmony_ci    return false;
18523b3eb3cSopenharmony_ci}
18623b3eb3cSopenharmony_ci
18723b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, const char* value, const NG::InspectorFilter& filter)
18823b3eb3cSopenharmony_ci{
18923b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
19023b3eb3cSopenharmony_ci        return Put(key, value);
19123b3eb3cSopenharmony_ci    }
19223b3eb3cSopenharmony_ci    return false;
19323b3eb3cSopenharmony_ci}
19423b3eb3cSopenharmony_ci
19523b3eb3cSopenharmony_ciconst JsonObject* JsonValue::GetJsonObject() const
19623b3eb3cSopenharmony_ci{
19723b3eb3cSopenharmony_ci    return object_;
19823b3eb3cSopenharmony_ci}
19923b3eb3cSopenharmony_ci
20023b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, const std::unique_ptr<JsonValue>& value)
20123b3eb3cSopenharmony_ci{
20223b3eb3cSopenharmony_ci    if (!value || !key) {
20323b3eb3cSopenharmony_ci        return false;
20423b3eb3cSopenharmony_ci    }
20523b3eb3cSopenharmony_ci    cJSON* jsonObject = cJSON_Duplicate(value->GetJsonObject(), true);
20623b3eb3cSopenharmony_ci    if (jsonObject == nullptr) {
20723b3eb3cSopenharmony_ci        return false;
20823b3eb3cSopenharmony_ci    }
20923b3eb3cSopenharmony_ci
21023b3eb3cSopenharmony_ci    cJSON_AddItemToObject(object_, key, jsonObject);
21123b3eb3cSopenharmony_ci    return true;
21223b3eb3cSopenharmony_ci}
21323b3eb3cSopenharmony_ci
21423b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, const std::unique_ptr<JsonValue>& value,
21523b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
21623b3eb3cSopenharmony_ci{
21723b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
21823b3eb3cSopenharmony_ci        return Put(key, value);
21923b3eb3cSopenharmony_ci    }
22023b3eb3cSopenharmony_ci    return false;
22123b3eb3cSopenharmony_ci}
22223b3eb3cSopenharmony_ci
22323b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, const std::unique_ptr<JsonValue>& value,
22423b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter)
22523b3eb3cSopenharmony_ci{
22623b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
22723b3eb3cSopenharmony_ci        return Put(key, value);
22823b3eb3cSopenharmony_ci    }
22923b3eb3cSopenharmony_ci    return false;
23023b3eb3cSopenharmony_ci}
23123b3eb3cSopenharmony_ci
23223b3eb3cSopenharmony_ci// add item to array
23323b3eb3cSopenharmony_cibool JsonValue::Put(const std::unique_ptr<JsonValue>& value)
23423b3eb3cSopenharmony_ci{
23523b3eb3cSopenharmony_ci    if (!value) {
23623b3eb3cSopenharmony_ci        return false;
23723b3eb3cSopenharmony_ci    }
23823b3eb3cSopenharmony_ci    cJSON* jsonObject = cJSON_Duplicate(value->GetJsonObject(), true);
23923b3eb3cSopenharmony_ci    if (jsonObject == nullptr) {
24023b3eb3cSopenharmony_ci        return false;
24123b3eb3cSopenharmony_ci    }
24223b3eb3cSopenharmony_ci
24323b3eb3cSopenharmony_ci    cJSON_AddItemToArray(object_, jsonObject);
24423b3eb3cSopenharmony_ci    return true;
24523b3eb3cSopenharmony_ci}
24623b3eb3cSopenharmony_ci
24723b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, size_t value)
24823b3eb3cSopenharmony_ci{
24923b3eb3cSopenharmony_ci    if (key == nullptr) {
25023b3eb3cSopenharmony_ci        return false;
25123b3eb3cSopenharmony_ci    }
25223b3eb3cSopenharmony_ci
25323b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateNumber(static_cast<double>(value));
25423b3eb3cSopenharmony_ci    if (child == nullptr) {
25523b3eb3cSopenharmony_ci        return false;
25623b3eb3cSopenharmony_ci    }
25723b3eb3cSopenharmony_ci    cJSON_AddItemToObject(object_, key, child);
25823b3eb3cSopenharmony_ci    return true;
25923b3eb3cSopenharmony_ci}
26023b3eb3cSopenharmony_ci
26123b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, size_t value,
26223b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
26323b3eb3cSopenharmony_ci{
26423b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
26523b3eb3cSopenharmony_ci        return Put(key, value);
26623b3eb3cSopenharmony_ci    }
26723b3eb3cSopenharmony_ci    return false;
26823b3eb3cSopenharmony_ci}
26923b3eb3cSopenharmony_ci
27023b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, size_t value, const NG::InspectorFilter& filter)
27123b3eb3cSopenharmony_ci{
27223b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
27323b3eb3cSopenharmony_ci        return Put(key, value);
27423b3eb3cSopenharmony_ci    }
27523b3eb3cSopenharmony_ci    return false;
27623b3eb3cSopenharmony_ci}
27723b3eb3cSopenharmony_ci
27823b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, int32_t value)
27923b3eb3cSopenharmony_ci{
28023b3eb3cSopenharmony_ci    if (key == nullptr) {
28123b3eb3cSopenharmony_ci        return false;
28223b3eb3cSopenharmony_ci    }
28323b3eb3cSopenharmony_ci
28423b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateNumber(static_cast<double>(value));
28523b3eb3cSopenharmony_ci    if (child == nullptr) {
28623b3eb3cSopenharmony_ci        return false;
28723b3eb3cSopenharmony_ci    }
28823b3eb3cSopenharmony_ci    cJSON_AddItemToObject(object_, key, child);
28923b3eb3cSopenharmony_ci    return true;
29023b3eb3cSopenharmony_ci}
29123b3eb3cSopenharmony_ci
29223b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, int32_t value,
29323b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
29423b3eb3cSopenharmony_ci{
29523b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
29623b3eb3cSopenharmony_ci        return Put(key, value);
29723b3eb3cSopenharmony_ci    }
29823b3eb3cSopenharmony_ci    return false;
29923b3eb3cSopenharmony_ci}
30023b3eb3cSopenharmony_ci
30123b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, int32_t value, const NG::InspectorFilter& filter)
30223b3eb3cSopenharmony_ci{
30323b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
30423b3eb3cSopenharmony_ci        return Put(key, value);
30523b3eb3cSopenharmony_ci    }
30623b3eb3cSopenharmony_ci    return false;
30723b3eb3cSopenharmony_ci}
30823b3eb3cSopenharmony_ci
30923b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, int64_t value)
31023b3eb3cSopenharmony_ci{
31123b3eb3cSopenharmony_ci    return Put(key, static_cast<double>(value));
31223b3eb3cSopenharmony_ci}
31323b3eb3cSopenharmony_ci
31423b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, int64_t value,
31523b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
31623b3eb3cSopenharmony_ci{
31723b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
31823b3eb3cSopenharmony_ci        return Put(key, value);
31923b3eb3cSopenharmony_ci    }
32023b3eb3cSopenharmony_ci    return false;
32123b3eb3cSopenharmony_ci}
32223b3eb3cSopenharmony_ci
32323b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, int64_t value, const NG::InspectorFilter& filter)
32423b3eb3cSopenharmony_ci{
32523b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
32623b3eb3cSopenharmony_ci        return Put(key, value);
32723b3eb3cSopenharmony_ci    }
32823b3eb3cSopenharmony_ci    return false;
32923b3eb3cSopenharmony_ci}
33023b3eb3cSopenharmony_ci
33123b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, double value)
33223b3eb3cSopenharmony_ci{
33323b3eb3cSopenharmony_ci    if (key == nullptr) {
33423b3eb3cSopenharmony_ci        return false;
33523b3eb3cSopenharmony_ci    }
33623b3eb3cSopenharmony_ci
33723b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateNumber(value);
33823b3eb3cSopenharmony_ci    if (child == nullptr) {
33923b3eb3cSopenharmony_ci        return false;
34023b3eb3cSopenharmony_ci    }
34123b3eb3cSopenharmony_ci    cJSON_AddItemToObject(object_, key, child);
34223b3eb3cSopenharmony_ci    return true;
34323b3eb3cSopenharmony_ci}
34423b3eb3cSopenharmony_ci
34523b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, double value,
34623b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
34723b3eb3cSopenharmony_ci{
34823b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
34923b3eb3cSopenharmony_ci        return Put(key, value);
35023b3eb3cSopenharmony_ci    }
35123b3eb3cSopenharmony_ci    return false;
35223b3eb3cSopenharmony_ci}
35323b3eb3cSopenharmony_ci
35423b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, double value, const NG::InspectorFilter& filter)
35523b3eb3cSopenharmony_ci{
35623b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
35723b3eb3cSopenharmony_ci        return Put(key, value);
35823b3eb3cSopenharmony_ci    }
35923b3eb3cSopenharmony_ci    return false;
36023b3eb3cSopenharmony_ci}
36123b3eb3cSopenharmony_ci
36223b3eb3cSopenharmony_ciJsonObject* JsonValue::ReleaseJsonObject()
36323b3eb3cSopenharmony_ci{
36423b3eb3cSopenharmony_ci    if (!isRoot_) {
36523b3eb3cSopenharmony_ci        return nullptr;
36623b3eb3cSopenharmony_ci    }
36723b3eb3cSopenharmony_ci    JsonObject* object = object_;
36823b3eb3cSopenharmony_ci    object_ = nullptr;
36923b3eb3cSopenharmony_ci    return object;
37023b3eb3cSopenharmony_ci}
37123b3eb3cSopenharmony_ci
37223b3eb3cSopenharmony_cibool JsonValue::PutRef(const char* key, std::unique_ptr<JsonValue>&& value)
37323b3eb3cSopenharmony_ci{
37423b3eb3cSopenharmony_ci    if (key == nullptr || value == nullptr) {
37523b3eb3cSopenharmony_ci        return false;
37623b3eb3cSopenharmony_ci    }
37723b3eb3cSopenharmony_ci    /*
37823b3eb3cSopenharmony_ci    * If value is root, it controls the lifecycle of JsonObject, we can just move it into current object
37923b3eb3cSopenharmony_ci    * Else we need to copy the JsonObject and put the new object in current object
38023b3eb3cSopenharmony_ci    */
38123b3eb3cSopenharmony_ci    if (value->isRoot_) {
38223b3eb3cSopenharmony_ci        cJSON_AddItemToObject(object_, key, value->ReleaseJsonObject());
38323b3eb3cSopenharmony_ci        return true;
38423b3eb3cSopenharmony_ci    } else {
38523b3eb3cSopenharmony_ci        std::unique_ptr<JsonValue> lValue = std::move(value);
38623b3eb3cSopenharmony_ci        return Put(key, lValue);
38723b3eb3cSopenharmony_ci    }
38823b3eb3cSopenharmony_ci}
38923b3eb3cSopenharmony_ci
39023b3eb3cSopenharmony_cibool JsonValue::PutRef(std::unique_ptr<JsonValue>&& value)
39123b3eb3cSopenharmony_ci{
39223b3eb3cSopenharmony_ci    if (value == nullptr) {
39323b3eb3cSopenharmony_ci        return false;
39423b3eb3cSopenharmony_ci    }
39523b3eb3cSopenharmony_ci    /*
39623b3eb3cSopenharmony_ci    * If value is root, it controls the lifecycle of JsonObject, we can just move it into current object
39723b3eb3cSopenharmony_ci    * Else we need to copy the JsonObject and put the new object in current object
39823b3eb3cSopenharmony_ci    */
39923b3eb3cSopenharmony_ci    if (value->isRoot_) {
40023b3eb3cSopenharmony_ci        cJSON_AddItemToArray(object_, value->ReleaseJsonObject());
40123b3eb3cSopenharmony_ci        return true;
40223b3eb3cSopenharmony_ci    } else {
40323b3eb3cSopenharmony_ci        std::unique_ptr<JsonValue> lValue = std::move(value);
40423b3eb3cSopenharmony_ci        return Put(lValue);
40523b3eb3cSopenharmony_ci    }
40623b3eb3cSopenharmony_ci}
40723b3eb3cSopenharmony_ci
40823b3eb3cSopenharmony_cibool JsonValue::Replace(const char* key, double value)
40923b3eb3cSopenharmony_ci{
41023b3eb3cSopenharmony_ci    if (key == nullptr) {
41123b3eb3cSopenharmony_ci        return false;
41223b3eb3cSopenharmony_ci    }
41323b3eb3cSopenharmony_ci
41423b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateNumber(value);
41523b3eb3cSopenharmony_ci    if (child == nullptr) {
41623b3eb3cSopenharmony_ci        return false;
41723b3eb3cSopenharmony_ci    }
41823b3eb3cSopenharmony_ci    if (!cJSON_ReplaceItemInObject(object_, key, child)) {
41923b3eb3cSopenharmony_ci        cJSON_Delete(child);
42023b3eb3cSopenharmony_ci        return false;
42123b3eb3cSopenharmony_ci    }
42223b3eb3cSopenharmony_ci    return true;
42323b3eb3cSopenharmony_ci}
42423b3eb3cSopenharmony_ci
42523b3eb3cSopenharmony_cibool JsonValue::Put(const char* key, bool value)
42623b3eb3cSopenharmony_ci{
42723b3eb3cSopenharmony_ci    if (key == nullptr) {
42823b3eb3cSopenharmony_ci        return false;
42923b3eb3cSopenharmony_ci    }
43023b3eb3cSopenharmony_ci
43123b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateBool(value);
43223b3eb3cSopenharmony_ci    if (child == nullptr) {
43323b3eb3cSopenharmony_ci        return false;
43423b3eb3cSopenharmony_ci    }
43523b3eb3cSopenharmony_ci    cJSON_AddItemToObject(object_, key, child);
43623b3eb3cSopenharmony_ci    return true;
43723b3eb3cSopenharmony_ci}
43823b3eb3cSopenharmony_ci
43923b3eb3cSopenharmony_cibool JsonValue::PutFixedAttr(const char* key, bool value,
44023b3eb3cSopenharmony_ci    const NG::InspectorFilter& filter, NG::FixedAttrBit attr)
44123b3eb3cSopenharmony_ci{
44223b3eb3cSopenharmony_ci    if (filter.CheckFixedAttr(attr)) {
44323b3eb3cSopenharmony_ci        return Put(key, value);
44423b3eb3cSopenharmony_ci    }
44523b3eb3cSopenharmony_ci    return false;
44623b3eb3cSopenharmony_ci}
44723b3eb3cSopenharmony_ci
44823b3eb3cSopenharmony_cibool JsonValue::PutExtAttr(const char* key, bool value, const NG::InspectorFilter& filter)
44923b3eb3cSopenharmony_ci{
45023b3eb3cSopenharmony_ci    if (filter.CheckExtAttr(key)) {
45123b3eb3cSopenharmony_ci        return Put(key, value);
45223b3eb3cSopenharmony_ci    }
45323b3eb3cSopenharmony_ci    return false;
45423b3eb3cSopenharmony_ci}
45523b3eb3cSopenharmony_ci
45623b3eb3cSopenharmony_cibool JsonValue::Replace(const char* key, bool value)
45723b3eb3cSopenharmony_ci{
45823b3eb3cSopenharmony_ci    if (key == nullptr) {
45923b3eb3cSopenharmony_ci        return false;
46023b3eb3cSopenharmony_ci    }
46123b3eb3cSopenharmony_ci
46223b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateBool(value);
46323b3eb3cSopenharmony_ci    if (child == nullptr) {
46423b3eb3cSopenharmony_ci        return false;
46523b3eb3cSopenharmony_ci    }
46623b3eb3cSopenharmony_ci    if (!cJSON_ReplaceItemInObject(object_, key, child)) {
46723b3eb3cSopenharmony_ci        cJSON_Delete(child);
46823b3eb3cSopenharmony_ci        return false;
46923b3eb3cSopenharmony_ci    }
47023b3eb3cSopenharmony_ci    return true;
47123b3eb3cSopenharmony_ci}
47223b3eb3cSopenharmony_ci
47323b3eb3cSopenharmony_cibool JsonValue::Replace(const char* key, const char* value)
47423b3eb3cSopenharmony_ci{
47523b3eb3cSopenharmony_ci    if ((value == nullptr) || (key == nullptr)) {
47623b3eb3cSopenharmony_ci        return false;
47723b3eb3cSopenharmony_ci    }
47823b3eb3cSopenharmony_ci
47923b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateString(value);
48023b3eb3cSopenharmony_ci    if (child == nullptr) {
48123b3eb3cSopenharmony_ci        return false;
48223b3eb3cSopenharmony_ci    }
48323b3eb3cSopenharmony_ci    if (!cJSON_ReplaceItemInObject(object_, key, child)) {
48423b3eb3cSopenharmony_ci        cJSON_Delete(child);
48523b3eb3cSopenharmony_ci        return false;
48623b3eb3cSopenharmony_ci    }
48723b3eb3cSopenharmony_ci    return true;
48823b3eb3cSopenharmony_ci}
48923b3eb3cSopenharmony_ci
49023b3eb3cSopenharmony_cibool JsonValue::Replace(const char* key, int32_t value)
49123b3eb3cSopenharmony_ci{
49223b3eb3cSopenharmony_ci    if (key == nullptr) {
49323b3eb3cSopenharmony_ci        return false;
49423b3eb3cSopenharmony_ci    }
49523b3eb3cSopenharmony_ci
49623b3eb3cSopenharmony_ci    cJSON* child = cJSON_CreateNumber(static_cast<double>(value));
49723b3eb3cSopenharmony_ci    if (child == nullptr) {
49823b3eb3cSopenharmony_ci        return false;
49923b3eb3cSopenharmony_ci    }
50023b3eb3cSopenharmony_ci    if (!cJSON_ReplaceItemInObject(object_, key, child)) {
50123b3eb3cSopenharmony_ci        cJSON_Delete(child);
50223b3eb3cSopenharmony_ci        return false;
50323b3eb3cSopenharmony_ci    }
50423b3eb3cSopenharmony_ci    return true;
50523b3eb3cSopenharmony_ci}
50623b3eb3cSopenharmony_ci
50723b3eb3cSopenharmony_cibool JsonValue::Replace(const char* key, const std::unique_ptr<JsonValue>& value)
50823b3eb3cSopenharmony_ci{
50923b3eb3cSopenharmony_ci    if ((value == nullptr) || (key == nullptr)) {
51023b3eb3cSopenharmony_ci        return false;
51123b3eb3cSopenharmony_ci    }
51223b3eb3cSopenharmony_ci    cJSON* jsonObject = cJSON_Duplicate(value->GetJsonObject(), true);
51323b3eb3cSopenharmony_ci    if (jsonObject == nullptr) {
51423b3eb3cSopenharmony_ci        return false;
51523b3eb3cSopenharmony_ci    }
51623b3eb3cSopenharmony_ci
51723b3eb3cSopenharmony_ci    if (!cJSON_ReplaceItemInObject(object_, key, jsonObject)) {
51823b3eb3cSopenharmony_ci        cJSON_Delete(jsonObject);
51923b3eb3cSopenharmony_ci        return false;
52023b3eb3cSopenharmony_ci    }
52123b3eb3cSopenharmony_ci    return true;
52223b3eb3cSopenharmony_ci}
52323b3eb3cSopenharmony_ci
52423b3eb3cSopenharmony_cibool JsonValue::Delete(const char* key)
52523b3eb3cSopenharmony_ci{
52623b3eb3cSopenharmony_ci    if (key == nullptr) {
52723b3eb3cSopenharmony_ci        return false;
52823b3eb3cSopenharmony_ci    }
52923b3eb3cSopenharmony_ci    cJSON_DeleteItemFromObject(object_, key);
53023b3eb3cSopenharmony_ci    return true;
53123b3eb3cSopenharmony_ci}
53223b3eb3cSopenharmony_ci
53323b3eb3cSopenharmony_cistd::string JsonValue::ToString()
53423b3eb3cSopenharmony_ci{
53523b3eb3cSopenharmony_ci    std::string result;
53623b3eb3cSopenharmony_ci    if (!object_) {
53723b3eb3cSopenharmony_ci        return result;
53823b3eb3cSopenharmony_ci    }
53923b3eb3cSopenharmony_ci
54023b3eb3cSopenharmony_ci    // It is null-terminated.
54123b3eb3cSopenharmony_ci    char* unformatted = cJSON_PrintUnformatted(object_);
54223b3eb3cSopenharmony_ci    if (unformatted != nullptr) {
54323b3eb3cSopenharmony_ci        result = unformatted;
54423b3eb3cSopenharmony_ci        cJSON_free(unformatted);
54523b3eb3cSopenharmony_ci    }
54623b3eb3cSopenharmony_ci    return result;
54723b3eb3cSopenharmony_ci}
54823b3eb3cSopenharmony_ci
54923b3eb3cSopenharmony_cistd::string JsonValue::GetString(const std::string& key, const std::string& defaultVal) const
55023b3eb3cSopenharmony_ci{
55123b3eb3cSopenharmony_ci    auto value = GetValue(key);
55223b3eb3cSopenharmony_ci    if (value && value->IsString()) {
55323b3eb3cSopenharmony_ci        return value->GetString();
55423b3eb3cSopenharmony_ci    }
55523b3eb3cSopenharmony_ci    return defaultVal;
55623b3eb3cSopenharmony_ci}
55723b3eb3cSopenharmony_ci
55823b3eb3cSopenharmony_ciint32_t JsonValue::GetInt(const std::string& key, int32_t defaultVal) const
55923b3eb3cSopenharmony_ci{
56023b3eb3cSopenharmony_ci    auto value = GetValue(key);
56123b3eb3cSopenharmony_ci    if (value && value->IsNumber()) {
56223b3eb3cSopenharmony_ci        return value->GetInt();
56323b3eb3cSopenharmony_ci    }
56423b3eb3cSopenharmony_ci    return defaultVal;
56523b3eb3cSopenharmony_ci}
56623b3eb3cSopenharmony_ci
56723b3eb3cSopenharmony_ciuint32_t JsonValue::GetUInt(const std::string& key, uint32_t defaultVal) const
56823b3eb3cSopenharmony_ci{
56923b3eb3cSopenharmony_ci    auto value = GetValue(key);
57023b3eb3cSopenharmony_ci    if (value && value->IsNumber()) {
57123b3eb3cSopenharmony_ci        return value->GetUInt();
57223b3eb3cSopenharmony_ci    }
57323b3eb3cSopenharmony_ci    return defaultVal;
57423b3eb3cSopenharmony_ci}
57523b3eb3cSopenharmony_ci
57623b3eb3cSopenharmony_ciint64_t JsonValue::GetInt64(const std::string& key, int64_t defaultVal) const
57723b3eb3cSopenharmony_ci{
57823b3eb3cSopenharmony_ci    auto value = GetValue(key);
57923b3eb3cSopenharmony_ci    if (value && value->IsNumber()) {
58023b3eb3cSopenharmony_ci        return value->GetInt64();
58123b3eb3cSopenharmony_ci    }
58223b3eb3cSopenharmony_ci    return defaultVal;
58323b3eb3cSopenharmony_ci}
58423b3eb3cSopenharmony_ci
58523b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonUtil::ParseJsonData(const char* data, const char** parseEnd)
58623b3eb3cSopenharmony_ci{
58723b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(cJSON_ParseWithOpts(data, parseEnd, true), true);
58823b3eb3cSopenharmony_ci}
58923b3eb3cSopenharmony_ci
59023b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonUtil::ParseJsonString(const std::string& content, const char** parseEnd)
59123b3eb3cSopenharmony_ci{
59223b3eb3cSopenharmony_ci    return ParseJsonData(content.c_str(), parseEnd);
59323b3eb3cSopenharmony_ci}
59423b3eb3cSopenharmony_ci
59523b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonUtil::Create(bool isRoot)
59623b3eb3cSopenharmony_ci{
59723b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(cJSON_CreateObject(), isRoot);
59823b3eb3cSopenharmony_ci}
59923b3eb3cSopenharmony_ci
60023b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> JsonUtil::CreateArray(bool isRoot)
60123b3eb3cSopenharmony_ci{
60223b3eb3cSopenharmony_ci    return std::make_unique<JsonValue>(cJSON_CreateArray(), isRoot);
60323b3eb3cSopenharmony_ci}
60423b3eb3cSopenharmony_ci
60523b3eb3cSopenharmony_ci} // namespace OHOS::Ace
606