src/core/lib/json/json.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_JSON_JSON_H
20 #define GRPC_CORE_LIB_JSON_JSON_H
21 
23 
24 #include <map>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 #include "absl/strings/string_view.h"
30 
32 
33 namespace grpc_core {
34 
35 // A JSON value, which can be any one of object, array, string,
36 // number, true, false, or null.
37 class Json {
38  public:
39  // TODO(roth): Currently, numbers are stored internally as strings,
40  // which makes the API a bit cumbersome to use. When we have time,
41  // consider whether there's a better alternative (e.g., maybe storing
42  // each numeric type as the native C++ type and automatically converting
43  // to string as needed).
44  enum class Type {
45  JSON_NULL,
46  JSON_TRUE,
47  JSON_FALSE,
48  NUMBER,
49  STRING,
50  OBJECT,
51  ARRAY
52  };
53 
54  using Object = std::map<std::string, Json>;
55  using Array = std::vector<Json>;
56 
57  // Parses JSON string from json_str. On error, sets *error.
59 
60  Json() = default;
61 
62  // Copyable.
63  Json(const Json& other) { CopyFrom(other); }
64  Json& operator=(const Json& other) {
65  CopyFrom(other);
66  return *this;
67  }
68 
69  // Moveable.
70  Json(Json&& other) noexcept { MoveFrom(std::move(other)); }
71  Json& operator=(Json&& other) noexcept {
72  MoveFrom(std::move(other));
73  return *this;
74  }
75 
76  // Construct from copying a string.
77  // If is_number is true, the type will be NUMBER instead of STRING.
78  // NOLINTNEXTLINE(google-explicit-constructor)
79  Json(const std::string& string, bool is_number = false)
80  : type_(is_number ? Type::NUMBER : Type::STRING), string_value_(string) {}
81  Json& operator=(const std::string& string) {
84  return *this;
85  }
86 
87  // Same thing for C-style strings, both const and mutable.
88  // NOLINTNEXTLINE(google-explicit-constructor)
89  Json(const char* string, bool is_number = false)
90  : Json(std::string(string), is_number) {}
91  Json& operator=(const char* string) {
92  *this = std::string(string);
93  return *this;
94  }
95  // NOLINTNEXTLINE(google-explicit-constructor)
96  Json(char* string, bool is_number = false)
97  : Json(std::string(string), is_number) {}
98  Json& operator=(char* string) {
99  *this = std::string(string);
100  return *this;
101  }
102 
103  // Construct by moving a string.
104  // NOLINTNEXTLINE(google-explicit-constructor)
105  Json(std::string&& string)
106  : type_(Type::STRING), string_value_(std::move(string)) {}
109  string_value_ = std::move(string);
110  return *this;
111  }
112 
113  // Construct from bool.
114  // NOLINTNEXTLINE(google-explicit-constructor)
115  Json(bool b) : type_(b ? Type::JSON_TRUE : Type::JSON_FALSE) {}
116  Json& operator=(bool b) {
118  return *this;
119  }
120 
121  // Construct from any numeric type.
122  template <typename NumericType>
123  // NOLINTNEXTLINE(google-explicit-constructor)
124  Json(NumericType number)
125  : type_(Type::NUMBER), string_value_(std::to_string(number)) {}
126  template <typename NumericType>
127  Json& operator=(NumericType number) {
130  return *this;
131  }
132 
133  // Construct by copying object.
134  // NOLINTNEXTLINE(google-explicit-constructor)
135  Json(const Object& object) : type_(Type::OBJECT), object_value_(object) {}
136  Json& operator=(const Object& object) {
138  object_value_ = object;
139  return *this;
140  }
141 
142  // Construct by moving object.
143  // NOLINTNEXTLINE(google-explicit-constructor)
144  Json(Object&& object)
145  : type_(Type::OBJECT), object_value_(std::move(object)) {}
146  Json& operator=(Object&& object) {
148  object_value_ = std::move(object);
149  return *this;
150  }
151 
152  // Construct by copying array.
153  // NOLINTNEXTLINE(google-explicit-constructor)
154  Json(const Array& array) : type_(Type::ARRAY), array_value_(array) {}
156  type_ = Type::ARRAY;
158  return *this;
159  }
160 
161  // Construct by moving array.
162  // NOLINTNEXTLINE(google-explicit-constructor)
165  type_ = Type::ARRAY;
167  return *this;
168  }
169 
170  // Dumps JSON from value to string form.
171  std::string Dump(int indent = 0) const;
172 
173  // Accessor methods.
174  Type type() const { return type_; }
175  const std::string& string_value() const { return string_value_; }
177  const Object& object_value() const { return object_value_; }
179  const Array& array_value() const { return array_value_; }
181 
182  bool operator==(const Json& other) const {
183  if (type_ != other.type_) return false;
184  switch (type_) {
185  case Type::NUMBER:
186  case Type::STRING:
187  if (string_value_ != other.string_value_) return false;
188  break;
189  case Type::OBJECT:
190  if (object_value_ != other.object_value_) return false;
191  break;
192  case Type::ARRAY:
193  if (array_value_ != other.array_value_) return false;
194  break;
195  default:
196  break;
197  }
198  return true;
199  }
200 
201  bool operator!=(const Json& other) const { return !(*this == other); }
202 
203  private:
204  void CopyFrom(const Json& other) {
205  type_ = other.type_;
206  switch (type_) {
207  case Type::NUMBER:
208  case Type::STRING:
210  break;
211  case Type::OBJECT:
213  break;
214  case Type::ARRAY:
215  array_value_ = other.array_value_;
216  break;
217  default:
218  break;
219  }
220  }
221 
222  void MoveFrom(Json&& other) {
223  type_ = other.type_;
224  other.type_ = Type::JSON_NULL;
225  switch (type_) {
226  case Type::NUMBER:
227  case Type::STRING:
228  string_value_ = std::move(other.string_value_);
229  break;
230  case Type::OBJECT:
231  object_value_ = std::move(other.object_value_);
232  break;
233  case Type::ARRAY:
234  array_value_ = std::move(other.array_value_);
235  break;
236  default:
237  break;
238  }
239  }
240 
245 };
246 
247 } // namespace grpc_core
248 
249 #endif /* GRPC_CORE_LIB_JSON_JSON_H */
grpc_core::Json::Json
Json(const Object &object)
Definition: src/core/lib/json/json.h:135
grpc_core::Json::Type::JSON_TRUE
@ JSON_TRUE
grpc_core::Json::type
Type type() const
Definition: src/core/lib/json/json.h:174
grpc_core::Json::CopyFrom
void CopyFrom(const Json &other)
Definition: src/core/lib/json/json.h:204
grpc_core::Json::operator=
Json & operator=(Object &&object)
Definition: src/core/lib/json/json.h:146
grpc_core::Json::operator!=
bool operator!=(const Json &other) const
Definition: src/core/lib/json/json.h:201
grpc_core::Json::Type::OBJECT
@ OBJECT
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Json::Json
Json(Json &&other) noexcept
Definition: src/core/lib/json/json.h:70
grpc_core::Json::Json
Json(NumericType number)
Definition: src/core/lib/json/json.h:124
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
array
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern array
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/array.c:111
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::Json::MoveFrom
void MoveFrom(Json &&other)
Definition: src/core/lib/json/json.h:222
grpc_core::Json::object_value
const Object & object_value() const
Definition: src/core/lib/json/json.h:177
grpc_core::Json::Json
Json(Array &&array)
Definition: src/core/lib/json/json.h:163
grpc_core::Json::Json
Json(char *string, bool is_number=false)
Definition: src/core/lib/json/json.h:96
grpc_core::Json::mutable_object
Object * mutable_object()
Definition: src/core/lib/json/json.h:178
python_utils.upload_rbe_results.indent
indent
Definition: upload_rbe_results.py:183
grpc_core::Json::Type::JSON_FALSE
@ JSON_FALSE
grpc_core::Json::operator==
bool operator==(const Json &other) const
Definition: src/core/lib/json/json.h:182
grpc_core::Json::operator=
Json & operator=(const Json &other)
Definition: src/core/lib/json/json.h:64
grpc_core::Json::Json
Json()=default
grpc_core::Json::operator=
Json & operator=(bool b)
Definition: src/core/lib/json/json.h:116
grpc_core::Json::object_value_
Object object_value_
Definition: src/core/lib/json/json.h:243
grpc_core::Json::array_value
const Array & array_value() const
Definition: src/core/lib/json/json.h:179
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
Array
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:258
array
Definition: undname.c:101
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc_core::Json::operator=
Json & operator=(const Array &array)
Definition: src/core/lib/json/json.h:155
grpc_core::Json::Json
Json(Object &&object)
Definition: src/core/lib/json/json.h:144
grpc_core::Json::Json
Json(const std::string &string, bool is_number=false)
Definition: src/core/lib/json/json.h:79
grpc_core::Json::operator=
Json & operator=(NumericType number)
Definition: src/core/lib/json/json.h:127
grpc_core::Json::operator=
Json & operator=(const Object &object)
Definition: src/core/lib/json/json.h:136
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
grpc_core::Json::Type::NUMBER
@ NUMBER
error.h
grpc_core::Json::Type::ARRAY
@ ARRAY
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::Json::array_value_
Array array_value_
Definition: src/core/lib/json/json.h:244
grpc_core::Json::Parse
static Json Parse(absl::string_view json_str, grpc_error_handle *error)
Definition: json_reader.cc:899
grpc_core::Json::operator=
Json & operator=(Array &&array)
Definition: src/core/lib/json/json.h:164
grpc_core::Json::Object
std::map< std::string, Json > Object
Definition: src/core/lib/json/json.h:54
grpc_core::Json::operator=
Json & operator=(std::string &&string)
Definition: src/core/lib/json/json.h:107
grpc_core::Json::Json
Json(std::string &&string)
Definition: src/core/lib/json/json.h:105
grpc_core::Json::Type::JSON_NULL
@ JSON_NULL
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::Json::string_value
const std::string & string_value() const
Definition: src/core/lib/json/json.h:175
grpc_core::Json::operator=
Json & operator=(Json &&other) noexcept
Definition: src/core/lib/json/json.h:71
grpc_core::Json::operator=
Json & operator=(const char *string)
Definition: src/core/lib/json/json.h:91
grpc_core::Json::Json
Json(const Json &other)
Definition: src/core/lib/json/json.h:63
grpc_core::Json::string_value_
std::string string_value_
Definition: src/core/lib/json/json.h:242
grpc_core::Json::operator=
Json & operator=(const std::string &string)
Definition: src/core/lib/json/json.h:81
grpc_core::Json::mutable_string_value
std::string * mutable_string_value()
Definition: src/core/lib/json/json.h:176
grpc_core::Json::type_
Type type_
Definition: src/core/lib/json/json.h:241
grpc_core::Json::Json
Json(bool b)
Definition: src/core/lib/json/json.h:115
grpc_core::Json::mutable_array
Array * mutable_array()
Definition: src/core/lib/json/json.h:180
grpc_error
Definition: error_internal.h:42
grpc_core::Json::Json
Json(const char *string, bool is_number=false)
Definition: src/core/lib/json/json.h:89
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc_core::Json::Json
Json(const Array &array)
Definition: src/core/lib/json/json.h:154
grpc_core::Json::Dump
std::string Dump(int indent=0) const
Definition: json_writer.cc:336
grpc_core::Json::Type::STRING
@ STRING
grpc_core::Json::operator=
Json & operator=(char *string)
Definition: src/core/lib/json/json.h:98
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:11