/root/doris/be/src/util/easy_json.cc
| Line | Count | Source | 
| 1 |  | // Licensed to the Apache Software Foundation (ASF) under one | 
| 2 |  | // or more contributor license agreements.  See the NOTICE file | 
| 3 |  | // distributed with this work for additional information | 
| 4 |  | // regarding copyright ownership.  The ASF licenses this file | 
| 5 |  | // to you under the Apache License, Version 2.0 (the | 
| 6 |  | // "License"); you may not use this file except in compliance | 
| 7 |  | // with the License.  You may obtain a copy of the License at | 
| 8 |  | // | 
| 9 |  | //   http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  | // | 
| 11 |  | // Unless required by applicable law or agreed to in writing, | 
| 12 |  | // software distributed under the License is distributed on an | 
| 13 |  | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
| 14 |  | // KIND, either express or implied.  See the License for the | 
| 15 |  | // specific language governing permissions and limitations | 
| 16 |  | // under the License. | 
| 17 |  |  | 
| 18 |  | #include "util/easy_json.h" | 
| 19 |  |  | 
| 20 |  | #include <glog/logging.h> | 
| 21 |  | #include <rapidjson/allocators.h> | 
| 22 |  | #include <rapidjson/document.h> | 
| 23 |  | #include <rapidjson/rapidjson.h> | 
| 24 |  | #include <rapidjson/stringbuffer.h> | 
| 25 |  | #include <rapidjson/writer.h> | 
| 26 |  |  | 
| 27 |  | #include <ostream> | 
| 28 |  | #include <string> | 
| 29 |  | #include <utility> | 
| 30 |  |  | 
| 31 |  | #include "common/exception.h" | 
| 32 |  | // IWYU pragma: no_include <rapidjson/encodings.h> | 
| 33 |  |  | 
| 34 |  | using rapidjson::SizeType; | 
| 35 |  | using rapidjson::Value; | 
| 36 |  | using std::string; | 
| 37 |  |  | 
| 38 |  | namespace doris { | 
| 39 |  |  | 
| 40 | 7 | EasyJson::EasyJson() : alloc_(new EasyJsonAllocator), value_(&alloc_->value()) {} | 
| 41 |  |  | 
| 42 |  | EasyJson::EasyJson(EasyJson::ComplexTypeInitializer type) | 
| 43 | 0 |         : alloc_(new EasyJsonAllocator), value_(&alloc_->value()) { | 
| 44 | 0 |     if (type == kObject) { | 
| 45 | 0 |         value_->SetObject(); | 
| 46 | 0 |     } else if (type == kArray) { | 
| 47 | 0 |         value_->SetArray(); | 
| 48 | 0 |     } | 
| 49 | 0 | } | 
| 50 |  |  | 
| 51 | 14 | EasyJson EasyJson::Get(const string& key) { | 
| 52 | 14 |     if (!value_->IsObject()) { | 
| 53 | 4 |         value_->SetObject(); | 
| 54 | 4 |     } | 
| 55 | 14 |     if (!value_->HasMember(key.c_str())) { | 
| 56 | 9 |         Value key_val(key.c_str(), alloc_->allocator()); | 
| 57 | 9 |         value_->AddMember(key_val, Value().SetNull(), alloc_->allocator()); | 
| 58 | 9 |     } | 
| 59 | 14 |     return EasyJson(&(*value_)[key.c_str()], alloc_); | 
| 60 | 14 | } | 
| 61 |  |  | 
| 62 | 3 | EasyJson EasyJson::Get(int index) { | 
| 63 | 3 |     if (!value_->IsArray()) { | 
| 64 | 1 |         value_->SetArray(); | 
| 65 | 1 |     } | 
| 66 | 5 |     while (SizeType(index) >= value_->Size()) { | 
| 67 | 2 |         value_->PushBack(Value().SetNull(), alloc_->allocator()); | 
| 68 | 2 |     } | 
| 69 | 3 |     return EasyJson(&(*value_)[index], alloc_); | 
| 70 | 3 | } | 
| 71 |  |  | 
| 72 | 7 | EasyJson EasyJson::operator[](const string& key) { | 
| 73 | 7 |     return Get(key); | 
| 74 | 7 | } | 
| 75 |  |  | 
| 76 | 3 | EasyJson EasyJson::operator[](int index) { | 
| 77 | 3 |     return Get(index); | 
| 78 | 3 | } | 
| 79 |  |  | 
| 80 | 0 | EasyJson& EasyJson::operator=(const string& val) { | 
| 81 | 0 |     value_->SetString(val.c_str(), alloc_->allocator()); | 
| 82 | 0 |     return *this; | 
| 83 | 0 | } | 
| 84 |  | template <typename T> | 
| 85 | 5 | EasyJson& EasyJson::operator=(T val) { | 
| 86 | 5 |     *value_ = val; | 
| 87 | 5 |     return *this; | 
| 88 | 5 | } _ZN5doris8EasyJsonaSIbEERS0_T_| Line | Count | Source |  | 85 | 2 | EasyJson& EasyJson::operator=(T val) { |  | 86 | 2 |     *value_ = val; |  | 87 | 2 |     return *this; |  | 88 | 2 | } | 
_ZN5doris8EasyJsonaSIiEERS0_T_| Line | Count | Source |  | 85 | 3 | EasyJson& EasyJson::operator=(T val) { |  | 86 | 3 |     *value_ = val; |  | 87 | 3 |     return *this; |  | 88 | 3 | } | 
Unexecuted instantiation: _ZN5doris8EasyJsonaSIlEERS0_T_Unexecuted instantiation: _ZN5doris8EasyJsonaSIjEERS0_T_Unexecuted instantiation: _ZN5doris8EasyJsonaSImEERS0_T_Unexecuted instantiation: _ZN5doris8EasyJsonaSIdEERS0_T_ | 
| 89 |  | template EasyJson& EasyJson::operator=(bool val); | 
| 90 |  | template EasyJson& EasyJson::operator=(int32_t val); | 
| 91 |  | template EasyJson& EasyJson::operator=(int64_t val); | 
| 92 |  | template EasyJson& EasyJson::operator=(uint32_t val); | 
| 93 |  | template EasyJson& EasyJson::operator=(uint64_t val); | 
| 94 |  | template EasyJson& EasyJson::operator=(double val); | 
| 95 |  | template <> | 
| 96 | 0 | EasyJson& EasyJson::operator=(const char* val) { | 
| 97 | 0 |     value_->SetString(val, alloc_->allocator()); | 
| 98 | 0 |     return *this; | 
| 99 | 0 | } | 
| 100 |  | template <> | 
| 101 | 2 | EasyJson& EasyJson::operator=(EasyJson::ComplexTypeInitializer val) { | 
| 102 | 2 |     if (val == kObject) { | 
| 103 | 1 |         value_->SetObject(); | 
| 104 | 1 |     } else if (val == kArray) { | 
| 105 | 1 |         value_->SetArray(); | 
| 106 | 1 |     } | 
| 107 | 2 |     return (*this); | 
| 108 | 2 | } | 
| 109 |  |  | 
| 110 |  | #ifdef __APPLE__ | 
| 111 |  | template <> | 
| 112 |  | EasyJson& EasyJson::operator=(unsigned long val) { | 
| 113 |  |     return EasyJson::operator=(static_cast<uint64_t>(val)); | 
| 114 |  | } | 
| 115 |  | #endif | 
| 116 |  |  | 
| 117 | 2 | EasyJson& EasyJson::SetObject() { | 
| 118 | 2 |     if (!value_->IsObject()) { | 
| 119 | 2 |         value_->SetObject(); | 
| 120 | 2 |     } | 
| 121 | 2 |     return *this; | 
| 122 | 2 | } | 
| 123 |  |  | 
| 124 | 1 | EasyJson& EasyJson::SetArray() { | 
| 125 | 1 |     if (!value_->IsArray()) { | 
| 126 | 1 |         value_->SetArray(); | 
| 127 | 1 |     } | 
| 128 | 1 |     return *this; | 
| 129 | 1 | } | 
| 130 |  |  | 
| 131 | 0 | EasyJson EasyJson::Set(const string& key, const string& val) { | 
| 132 | 0 |     return (Get(key) = val); | 
| 133 | 0 | } | 
| 134 |  | template <typename T> | 
| 135 | 2 | EasyJson EasyJson::Set(const string& key, T val) { | 
| 136 | 2 |     return (Get(key) = val); | 
| 137 | 2 | } _ZN5doris8EasyJson3SetIbEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_| Line | Count | Source |  | 135 | 1 | EasyJson EasyJson::Set(const string& key, T val) { |  | 136 | 1 |     return (Get(key) = val); |  | 137 | 1 | } | 
Unexecuted instantiation: _ZN5doris8EasyJson3SetIiEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_Unexecuted instantiation: _ZN5doris8EasyJson3SetIlEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_Unexecuted instantiation: _ZN5doris8EasyJson3SetIjEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_Unexecuted instantiation: _ZN5doris8EasyJson3SetImEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_Unexecuted instantiation: _ZN5doris8EasyJson3SetIdEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_Unexecuted instantiation: _ZN5doris8EasyJson3SetIPKcEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET__ZN5doris8EasyJson3SetINS0_22ComplexTypeInitializerEEES0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_| Line | Count | Source |  | 135 | 1 | EasyJson EasyJson::Set(const string& key, T val) { |  | 136 | 1 |     return (Get(key) = val); |  | 137 | 1 | } | 
 | 
| 138 |  | template EasyJson EasyJson::Set(const string& key, bool val); | 
| 139 |  | template EasyJson EasyJson::Set(const string& key, int32_t val); | 
| 140 |  | template EasyJson EasyJson::Set(const string& key, int64_t val); | 
| 141 |  | template EasyJson EasyJson::Set(const string& key, uint32_t val); | 
| 142 |  | template EasyJson EasyJson::Set(const string& key, uint64_t val); | 
| 143 |  | template EasyJson EasyJson::Set(const string& key, double val); | 
| 144 |  | template EasyJson EasyJson::Set(const string& key, const char* val); | 
| 145 |  | template EasyJson EasyJson::Set(const string& key, EasyJson::ComplexTypeInitializer val); | 
| 146 |  |  | 
| 147 | 0 | EasyJson EasyJson::Set(int index, const string& val) { | 
| 148 | 0 |     return (Get(index) = val); | 
| 149 | 0 | } | 
| 150 |  | template <typename T> | 
| 151 | 0 | EasyJson EasyJson::Set(int index, T val) { | 
| 152 | 0 |     return (Get(index) = val); | 
| 153 | 0 | } Unexecuted instantiation: _ZN5doris8EasyJson3SetIbEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetIiEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetIlEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetIjEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetImEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetIdEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetIPKcEES0_iT_Unexecuted instantiation: _ZN5doris8EasyJson3SetINS0_22ComplexTypeInitializerEEES0_iT_ | 
| 154 |  | template EasyJson EasyJson::Set(int index, bool val); | 
| 155 |  | template EasyJson EasyJson::Set(int index, int32_t val); | 
| 156 |  | template EasyJson EasyJson::Set(int index, int64_t val); | 
| 157 |  | template EasyJson EasyJson::Set(int index, uint32_t val); | 
| 158 |  | template EasyJson EasyJson::Set(int index, uint64_t val); | 
| 159 |  | template EasyJson EasyJson::Set(int index, double val); | 
| 160 |  | template EasyJson EasyJson::Set(int index, const char* val); | 
| 161 |  | template EasyJson EasyJson::Set(int index, EasyJson::ComplexTypeInitializer val); | 
| 162 |  |  | 
| 163 | 1 | EasyJson EasyJson::PushBack(const string& val) { | 
| 164 | 1 |     if (!value_->IsArray()) { | 
| 165 | 1 |         value_->SetArray(); | 
| 166 | 1 |     } | 
| 167 | 1 |     Value push_val(val.c_str(), alloc_->allocator()); | 
| 168 | 1 |     value_->PushBack(push_val, alloc_->allocator()); | 
| 169 | 1 |     return EasyJson(&(*value_)[value_->Size() - 1], alloc_); | 
| 170 | 1 | } | 
| 171 |  | template <typename T> | 
| 172 | 2 | EasyJson EasyJson::PushBack(T val) { | 
| 173 | 2 |     if (!value_->IsArray()) { | 
| 174 | 0 |         value_->SetArray(); | 
| 175 | 0 |     } | 
| 176 | 2 |     value_->PushBack(val, alloc_->allocator()); | 
| 177 | 2 |     return EasyJson(&(*value_)[value_->Size() - 1], alloc_); | 
| 178 | 2 | } Unexecuted instantiation: _ZN5doris8EasyJson8PushBackIbEES0_T__ZN5doris8EasyJson8PushBackIiEES0_T_| Line | Count | Source |  | 172 | 2 | EasyJson EasyJson::PushBack(T val) { |  | 173 | 2 |     if (!value_->IsArray()) { |  | 174 | 0 |         value_->SetArray(); |  | 175 | 0 |     } |  | 176 | 2 |     value_->PushBack(val, alloc_->allocator()); |  | 177 | 2 |     return EasyJson(&(*value_)[value_->Size() - 1], alloc_); |  | 178 | 2 | } | 
Unexecuted instantiation: _ZN5doris8EasyJson8PushBackIlEES0_T_Unexecuted instantiation: _ZN5doris8EasyJson8PushBackIjEES0_T_Unexecuted instantiation: _ZN5doris8EasyJson8PushBackImEES0_T_Unexecuted instantiation: _ZN5doris8EasyJson8PushBackIdEES0_T_ | 
| 179 |  | template EasyJson EasyJson::PushBack(bool val); | 
| 180 |  | template EasyJson EasyJson::PushBack(int32_t val); | 
| 181 |  | template EasyJson EasyJson::PushBack(int64_t val); | 
| 182 |  | template EasyJson EasyJson::PushBack(uint32_t val); | 
| 183 |  | template EasyJson EasyJson::PushBack(uint64_t val); | 
| 184 |  | template EasyJson EasyJson::PushBack(double val); | 
| 185 |  | template <> | 
| 186 | 0 | EasyJson EasyJson::PushBack(const char* val) { | 
| 187 | 0 |     if (!value_->IsArray()) { | 
| 188 | 0 |         value_->SetArray(); | 
| 189 | 0 |     } | 
| 190 | 0 |     Value push_val(val, alloc_->allocator()); | 
| 191 | 0 |     value_->PushBack(push_val, alloc_->allocator()); | 
| 192 | 0 |     return EasyJson(&(*value_)[value_->Size() - 1], alloc_); | 
| 193 | 0 | } | 
| 194 |  | template <> | 
| 195 | 1 | EasyJson EasyJson::PushBack(EasyJson::ComplexTypeInitializer val) { | 
| 196 | 1 |     if (!value_->IsArray()) { | 
| 197 | 0 |         value_->SetArray(); | 
| 198 | 0 |     } | 
| 199 | 1 |     Value push_val; | 
| 200 | 1 |     if (val == kObject) { | 
| 201 | 1 |         push_val.SetObject(); | 
| 202 | 1 |     } else if (val == kArray) { | 
| 203 | 0 |         push_val.SetArray(); | 
| 204 | 0 |     } else { | 
| 205 | 0 |         throw Exception(Status::FatalError("Unknown initializer type")); | 
| 206 | 0 |     } | 
| 207 | 1 |     value_->PushBack(push_val, alloc_->allocator()); | 
| 208 | 1 |     return EasyJson(&(*value_)[value_->Size() - 1], alloc_); | 
| 209 | 1 | } | 
| 210 |  |  | 
| 211 | 2 | string EasyJson::ToString() const { | 
| 212 | 2 |     rapidjson::StringBuffer buffer; | 
| 213 | 2 |     rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | 
| 214 | 2 |     value_->Accept(writer); | 
| 215 | 2 |     return buffer.GetString(); | 
| 216 | 2 | } | 
| 217 |  |  | 
| 218 |  | EasyJson::EasyJson(Value* value, std::shared_ptr<EasyJsonAllocator> alloc) | 
| 219 | 21 |         : alloc_(std::move(alloc)), value_(value) {} | 
| 220 |  |  | 
| 221 |  | } // namespace doris |