summaryrefslogtreecommitdiff
path: root/grpc/src/core/lib/json/json.h
blob: fc09d3a7255a2753f572061b7537f0d25c2f9ea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef GRPC_CORE_LIB_JSON_JSON_H
#define GRPC_CORE_LIB_JSON_JSON_H

#include <grpc/support/port_platform.h>

#include <stdlib.h>

#include <map>
#include <string>
#include <vector>

#include "absl/strings/string_view.h"

#include "src/core/lib/iomgr/error.h"

namespace grpc_core {

// A JSON value, which can be any one of object, array, string,
// number, true, false, or null.
class Json {
 public:
  // TODO(roth): Currently, numbers are stored internally as strings,
  // which makes the API a bit cumbersome to use. When we have time,
  // consider whether there's a better alternative (e.g., maybe storing
  // each numeric type as the native C++ type and automatically converting
  // to string as needed).
  enum class Type {
    JSON_NULL,
    JSON_TRUE,
    JSON_FALSE,
    NUMBER,
    STRING,
    OBJECT,
    ARRAY
  };

  using Object = std::map<std::string, Json>;
  using Array = std::vector<Json>;

  // Parses JSON string from json_str.  On error, sets *error.
  static Json Parse(absl::string_view json_str, grpc_error_handle* error);

  Json() = default;

  // Copyable.
  Json(const Json& other) { CopyFrom(other); }
  Json& operator=(const Json& other) {
    CopyFrom(other);
    return *this;
  }

  // Moveable.
  Json(Json&& other) noexcept { MoveFrom(std::move(other)); }
  Json& operator=(Json&& other) noexcept {
    MoveFrom(std::move(other));
    return *this;
  }

  // Construct from copying a string.
  // If is_number is true, the type will be NUMBER instead of STRING.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(const std::string& string, bool is_number = false)
      : type_(is_number ? Type::NUMBER : Type::STRING), string_value_(string) {}
  Json& operator=(const std::string& string) {
    type_ = Type::STRING;
    string_value_ = string;
    return *this;
  }

  // Same thing for C-style strings, both const and mutable.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(const char* string, bool is_number = false)
      : Json(std::string(string), is_number) {}
  Json& operator=(const char* string) {
    *this = std::string(string);
    return *this;
  }
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(char* string, bool is_number = false)
      : Json(std::string(string), is_number) {}
  Json& operator=(char* string) {
    *this = std::string(string);
    return *this;
  }

  // Construct by moving a string.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(std::string&& string)
      : type_(Type::STRING), string_value_(std::move(string)) {}
  Json& operator=(std::string&& string) {
    type_ = Type::STRING;
    string_value_ = std::move(string);
    return *this;
  }

  // Construct from bool.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(bool b) : type_(b ? Type::JSON_TRUE : Type::JSON_FALSE) {}
  Json& operator=(bool b) {
    type_ = b ? Type::JSON_TRUE : Type::JSON_FALSE;
    return *this;
  }

  // Construct from any numeric type.
  template <typename NumericType>
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(NumericType number)
      : type_(Type::NUMBER), string_value_(std::to_string(number)) {}
  template <typename NumericType>
  Json& operator=(NumericType number) {
    type_ = Type::NUMBER;
    string_value_ = std::to_string(number);
    return *this;
  }

  // Construct by copying object.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(const Object& object) : type_(Type::OBJECT), object_value_(object) {}
  Json& operator=(const Object& object) {
    type_ = Type::OBJECT;
    object_value_ = object;
    return *this;
  }

  // Construct by moving object.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(Object&& object)
      : type_(Type::OBJECT), object_value_(std::move(object)) {}
  Json& operator=(Object&& object) {
    type_ = Type::OBJECT;
    object_value_ = std::move(object);
    return *this;
  }

  // Construct by copying array.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(const Array& array) : type_(Type::ARRAY), array_value_(array) {}
  Json& operator=(const Array& array) {
    type_ = Type::ARRAY;
    array_value_ = array;
    return *this;
  }

  // Construct by moving array.
  // NOLINTNEXTLINE(google-explicit-constructor)
  Json(Array&& array) : type_(Type::ARRAY), array_value_(std::move(array)) {}
  Json& operator=(Array&& array) {
    type_ = Type::ARRAY;
    array_value_ = std::move(array);
    return *this;
  }

  // Dumps JSON from value to string form.
  std::string Dump(int indent = 0) const;

  // Accessor methods.
  Type type() const { return type_; }
  const std::string& string_value() const { return string_value_; }
  std::string* mutable_string_value() { return &string_value_; }
  const Object& object_value() const { return object_value_; }
  Object* mutable_object() { return &object_value_; }
  const Array& array_value() const { return array_value_; }
  Array* mutable_array() { return &array_value_; }

  bool operator==(const Json& other) const {
    if (type_ != other.type_) return false;
    switch (type_) {
      case Type::NUMBER:
      case Type::STRING:
        if (string_value_ != other.string_value_) return false;
        break;
      case Type::OBJECT:
        if (object_value_ != other.object_value_) return false;
        break;
      case Type::ARRAY:
        if (array_value_ != other.array_value_) return false;
        break;
      default:
        break;
    }
    return true;
  }

  bool operator!=(const Json& other) const { return !(*this == other); }

 private:
  void CopyFrom(const Json& other) {
    type_ = other.type_;
    switch (type_) {
      case Type::NUMBER:
      case Type::STRING:
        string_value_ = other.string_value_;
        break;
      case Type::OBJECT:
        object_value_ = other.object_value_;
        break;
      case Type::ARRAY:
        array_value_ = other.array_value_;
        break;
      default:
        break;
    }
  }

  void MoveFrom(Json&& other) {
    type_ = other.type_;
    other.type_ = Type::JSON_NULL;
    switch (type_) {
      case Type::NUMBER:
      case Type::STRING:
        string_value_ = std::move(other.string_value_);
        break;
      case Type::OBJECT:
        object_value_ = std::move(other.object_value_);
        break;
      case Type::ARRAY:
        array_value_ = std::move(other.array_value_);
        break;
      default:
        break;
    }
  }

  Type type_ = Type::JSON_NULL;
  std::string string_value_;
  Object object_value_;
  Array array_value_;
};

}  // namespace grpc_core

#endif /* GRPC_CORE_LIB_JSON_JSON_H */