• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_BUILDER_H
17 #define JSON_BUILDER_H
18 
19 #include <string>
20 #include <vector>
21 
22 namespace OHOS::UpdateEngine {
23 class JsonBuilder {
24 public:
Append(const std::string & qualifier)25     JsonBuilder &Append(const std::string &qualifier)
26     {
27         builder_.append(qualifier);
28         return *this;
29     }
30 
Append(const std::string & key,const uint32_t value)31     JsonBuilder &Append(const std::string &key, const uint32_t value)
32     {
33         return AppendNum(key, std::to_string(value));
34     }
35 
Append(const std::string & key,const int32_t value)36     JsonBuilder &Append(const std::string &key, const int32_t value)
37     {
38         return AppendNum(key, std::to_string(value));
39     }
40 
Append(const std::string & key,const bool value)41     JsonBuilder &Append(const std::string &key, const bool value)
42     {
43         const std::string valueString = value ? "true" : "false";
44         return AppendNum(key, valueString);
45     }
46 
Append(const std::string & key,const int64_t value)47     JsonBuilder &Append(const std::string &key, const int64_t value)
48     {
49         return AppendNum(key, std::to_string(value));
50     }
51 
52     JsonBuilder &Append(const std::string &key, const std::string &value, const bool isJsonString = false)
53     {
54         AppendComma();
55         if (isJsonString) {
56             builder_.append("\"").append(key).append("\"").append(":").append(value);
57         } else {
58             builder_.append("\"").append(key).append("\"").append(":").append("\"").append(value).append("\"");
59         }
60         return *this;
61     }
62 
Append(const std::string & key,const JsonBuilder & jsonBuilder)63     JsonBuilder &Append(const std::string &key, const JsonBuilder &jsonBuilder)
64     {
65         AppendComma();
66         builder_.append("\"").append(key).append("\"").append(":").append(jsonBuilder.ToJson());
67         return *this;
68     }
69 
Append(const std::string & key,const std::vector<JsonBuilder> & valueList)70     JsonBuilder &Append(const std::string &key, const std::vector<JsonBuilder> &valueList)
71     {
72         if (!valueList.empty()) {
73             JsonBuilder jsonArray;
74             jsonArray.Append("[");
75             for (size_t i = 0; i < valueList.size(); i++) {
76                 jsonArray.Append(valueList[i].ToJson());
77                 if (i != valueList.size() - 1) {
78                     jsonArray.Append(",");
79                 }
80             }
81             jsonArray.Append("]");
82 
83             Append(key, jsonArray);
84         }
85         return *this;
86     }
87 
ToJson()88     std::string ToJson() const
89     {
90         return builder_;
91     }
92 
93 private:
AppendNum(const std::string & key,const std::string & value)94     JsonBuilder &AppendNum(const std::string &key, const std::string &value)
95     {
96         AppendComma();
97         builder_.append("\"").append(key).append("\"").append(":").append(value);
98         return *this;
99     }
100 
AppendComma()101     void AppendComma()
102     {
103         if (isFirstItem_) {
104             isFirstItem_ = false;
105         } else {
106             builder_.append(",");
107         }
108     }
109 
110 private:
111     std::string builder_;
112     bool isFirstItem_ = true;
113 };
114 } // namespace OHOS::UpdateEngine
115 #endif // JSON_BUILDER_H