• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 JSON_NODE_H
17 #define JSON_NODE_H
18 
19 #include <filesystem>
20 #include <list>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <type_traits>
25 #include <unordered_map>
26 #include <variant>
27 #include <vector>
28 
29 #include "cJSON.h"
30 #include "macros.h"
31 #include "traits_util.h"
32 
33 namespace Updater {
34 class JsonNode;
35 
36 enum class NodeType { OBJECT, INT, STRING, ARRAY, BOOL, NUL, UNKNOWN };
37 
38 using NodeMap = std::unordered_map<std::string, std::unique_ptr<JsonNode>>;
39 using NodeVec = std::vector<std::unique_ptr<JsonNode>>;
40 using cJSONPtr = std::unique_ptr<cJSON, decltype(&cJSON_Delete)>;
41 template<typename...T>
42 using optionalVariant = std::variant<std::optional<T> ...>;
43 
44 namespace Fs = std::filesystem;
45 class JsonNode {
46     DISALLOW_COPY_MOVE(JsonNode);
47 public:
48     JsonNode();
49     explicit JsonNode(const Fs::path &path);
50     explicit JsonNode(const std::string &str, bool needDelete = true);
51     explicit JsonNode(const cJSON *root, bool needDelete = true);
52     ~JsonNode();
53 
54     const JsonNode &operator[](int idx) const;
55     const JsonNode &operator[](const std::string &key) const;
56 
57     template<typename T>
As()58     std::optional<T> As() const
59     {
60         if (auto optPtr = std::get_if<std::optional<Detail::StandardType<T>>>(&innerObj_); optPtr) {
61             return *optPtr;
62         }
63         return std::nullopt;
64     }
65 
66     template<typename T>
67     bool operator==(T rhs) const
68     {
69         if (auto optPtr = std::get_if<std::optional<Detail::StandardType<T>>>(&innerObj_); optPtr) {
70             return *optPtr == rhs;
71         }
72         return false;
73     }
74 
Size()75     int Size() const
76     {
77         return size_;
78     }
Type()79     NodeType Type() const
80     {
81         return type_;
82     }
Key()83     std::optional<std::string> Key() const
84     {
85         return key_;
86     }
87     std::list<std::reference_wrapper<JsonNode>>::const_iterator begin() const;
88     std::list<std::reference_wrapper<JsonNode>>::const_iterator end() const;
89 private:
90     void Parse(const cJSON *root);
91     void Init(const cJSON *root, bool needDelete);
92     template<typename T>
Assign(T rhs)93     void Assign(T rhs) const
94     {
95         if (innerObj_.valueless_by_exception()) {
96             innerObj_ = std::optional<T>(rhs);
97         }
98         if (auto optPtr = std::get_if<std::optional<T>>(&innerObj_); optPtr) {
99             *optPtr = std::optional<T>(rhs);
100         }
101     }
102     int size_ {1};
103     NodeType type_ {NodeType::UNKNOWN};         /* json node type */
104     std::optional<std::string> key_ {std::nullopt}; /* key for object items */
105     optionalVariant<bool, int, std::string, NodeVec, NodeMap> innerObj_ {};
106     std::list<std::reference_wrapper<JsonNode>> innerNodesList_ {};
107 };
108 
GetInvalidNode()109 inline const JsonNode &GetInvalidNode()
110 {
111     static JsonNode emptyNode;  // used for invalid json node
112     return emptyNode;
113 }
114 }
115 #endif // NODE_H
116