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 std::vector<JsonBuilder>::size_type valueListSize = valueList.size(); 73 if (!valueList.empty()) { 74 JsonBuilder jsonArray; 75 jsonArray.Append("["); 76 for (std::vector<JsonBuilder>::size_type i = 0; i < valueListSize; i++) { 77 jsonArray.Append(valueList[i].ToJson()); 78 if (i != valueListSize - 1) { 79 jsonArray.Append(","); 80 } 81 } 82 jsonArray.Append("]"); 83 84 Append(key, jsonArray); 85 } 86 return *this; 87 } 88 ToJson()89 std::string ToJson() const 90 { 91 return builder_; 92 } 93 94 private: AppendNum(const std::string & key,const std::string & value)95 JsonBuilder &AppendNum(const std::string &key, const std::string &value) 96 { 97 AppendComma(); 98 builder_.append("\"").append(key).append("\"").append(":").append(value); 99 return *this; 100 } 101 AppendComma()102 void AppendComma() 103 { 104 if (isFirstItem_) { 105 isFirstItem_ = false; 106 } else { 107 builder_.append(","); 108 } 109 } 110 111 private: 112 std::string builder_; 113 bool isFirstItem_ = true; 114 }; 115 } // namespace OHOS::UpdateEngine 116 #endif // JSON_BUILDER_H