• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_INTERFACE_UI_SESSION_JSON_UTIL_H
17 #define FOUNDATION_ACE_INTERFACE_UI_SESSION_JSON_UTIL_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "base/utils/macros.h"
23 
24 struct cJSON;
25 
26 namespace OHOS::Ace {
27 
28 using JsonObject = cJSON;
29 
30 class ACE_FORCE_EXPORT InspectorJsonValue {
31 public:
32     InspectorJsonValue() = default;
33     explicit InspectorJsonValue(JsonObject* object);
34     InspectorJsonValue(JsonObject* object, bool isRoot);
35     ~InspectorJsonValue();
36     // put functions
37     bool Put(const char* key, const char* value);
38     bool Put(const char* key, size_t value);
39     bool Put(const char* key, int32_t value);
40     bool Put(const char* key, int64_t value);
41     bool Put(const char* key, double value);
42     bool Put(const char* key, bool value);
43     bool Put(const char* key, const std::unique_ptr<InspectorJsonValue>& value);
44     bool Put(const std::unique_ptr<InspectorJsonValue>& value);
45     bool Put(double value);
46     bool Replace(const char* key, const char* value);
47     // serialize
48     std::string ToString();
49     bool IsObject() const;
50     bool IsNull() const;
51     bool IsString() const;
52     bool IsNumber() const;
53     bool Contains(const std::string& key) const;
54     const JsonObject* GetJsonObject() const;
55 
56     std::string GetString() const;
57     std::string GetString(const std::string& key, const std::string& defaultVal = "") const;
58     std::unique_ptr<InspectorJsonValue> GetValue(const std::string& key) const;
59     int32_t GetInt() const;
60     int32_t GetInt(const std::string& key, int32_t defaultVal = 0) const;
61 
62 private:
63     JsonObject* object_ = nullptr;
64     bool isRoot_ = false;
65 };
66 
67 class ACE_FORCE_EXPORT InspectorJsonUtil final {
68 public:
69     InspectorJsonUtil() = delete;
70     ~InspectorJsonUtil() = delete;
71     static std::shared_ptr<InspectorJsonValue> Create(bool isRoot = true);
72     static std::unique_ptr<InspectorJsonValue> CreateArray(bool isRoot = true);
73     static std::unique_ptr<InspectorJsonValue> CreateObject(bool isRoot = true);
74     static std::unique_ptr<InspectorJsonValue> ParseJsonData(const char* data, const char** parseEnd = nullptr);
75     static std::unique_ptr<InspectorJsonValue> ParseJsonString(
76         const std::string& content, const char** parseEnd = nullptr);
77 };
78 
79 } // namespace OHOS::Ace
80 
81 #endif // FOUNDATION_ACE_INTERFACE_UI_SESSION_JSON_UTIL_H