xgboost
json.h
Go to the documentation of this file.
1 
4 #ifndef XGBOOST_JSON_H_
5 #define XGBOOST_JSON_H_
6 
8 #include <xgboost/logging.h>
9 #include <xgboost/parameter.h>
10 #include <xgboost/string_view.h>
11 
12 #include <functional>
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 namespace xgboost {
20 
21 class Json;
22 class JsonReader;
23 class JsonWriter;
24 
25 class Value {
26  private:
27  mutable class IntrusivePtrCell ref_;
28  friend IntrusivePtrCell &
29  IntrusivePtrRefCount(xgboost::Value const *t) noexcept {
30  return t->ref_;
31  }
32 
33  public:
35  enum class ValueKind {
36  kString,
37  kNumber,
38  kInteger,
39  kObject, // std::map
40  kArray, // std::vector
41  kBoolean,
42  kNull,
43  // typed array for ubjson
45  kU8Array,
46  kI32Array,
47  kI64Array
48  };
49 
50  explicit Value(ValueKind _kind) : kind_{_kind} {}
51 
52  ValueKind Type() const { return kind_; }
53  virtual ~Value() = default;
54 
55  virtual void Save(JsonWriter* writer) const = 0;
56 
57  virtual Json& operator[](std::string const& key);
58  virtual Json& operator[](int ind);
59 
60  virtual bool operator==(Value const& rhs) const = 0;
61 #if !defined(__APPLE__)
62  virtual Value& operator=(Value const& rhs) = delete;
63 #endif // !defined(__APPLE__)
64 
65  std::string TypeStr() const;
66 
67  private:
68  ValueKind kind_;
69 };
70 
71 template <typename T>
72 bool IsA(Value const* value) {
73  return T::IsClassOf(value);
74 }
75 
76 template <typename T, typename U>
77 T* Cast(U* value) {
78  if (IsA<T>(value)) {
79  return dynamic_cast<T*>(value);
80  } else {
81  LOG(FATAL) << "Invalid cast, from " + value->TypeStr() + " to " + T().TypeStr();
82  }
83  return dynamic_cast<T*>(value); // suppress compiler warning.
84 }
85 
86 class JsonString : public Value {
87  std::string str_;
88 
89  public:
90  JsonString() : Value(ValueKind::kString) {}
91  JsonString(std::string const& str) : // NOLINT
92  Value(ValueKind::kString), str_{str} {}
93  JsonString(std::string&& str) noexcept : // NOLINT
94  Value(ValueKind::kString), str_{std::forward<std::string>(str)} {}
95  JsonString(JsonString&& str) noexcept : Value(ValueKind::kString) { // NOLINT
96  std::swap(str.str_, this->str_);
97  }
98 
99  void Save(JsonWriter* writer) const override;
100 
101  std::string const& GetString() && { return str_; }
102  std::string const& GetString() const & { return str_; }
103  std::string& GetString() & { return str_; }
104 
105  bool operator==(Value const& rhs) const override;
106 
107  static bool IsClassOf(Value const* value) {
108  return value->Type() == ValueKind::kString;
109  }
110 };
111 
112 class JsonArray : public Value {
113  std::vector<Json> vec_;
114 
115  public:
116  JsonArray() : Value(ValueKind::kArray) {}
117  JsonArray(std::vector<Json>&& arr) noexcept // NOLINT
118  : Value(ValueKind::kArray), vec_{std::forward<std::vector<Json>>(arr)} {}
119  JsonArray(std::vector<Json> const& arr) : // NOLINT
120  Value(ValueKind::kArray), vec_{arr} {}
121  JsonArray(JsonArray const& that) = delete;
122  JsonArray(JsonArray && that) noexcept;
123 
124  void Save(JsonWriter* writer) const override;
125 
126  Json& operator[](int ind) override { return vec_.at(ind); }
127  // silent the partial oveeridden warning
128  Json& operator[](std::string const& key) override { return Value::operator[](key); }
129 
130  std::vector<Json> const& GetArray() && { return vec_; }
131  std::vector<Json> const& GetArray() const & { return vec_; }
132  std::vector<Json>& GetArray() & { return vec_; }
133 
134  bool operator==(Value const& rhs) const override;
135 
136  static bool IsClassOf(Value const* value) {
137  return value->Type() == ValueKind::kArray;
138  }
139 };
140 
147 template <typename T, Value::ValueKind kind>
148 class JsonTypedArray : public Value {
149  std::vector<T> vec_;
150 
151  public:
152  using Type = T;
153 
154  JsonTypedArray() : Value(kind) {}
155  explicit JsonTypedArray(size_t n) : Value(kind) { vec_.resize(n); }
156  JsonTypedArray(JsonTypedArray&& that) noexcept : Value{kind}, vec_{std::move(that.vec_)} {}
157 
158  bool operator==(Value const& rhs) const override;
159 
160  void Set(size_t i, T v) { vec_[i] = v; }
161  size_t Size() const { return vec_.size(); }
162 
163  void Save(JsonWriter* writer) const override;
164 
165  std::vector<T> const& GetArray() && { return vec_; }
166  std::vector<T> const& GetArray() const& { return vec_; }
167  std::vector<T>& GetArray() & { return vec_; }
168 
169  static bool IsClassOf(Value const* value) { return value->Type() == kind; }
170 };
171 
188 
189 class JsonObject : public Value {
190  public:
191  using Map = std::map<std::string, Json, std::less<>>;
192 
193  private:
194  Map object_;
195 
196  public:
197  JsonObject() : Value(ValueKind::kObject) {}
198  JsonObject(Map&& object) noexcept; // NOLINT
199  JsonObject(JsonObject const& that) = delete;
200  JsonObject(JsonObject&& that) noexcept;
201 
202  void Save(JsonWriter* writer) const override;
203 
204  // silent the partial oveeridden warning
205  Json& operator[](int ind) override { return Value::operator[](ind); }
206  Json& operator[](std::string const& key) override { return object_[key]; }
207 
208  Map const& GetObject() && { return object_; }
209  Map const& GetObject() const& { return object_; }
210  Map& GetObject() & { return object_; }
211 
212  bool operator==(Value const& rhs) const override;
213 
214  static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kObject; }
215  ~JsonObject() override = default;
216 };
217 
218 class JsonNumber : public Value {
219  public:
220  using Float = float;
221 
222  private:
223  Float number_ { 0 };
224 
225  public:
226  JsonNumber() : Value(ValueKind::kNumber) {}
227  template <typename FloatT,
228  typename std::enable_if<std::is_same<FloatT, Float>::value>::type* = nullptr>
229  JsonNumber(FloatT value) : Value(ValueKind::kNumber) { // NOLINT
230  number_ = value;
231  }
232  template <typename FloatT,
233  typename std::enable_if<std::is_same<FloatT, double>::value>::type* = nullptr>
234  JsonNumber(FloatT value) : Value{ValueKind::kNumber}, // NOLINT
235  number_{static_cast<Float>(value)} {}
236  JsonNumber(JsonNumber const& that) = delete;
237  JsonNumber(JsonNumber&& that) noexcept : Value{ValueKind::kNumber}, number_{that.number_} {}
238 
239  void Save(JsonWriter* writer) const override;
240 
241  Float const& GetNumber() && { return number_; }
242  Float const& GetNumber() const & { return number_; }
243  Float& GetNumber() & { return number_; }
244 
245  bool operator==(Value const& rhs) const override;
246 
247  static bool IsClassOf(Value const* value) {
248  return value->Type() == ValueKind::kNumber;
249  }
250 };
251 
252 class JsonInteger : public Value {
253  public:
254  using Int = int64_t;
255 
256  private:
257  Int integer_ {0};
258 
259  public:
260  JsonInteger() : Value(ValueKind::kInteger) {} // NOLINT
261  template <typename IntT,
262  typename std::enable_if<std::is_same<IntT, Int>::value>::type* = nullptr>
263  JsonInteger(IntT value) : Value(ValueKind::kInteger), integer_{value} {} // NOLINT
264  template <typename IntT,
265  typename std::enable_if<std::is_same<IntT, size_t>::value>::type* = nullptr>
266  JsonInteger(IntT value) : Value(ValueKind::kInteger), // NOLINT
267  integer_{static_cast<Int>(value)} {}
268  template <typename IntT,
269  typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* = nullptr>
270  JsonInteger(IntT value) : Value(ValueKind::kInteger), // NOLINT
271  integer_{static_cast<Int>(value)} {}
272  template <typename IntT,
273  typename std::enable_if<
274  std::is_same<IntT, uint32_t>::value &&
275  !std::is_same<std::size_t, uint32_t>::value>::type * = nullptr>
276  JsonInteger(IntT value) // NOLINT
277  : Value(ValueKind::kInteger),
278  integer_{static_cast<Int>(value)} {}
279 
280  JsonInteger(JsonInteger &&that) noexcept
281  : Value{ValueKind::kInteger}, integer_{that.integer_} {}
282 
283  bool operator==(Value const& rhs) const override;
284 
285  Int const& GetInteger() && { return integer_; }
286  Int const& GetInteger() const & { return integer_; }
287  Int& GetInteger() & { return integer_; }
288  void Save(JsonWriter* writer) const override;
289 
290  static bool IsClassOf(Value const* value) {
291  return value->Type() == ValueKind::kInteger;
292  }
293 };
294 
295 class JsonNull : public Value {
296  public:
297  JsonNull() : Value(ValueKind::kNull) {}
298  JsonNull(std::nullptr_t) : Value(ValueKind::kNull) {} // NOLINT
299  JsonNull(JsonNull&&) noexcept : Value(ValueKind::kNull) {}
300 
301  void Save(JsonWriter* writer) const override;
302 
303  bool operator==(Value const& rhs) const override;
304 
305  static bool IsClassOf(Value const* value) {
306  return value->Type() == ValueKind::kNull;
307  }
308 };
309 
311 class JsonBoolean : public Value {
312  bool boolean_ = false;
313 
314  public:
315  JsonBoolean() : Value(ValueKind::kBoolean) {} // NOLINT
316  // Ambigious with JsonNumber.
317  template <typename Bool,
318  typename std::enable_if<
319  std::is_same<Bool, bool>::value ||
320  std::is_same<Bool, bool const>::value>::type* = nullptr>
321  JsonBoolean(Bool value) : // NOLINT
322  Value(ValueKind::kBoolean), boolean_{value} {}
323  JsonBoolean(JsonBoolean&& value) noexcept: // NOLINT
324  Value(ValueKind::kBoolean), boolean_{value.boolean_} {}
325 
326  void Save(JsonWriter* writer) const override;
327 
328  bool const& GetBoolean() && { return boolean_; }
329  bool const& GetBoolean() const & { return boolean_; }
330  bool& GetBoolean() & { return boolean_; }
331 
332  bool operator==(Value const& rhs) const override;
333 
334  static bool IsClassOf(Value const* value) {
335  return value->Type() == ValueKind::kBoolean;
336  }
337 };
338 
356 class Json {
357  public:
362  static Json Load(StringView str, std::ios::openmode mode = std::ios::in);
364  static Json Load(JsonReader* reader);
369  static void Dump(Json json, std::string* out, std::ios::openmode mode = std::ios::out);
370  static void Dump(Json json, std::vector<char>* out, std::ios::openmode mode = std::ios::out);
372  static void Dump(Json json, JsonWriter* writer);
373 
374  Json() : ptr_{new JsonNull} {}
375 
376  // number
377  explicit Json(JsonNumber number) : ptr_{new JsonNumber(std::move(number))} {}
379  ptr_.reset(new JsonNumber(std::move(number)));
380  return *this;
381  }
382  // integer
383  explicit Json(JsonInteger integer) : ptr_{new JsonInteger(std::move(integer))} {}
385  ptr_.reset(new JsonInteger(std::move(integer)));
386  return *this;
387  }
388  // array
389  explicit Json(JsonArray&& list) : ptr_{new JsonArray(std::forward<JsonArray>(list))} {}
391  ptr_.reset(new JsonArray(std::forward<JsonArray>(array)));
392  return *this;
393  }
394  // typed array
395  template <typename T, Value::ValueKind kind>
396  explicit Json(JsonTypedArray<T, kind>&& list)
397  : ptr_{new JsonTypedArray<T, kind>(std::forward<JsonTypedArray<T, kind>>(list))} {}
398  template <typename T, Value::ValueKind kind>
400  ptr_.reset(new JsonTypedArray<T, kind>(std::forward<JsonTypedArray<T, kind>>(array)));
401  return *this;
402  }
403  // object
404  explicit Json(JsonObject&& object) : ptr_{new JsonObject(std::forward<JsonObject>(object))} {}
405  Json& operator=(JsonObject&& object) {
406  ptr_.reset(new JsonObject(std::forward<JsonObject>(object)));
407  return *this;
408  }
409  // string
410  explicit Json(JsonString&& str) : ptr_{new JsonString(std::forward<JsonString>(str))} {}
412  ptr_.reset(new JsonString(std::forward<JsonString>(str)));
413  return *this;
414  }
415  // bool
416  explicit Json(JsonBoolean boolean) :
417  ptr_{new JsonBoolean(std::move(boolean))} {}
419  ptr_.reset(new JsonBoolean(std::move(boolean)));
420  return *this;
421  }
422  // null
423  explicit Json(JsonNull null) :
424  ptr_{new JsonNull(std::move(null))} {}
426  ptr_.reset(new JsonNull(std::move(null)));
427  return *this;
428  }
429 
430  // copy
431  Json(Json const& other) = default;
432  Json& operator=(Json const& other) = default;
433  // move
434  Json(Json &&other) noexcept { std::swap(this->ptr_, other.ptr_); }
435  Json &operator=(Json &&other) noexcept {
436  std::swap(this->ptr_, other.ptr_);
437  return *this;
438  }
439 
441  Json& operator[](std::string const & key) const { return (*ptr_)[key]; }
443  Json& operator[](int ind) const { return (*ptr_)[ind]; }
444 
446  Value const& GetValue() const & { return *ptr_; }
447  Value const& GetValue() && { return *ptr_; }
448  Value& GetValue() & { return *ptr_; }
449 
450  bool operator==(Json const& rhs) const {
451  return *ptr_ == *(rhs.ptr_);
452  }
453 
454  friend std::ostream& operator<<(std::ostream& os, Json const& j) {
455  std::string str;
456  Json::Dump(j, &str);
457  os << str;
458  return os;
459  }
460 
461  IntrusivePtr<Value> const& Ptr() const { return ptr_; }
462 
463  private:
464  IntrusivePtr<Value> ptr_;
465 };
466 
476 template <typename T>
477 bool IsA(Json const& j) {
478  auto const& v = j.GetValue();
479  return IsA<T>(&v);
480 }
481 
482 namespace detail {
483 // Number
484 template <typename T,
485  typename std::enable_if<
486  std::is_same<T, JsonNumber>::value>::type* = nullptr>
487 JsonNumber::Float& GetImpl(T& val) { // NOLINT
488  return val.GetNumber();
489 }
490 template <typename T,
491  typename std::enable_if<
492  std::is_same<T, JsonNumber const>::value>::type* = nullptr>
493 JsonNumber::Float const& GetImpl(T& val) { // NOLINT
494  return val.GetNumber();
495 }
496 
497 // Integer
498 template <typename T,
499  typename std::enable_if<
500  std::is_same<T, JsonInteger>::value>::type* = nullptr>
501 JsonInteger::Int& GetImpl(T& val) { // NOLINT
502  return val.GetInteger();
503 }
504 template <typename T,
505  typename std::enable_if<
506  std::is_same<T, JsonInteger const>::value>::type* = nullptr>
507 JsonInteger::Int const& GetImpl(T& val) { // NOLINT
508  return val.GetInteger();
509 }
510 
511 // String
512 template <typename T,
513  typename std::enable_if<
514  std::is_same<T, JsonString>::value>::type* = nullptr>
515 std::string& GetImpl(T& val) { // NOLINT
516  return val.GetString();
517 }
518 template <typename T,
519  typename std::enable_if<
520  std::is_same<T, JsonString const>::value>::type* = nullptr>
521 std::string const& GetImpl(T& val) { // NOLINT
522  return val.GetString();
523 }
524 
525 // Boolean
526 template <typename T,
527  typename std::enable_if<
528  std::is_same<T, JsonBoolean>::value>::type* = nullptr>
529 bool& GetImpl(T& val) { // NOLINT
530  return val.GetBoolean();
531 }
532 template <typename T,
533  typename std::enable_if<
534  std::is_same<T, JsonBoolean const>::value>::type* = nullptr>
535 bool const& GetImpl(T& val) { // NOLINT
536  return val.GetBoolean();
537 }
538 
539 // Array
540 template <typename T,
541  typename std::enable_if<
542  std::is_same<T, JsonArray>::value>::type* = nullptr>
543 std::vector<Json>& GetImpl(T& val) { // NOLINT
544  return val.GetArray();
545 }
546 template <typename T,
547  typename std::enable_if<
548  std::is_same<T, JsonArray const>::value>::type* = nullptr>
549 std::vector<Json> const& GetImpl(T& val) { // NOLINT
550  return val.GetArray();
551 }
552 
553 // Typed Array
554 template <typename T, Value::ValueKind kind>
555 std::vector<T>& GetImpl(JsonTypedArray<T, kind>& val) { // NOLINT
556  return val.GetArray();
557 }
558 template <typename T, Value::ValueKind kind>
559 std::vector<T> const& GetImpl(JsonTypedArray<T, kind> const& val) {
560  return val.GetArray();
561 }
562 
563 // Object
564 template <typename T, typename std::enable_if<std::is_same<T, JsonObject>::value>::type* = nullptr>
565 JsonObject::Map& GetImpl(T& val) { // NOLINT
566  return val.GetObject();
567 }
568 template <typename T,
569  typename std::enable_if<std::is_same<T, JsonObject const>::value>::type* = nullptr>
570 JsonObject::Map const& GetImpl(T& val) { // NOLINT
571  return val.GetObject();
572 }
573 } // namespace detail
574 
583 template <typename T, typename U>
584 auto get(U& json) -> decltype(detail::GetImpl(*Cast<T>(&json.GetValue())))& { // NOLINT
585  auto& value = *Cast<T>(&json.GetValue());
586  return detail::GetImpl(value);
587 }
588 
590 using Array = JsonArray;
595 using Null = JsonNull;
596 
597 // Utils tailored for XGBoost.
598 template <typename Parameter>
599 Object ToJson(Parameter const& param) {
600  Object obj;
601  for (auto const& kv : param.__DICT__()) {
602  obj[kv.first] = kv.second;
603  }
604  return obj;
605 }
606 
607 template <typename Parameter>
608 Args FromJson(Json const& obj, Parameter* param) {
609  auto const& j_param = get<Object const>(obj);
610  std::map<std::string, std::string> m;
611  for (auto const& kv : j_param) {
612  m[kv.first] = get<String const>(kv.second);
613  }
614  return param->UpdateAllowUnknown(m);
615 }
616 } // namespace xgboost
617 #endif // XGBOOST_JSON_H_
Helper class for embedding reference counting into client objects. See https://www....
Definition: intrusive_ptr.h:20
Implementation of Intrusive Pointer. A smart pointer that points to an object with an embedded refere...
Definition: intrusive_ptr.h:73
Definition: json.h:112
JsonArray(std::vector< Json > const &arr)
Definition: json.h:119
JsonArray(JsonArray const &that)=delete
Json & operator[](std::string const &key) override
Definition: json.h:128
std::vector< Json > const & GetArray() &&
Definition: json.h:130
std::vector< Json > & GetArray() &
Definition: json.h:132
JsonArray(std::vector< Json > &&arr) noexcept
Definition: json.h:117
JsonArray(JsonArray &&that) noexcept
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonArray()
Definition: json.h:116
Json & operator[](int ind) override
Definition: json.h:126
static bool IsClassOf(Value const *value)
Definition: json.h:136
std::vector< Json > const & GetArray() const &
Definition: json.h:131
Describes both true and false.
Definition: json.h:311
bool const & GetBoolean() const &
Definition: json.h:329
static bool IsClassOf(Value const *value)
Definition: json.h:334
bool const & GetBoolean() &&
Definition: json.h:328
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonBoolean()
Definition: json.h:315
JsonBoolean(Bool value)
Definition: json.h:321
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:323
bool & GetBoolean() &
Definition: json.h:330
Definition: json.h:252
void Save(JsonWriter *writer) const override
JsonInteger(IntT value)
Definition: json.h:263
int64_t Int
Definition: json.h:254
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:280
Int const & GetInteger() const &
Definition: json.h:286
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:290
JsonInteger()
Definition: json.h:260
Int & GetInteger() &
Definition: json.h:287
Int const & GetInteger() &&
Definition: json.h:285
Definition: json.h:295
JsonNull(std::nullptr_t)
Definition: json.h:298
JsonNull()
Definition: json.h:297
bool operator==(Value const &rhs) const override
JsonNull(JsonNull &&) noexcept
Definition: json.h:299
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:305
Definition: json.h:218
Float const & GetNumber() const &
Definition: json.h:242
Float const & GetNumber() &&
Definition: json.h:241
JsonNumber(JsonNumber const &that)=delete
Float & GetNumber() &
Definition: json.h:243
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:237
bool operator==(Value const &rhs) const override
float Float
Definition: json.h:220
JsonNumber()
Definition: json.h:226
static bool IsClassOf(Value const *value)
Definition: json.h:247
JsonNumber(FloatT value)
Definition: json.h:229
void Save(JsonWriter *writer) const override
Definition: json.h:189
Json & operator[](int ind) override
Definition: json.h:205
Map const & GetObject() &&
Definition: json.h:208
JsonObject(JsonObject &&that) noexcept
void Save(JsonWriter *writer) const override
Map const & GetObject() const &
Definition: json.h:209
Json & operator[](std::string const &key) override
Definition: json.h:206
bool operator==(Value const &rhs) const override
~JsonObject() override=default
std::map< std::string, Json, std::less<> > Map
Definition: json.h:191
Map & GetObject() &
Definition: json.h:210
JsonObject(JsonObject const &that)=delete
static bool IsClassOf(Value const *value)
Definition: json.h:214
JsonObject()
Definition: json.h:197
JsonObject(Map &&object) noexcept
Definition: json_io.h:39
Definition: json.h:86
static bool IsClassOf(Value const *value)
Definition: json.h:107
JsonString(std::string &&str) noexcept
Definition: json.h:93
void Save(JsonWriter *writer) const override
std::string const & GetString() &&
Definition: json.h:101
std::string & GetString() &
Definition: json.h:103
bool operator==(Value const &rhs) const override
JsonString(std::string const &str)
Definition: json.h:91
JsonString(JsonString &&str) noexcept
Definition: json.h:95
std::string const & GetString() const &
Definition: json.h:102
JsonString()
Definition: json.h:90
Typed array for Universal Binary JSON.
Definition: json.h:148
void Set(size_t i, T v)
Definition: json.h:160
std::vector< T > & GetArray() &
Definition: json.h:167
std::vector< T > const & GetArray() &&
Definition: json.h:165
size_t Size() const
Definition: json.h:161
T Type
Definition: json.h:152
JsonTypedArray(size_t n)
Definition: json.h:155
void Save(JsonWriter *writer) const override
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:169
JsonTypedArray(JsonTypedArray &&that) noexcept
Definition: json.h:156
JsonTypedArray()
Definition: json.h:154
std::vector< T > const & GetArray() const &
Definition: json.h:166
Definition: json_io.h:131
Data structure representing JSON format.
Definition: json.h:356
Json()
Definition: json.h:374
Json & operator=(JsonNumber number)
Definition: json.h:378
Json(JsonNull null)
Definition: json.h:423
Json & operator=(JsonTypedArray< T, kind > &&array)
Definition: json.h:399
Json & operator=(JsonString &&str)
Definition: json.h:411
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:441
Json(JsonArray &&list)
Definition: json.h:389
Json(JsonObject &&object)
Definition: json.h:404
static void Dump(Json json, std::vector< char > *out, std::ios::openmode mode=std::ios::out)
Json(Json &&other) noexcept
Definition: json.h:434
Json(Json const &other)=default
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:446
Json & operator=(Json const &other)=default
Json(JsonString &&str)
Definition: json.h:410
Json(JsonInteger integer)
Definition: json.h:383
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:443
Json & operator=(JsonObject &&object)
Definition: json.h:405
Json & operator=(JsonArray &&array)
Definition: json.h:390
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:454
Json & operator=(JsonInteger integer)
Definition: json.h:384
Json & operator=(JsonBoolean boolean)
Definition: json.h:418
static Json Load(JsonReader *reader)
Pass your own JsonReader.
Json(JsonBoolean boolean)
Definition: json.h:416
IntrusivePtr< Value > const & Ptr() const
Definition: json.h:461
Json(JsonTypedArray< T, kind > &&list)
Definition: json.h:396
Json(JsonNumber number)
Definition: json.h:377
Json & operator=(JsonNull null)
Definition: json.h:425
bool operator==(Json const &rhs) const
Definition: json.h:450
static void Dump(Json json, JsonWriter *writer)
Use your own JsonWriter.
Json & operator=(Json &&other) noexcept
Definition: json.h:435
static Json Load(StringView str, std::ios::openmode mode=std::ios::in)
Decode the JSON object. Optional parameter mode for choosing between text and binary (ubjson) input.
static void Dump(Json json, std::string *out, std::ios::openmode mode=std::ios::out)
Encode the JSON object. Optional parameter mode for choosing between text and binary (ubjson) output.
Value const & GetValue() &&
Definition: json.h:447
Value & GetValue() &
Definition: json.h:448
Definition: json.h:25
virtual Value & operator=(Value const &rhs)=delete
virtual bool operator==(Value const &rhs) const =0
Value(ValueKind _kind)
Definition: json.h:50
virtual Json & operator[](std::string const &key)
virtual ~Value()=default
virtual Json & operator[](int ind)
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:35
friend IntrusivePtrCell & IntrusivePtrRefCount(xgboost::Value const *t) noexcept
Definition: json.h:29
ValueKind Type() const
Definition: json.h:52
std::string TypeStr() const
virtual void Save(JsonWriter *writer) const =0
Implementation of Intrusive Ptr.
Definition: intrusive_ptr.h:207
void swap(xgboost::IntrusivePtr< T > &x, xgboost::IntrusivePtr< T > &y) noexcept
Definition: intrusive_ptr.h:209
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:487
namespace of xgboost
Definition: base.h:110
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:318
bool IsA(Value const *value)
Definition: json.h:72
auto get(U &json) -> decltype(detail::GetImpl(*Cast< T >(&json.GetValue())))&
Get Json value.
Definition: json.h:584
T * Cast(U *value)
Definition: json.h:77
Object ToJson(Parameter const &param)
Definition: json.h:599
Args FromJson(Json const &obj, Parameter *param)
Definition: json.h:608
macro for using C++11 enum class as DMLC parameter
Definition: string_view.h:15