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 <string>
15 #include <type_traits> // std::enable_if_t
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_;
29  return t->ref_;
30  }
31 
32  public:
38  enum class ValueKind : std::int64_t {
39  kString = 0,
40  kNumber = 1,
41  kInteger = 2,
42  kObject = 3, // std::map
43  kArray = 4, // std::vector
44  kBoolean = 5,
45  kNull = 6,
46  // typed array for ubjson
47  kF32Array = 7,
48  kF64Array = 8,
49  kI8Array = 9,
50  kU8Array = 10,
51  kI16Array = 11,
52  kU16Array = 12,
53  kI32Array = 13,
54  kU32Array = 14,
55  kI64Array = 15,
56  kU64Array = 16,
57  };
58 
59  explicit Value(ValueKind _kind) : kind_{_kind} {}
60 
61  ValueKind Type() const { return kind_; }
62  virtual ~Value() = default;
63 
64  virtual void Save(JsonWriter* writer) const = 0;
65 
66  virtual Json& operator[](std::string const& key);
67  virtual Json& operator[](int ind);
68 
69  virtual bool operator==(Value const& rhs) const = 0;
70  virtual Value& operator=(Value const& rhs) = delete;
71 
72  std::string TypeStr() const;
73 
74  private:
75  ValueKind kind_;
76 };
77 
78 template <typename T>
79 bool IsA(Value const* value) {
80  return T::IsClassOf(value);
81 }
82 
83 template <typename T, typename U>
84 T* Cast(U* value) {
85  if (IsA<T>(value)) {
86  return dynamic_cast<T*>(value);
87  } else {
88  LOG(FATAL) << "Invalid cast, from " + value->TypeStr() + " to " + T().TypeStr();
89  }
90  return dynamic_cast<T*>(value); // suppress compiler warning.
91 }
92 
93 class JsonString : public Value {
94  std::string str_;
95 
96  public:
97  JsonString() : Value(ValueKind::kString) {}
98  JsonString(std::string const& str) : // NOLINT
99  Value(ValueKind::kString), str_{str} {}
100  JsonString(std::string&& str) noexcept : // NOLINT
101  Value(ValueKind::kString), str_{std::forward<std::string>(str)} {}
102  JsonString(JsonString&& str) noexcept : Value(ValueKind::kString) { // NOLINT
103  std::swap(str.str_, this->str_);
104  }
105 
106  void Save(JsonWriter* writer) const override;
107 
108  std::string const& GetString() && { return str_; }
109  std::string const& GetString() const & { return str_; }
110  std::string& GetString() & { return str_; }
111 
112  bool operator==(Value const& rhs) const override;
113  Value& operator=(Value const& rhs) override = delete;
114 
115  static bool IsClassOf(Value const* value) {
116  return value->Type() == ValueKind::kString;
117  }
118 };
119 
120 class JsonArray : public Value {
121  std::vector<Json> vec_;
122 
123  public:
124  JsonArray() : Value(ValueKind::kArray) {}
125  JsonArray(std::vector<Json>&& arr) noexcept // NOLINT
126  : Value(ValueKind::kArray), vec_{std::forward<std::vector<Json>>(arr)} {}
127  JsonArray(std::vector<Json> const& arr) : // NOLINT
128  Value(ValueKind::kArray), vec_{arr} {}
129  JsonArray(JsonArray const& that) = delete;
130  JsonArray(JsonArray && that) noexcept;
131 
132  void Save(JsonWriter* writer) const override;
133 
134  Json& operator[](int ind) override { return vec_.at(ind); }
135  // silent the partial oveeridden warning
136  Json& operator[](std::string const& key) override { return Value::operator[](key); }
137 
138  std::vector<Json> const& GetArray() && { return vec_; }
139  std::vector<Json> const& GetArray() const & { return vec_; }
140  std::vector<Json>& GetArray() & { return vec_; }
141 
142  bool operator==(Value const& rhs) const override;
143  Value& operator=(Value const& rhs) override = delete;
144 
145  static bool IsClassOf(Value const* value) {
146  return value->Type() == ValueKind::kArray;
147  }
148 };
149 
156 template <typename T, Value::ValueKind kind>
157 class JsonTypedArray : public Value {
158  std::vector<T> vec_;
159 
160  public:
161  using value_type = T; // NOLINT
162 
163  JsonTypedArray() : Value(kind) {}
164  explicit JsonTypedArray(std::size_t n) : Value(kind) { vec_.resize(n); }
165  JsonTypedArray(JsonTypedArray&& that) noexcept : Value{kind}, vec_{std::move(that.vec_)} {}
166 
167  bool operator==(Value const& rhs) const override;
168  Value& operator=(Value const& rhs) override = delete;
169 
170  void Set(size_t i, T v) { vec_[i] = v; }
171  size_t Size() const { return vec_.size(); }
172 
173  void Save(JsonWriter* writer) const override;
174 
175  std::vector<T> const& GetArray() && { return vec_; }
176  std::vector<T> const& GetArray() const& { return vec_; }
177  std::vector<T>& GetArray() & { return vec_; }
178 
179  static bool IsClassOf(Value const* value) { return value->Type() == kind; }
180 };
181 
222 
223 class JsonObject : public Value {
224  public:
225  using Map = std::map<std::string, Json, std::less<>>;
226 
227  private:
228  Map object_;
229 
230  public:
231  JsonObject() : Value(ValueKind::kObject) {}
232  JsonObject(Map&& object) noexcept; // NOLINT
233  JsonObject(JsonObject const& that) = delete;
234  JsonObject(JsonObject&& that) noexcept;
235 
236  void Save(JsonWriter* writer) const override;
237 
238  // silent the partial oveeridden warning
239  Json& operator[](int ind) override { return Value::operator[](ind); }
240  Json& operator[](std::string const& key) override { return object_[key]; }
241 
242  Map const& GetObject() && { return object_; }
243  Map const& GetObject() const& { return object_; }
244  Map& GetObject() & { return object_; }
245 
246  bool operator==(Value const& rhs) const override;
247  Value& operator=(Value const& rhs) override = delete;
248 
249  static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kObject; }
250  ~JsonObject() override = default;
251 };
252 
253 namespace detail {
254 template <typename T, typename U>
255 using IsSameT = std::enable_if_t<std::is_same_v<std::remove_cv_t<T>, std::remove_cv_t<U>>>;
256 
257 template <typename T>
258 using IsF64T = std::enable_if_t<std::is_same_v<T, double>>;
259 } // namespace detail
260 
261 class JsonNumber : public Value {
262  public:
263  using Float = float;
264 
265  private:
266  Float number_ { 0 };
267 
268  public:
269  JsonNumber() : Value(ValueKind::kNumber) {}
270  template <typename FloatT, typename detail::IsSameT<FloatT, Float>* = nullptr>
271  JsonNumber(FloatT value) : Value(ValueKind::kNumber), number_{value} {} // NOLINT
272  template <typename FloatT, typename detail::IsF64T<FloatT>* = nullptr>
273  JsonNumber(FloatT value) // NOLINT
274  : Value{ValueKind::kNumber}, number_{static_cast<Float>(value)} {}
275  JsonNumber(JsonNumber const& that) = delete;
276  JsonNumber(JsonNumber&& that) noexcept : Value{ValueKind::kNumber}, number_{that.number_} {}
277 
278  void Save(JsonWriter* writer) const override;
279 
280  Float const& GetNumber() && { return number_; }
281  Float const& GetNumber() const & { return number_; }
282  Float& GetNumber() & { return number_; }
283 
284  bool operator==(Value const& rhs) const override;
285  Value& operator=(Value const& rhs) override = delete;
286 
287  static bool IsClassOf(Value const* value) {
288  return value->Type() == ValueKind::kNumber;
289  }
290 };
291 
292 namespace detail {
293 template <typename IntT>
294 using Not32SizeT = std::enable_if_t<std::is_same_v<IntT, std::uint32_t> &&
295  !std::is_same_v<std::size_t, std::uint32_t>>;
296 }
297 
298 
299 class JsonInteger : public Value {
300  public:
301  using Int = int64_t;
302 
303  private:
304  Int integer_ {0};
305 
306  public:
307  JsonInteger() : Value(ValueKind::kInteger) {} // NOLINT
308  template <typename IntT, typename detail::IsSameT<IntT, Int>* = nullptr>
309  JsonInteger(IntT value) : Value(ValueKind::kInteger), integer_{value} {} // NOLINT
310  template <typename IntT, typename detail::IsSameT<IntT, std::size_t>* = nullptr>
311  JsonInteger(IntT value) // NOLINT
312  : Value(ValueKind::kInteger), integer_{static_cast<Int>(value)} {}
313  template <typename IntT, typename detail::IsSameT<IntT, std::int32_t>* = nullptr>
314  JsonInteger(IntT value) // NOLINT
315  : Value(ValueKind::kInteger), integer_{static_cast<Int>(value)} {}
316  template <typename IntT,
317  typename detail::Not32SizeT<IntT>* = nullptr>
318  JsonInteger(IntT value) // NOLINT
319  : Value(ValueKind::kInteger), integer_{static_cast<Int>(value)} {}
320 
321  JsonInteger(JsonInteger &&that) noexcept
322  : Value{ValueKind::kInteger}, integer_{that.integer_} {}
323 
324  bool operator==(Value const& rhs) const override;
325  Value& operator=(Value const& rhs) override = delete;
326 
327  Int const& GetInteger() && { return integer_; }
328  Int const& GetInteger() const & { return integer_; }
329  Int& GetInteger() & { return integer_; }
330  void Save(JsonWriter* writer) const override;
331 
332  static bool IsClassOf(Value const* value) {
333  return value->Type() == ValueKind::kInteger;
334  }
335 };
336 
337 class JsonNull : public Value {
338  public:
339  JsonNull() : Value(ValueKind::kNull) {}
340  JsonNull(std::nullptr_t) : Value(ValueKind::kNull) {} // NOLINT
341  JsonNull(JsonNull&&) noexcept : Value(ValueKind::kNull) {}
342 
343  void Save(JsonWriter* writer) const override;
344 
345  bool operator==(Value const& rhs) const override;
346  Value& operator=(Value const& rhs) override = delete;
347 
348  static bool IsClassOf(Value const* value) {
349  return value->Type() == ValueKind::kNull;
350  }
351 };
352 
354 class JsonBoolean : public Value {
355  bool boolean_ = false;
356 
357  public:
358  JsonBoolean() : Value(ValueKind::kBoolean) {} // NOLINT
359  // Ambigious with JsonNumber.
360  template <typename Bool, typename detail::IsSameT<std::remove_cv_t<Bool>, bool>* = nullptr>
361  JsonBoolean(Bool value) : Value(ValueKind::kBoolean), boolean_{value} {} // NOLINT
362  JsonBoolean(JsonBoolean&& value) noexcept: // NOLINT
363  Value(ValueKind::kBoolean), boolean_{value.boolean_} {}
364 
365  void Save(JsonWriter* writer) const override;
366 
367  bool const& GetBoolean() && { return boolean_; }
368  bool const& GetBoolean() const & { return boolean_; }
369  bool& GetBoolean() & { return boolean_; }
370 
371  bool operator==(Value const& rhs) const override;
372  Value& operator=(Value const& rhs) override = delete;
373 
374  static bool IsClassOf(Value const* value) {
375  return value->Type() == ValueKind::kBoolean;
376  }
377 };
378 
396 class Json {
397  public:
402  static Json Load(StringView str, std::ios::openmode mode = std::ios::in);
404  static Json Load(JsonReader* reader);
409  static void Dump(Json json, std::string* out, std::ios::openmode mode = std::ios::out);
410  static void Dump(Json json, std::vector<char>* out, std::ios::openmode mode = std::ios::out);
412  static void Dump(Json json, JsonWriter* writer);
413 
414  template <typename Container = std::string>
415  static Container Dump(Json json) {
416  if constexpr (std::is_same_v<Container, std::string>) {
417  std::string str;
418  Dump(json, &str);
419  return str;
420  } else {
421  std::vector<char> str;
422  Dump(json, &str);
423  return str;
424  }
425  }
426 
427  Json() = default;
428 
429  // number
430  explicit Json(JsonNumber number) : ptr_{new JsonNumber(std::move(number))} {}
432  ptr_.reset(new JsonNumber(std::move(number)));
433  return *this;
434  }
435  // integer
436  explicit Json(JsonInteger integer) : ptr_{new JsonInteger(std::move(integer))} {}
438  ptr_.reset(new JsonInteger(std::move(integer)));
439  return *this;
440  }
441  // array
442  explicit Json(JsonArray&& list) : ptr_{new JsonArray(std::forward<JsonArray>(list))} {}
444  ptr_.reset(new JsonArray(std::forward<JsonArray>(array)));
445  return *this;
446  }
447  // typed array
448  template <typename T, Value::ValueKind kind>
449  explicit Json(JsonTypedArray<T, kind>&& list)
450  : ptr_{new JsonTypedArray<T, kind>(std::forward<JsonTypedArray<T, kind>>(list))} {}
451  template <typename T, Value::ValueKind kind>
453  ptr_.reset(new JsonTypedArray<T, kind>(std::forward<JsonTypedArray<T, kind>>(array)));
454  return *this;
455  }
456  // object
457  explicit Json(JsonObject&& object) : ptr_{new JsonObject(std::forward<JsonObject>(object))} {}
458  Json& operator=(JsonObject&& object) {
459  ptr_.reset(new JsonObject(std::forward<JsonObject>(object)));
460  return *this;
461  }
462  // string
463  explicit Json(JsonString&& str) : ptr_{new JsonString(std::forward<JsonString>(str))} {}
465  ptr_.reset(new JsonString(std::forward<JsonString>(str)));
466  return *this;
467  }
468  // bool
469  explicit Json(JsonBoolean boolean) :
470  ptr_{new JsonBoolean(std::move(boolean))} {}
472  ptr_.reset(new JsonBoolean(std::move(boolean)));
473  return *this;
474  }
475  // null
476  explicit Json(JsonNull null) :
477  ptr_{new JsonNull(std::move(null))} {}
479  ptr_.reset(new JsonNull(std::move(null)));
480  return *this;
481  }
482 
483  // copy
484  Json(Json const& other) = default;
485  Json& operator=(Json const& other) = default;
486  // move
487  Json(Json &&other) noexcept { std::swap(this->ptr_, other.ptr_); }
488  Json &operator=(Json &&other) noexcept {
489  std::swap(this->ptr_, other.ptr_);
490  return *this;
491  }
492 
494  Json& operator[](std::string const & key) const { return (*ptr_)[key]; }
496  Json& operator[](int ind) const { return (*ptr_)[ind]; }
497 
499  [[nodiscard]] Value const& GetValue() const& { return *ptr_; }
500  Value const& GetValue() && { return *ptr_; }
501  Value& GetValue() & { return *ptr_; }
502 
503  bool operator==(Json const& rhs) const {
504  return *ptr_ == *(rhs.ptr_);
505  }
506 
507  friend std::ostream& operator<<(std::ostream& os, Json const& j) {
508  std::string str;
509  Json::Dump(j, &str);
510  os << str;
511  return os;
512  }
513 
514  [[nodiscard]] IntrusivePtr<Value> const& Ptr() const { return ptr_; }
515 
516  private:
517  IntrusivePtr<Value> ptr_{new JsonNull};
518 };
519 
529 template <typename T>
530 bool IsA(Json const& j) {
531  auto const& v = j.GetValue();
532  return IsA<T>(&v);
533 }
534 
535 namespace detail {
536 // Number
537 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonNumber>>* = nullptr>
538 JsonNumber::Float& GetImpl(T& val) { // NOLINT
539  return val.GetNumber();
540 }
541 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonNumber const>>* = nullptr>
542 JsonNumber::Float const& GetImpl(T& val) { // NOLINT
543  return val.GetNumber();
544 }
545 
546 // Integer
547 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonInteger>>* = nullptr>
548 JsonInteger::Int& GetImpl(T& val) { // NOLINT
549  return val.GetInteger();
550 }
551 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonInteger const>>* = nullptr>
552 JsonInteger::Int const& GetImpl(T& val) { // NOLINT
553  return val.GetInteger();
554 }
555 
556 // String
557 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonString>>* = nullptr>
558 std::string& GetImpl(T& val) { // NOLINT
559  return val.GetString();
560 }
561 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonString const>>* = nullptr>
562 std::string const& GetImpl(T& val) { // NOLINT
563  return val.GetString();
564 }
565 
566 // Boolean
567 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonBoolean>>* = nullptr>
568 bool& GetImpl(T& val) { // NOLINT
569  return val.GetBoolean();
570 }
571 template <typename T,
572  typename std::enable_if_t<std::is_same_v<T, JsonBoolean const>>* = nullptr>
573 bool const& GetImpl(T& val) { // NOLINT
574  return val.GetBoolean();
575 }
576 
577 // Array
578 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonArray>>* = nullptr>
579 std::vector<Json>& GetImpl(T& val) { // NOLINT
580  return val.GetArray();
581 }
582 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonArray const>>* = nullptr>
583 std::vector<Json> const& GetImpl(T& val) { // NOLINT
584  return val.GetArray();
585 }
586 
587 // Typed Array
588 template <typename T, Value::ValueKind kind>
589 std::vector<T>& GetImpl(JsonTypedArray<T, kind>& val) { // NOLINT
590  return val.GetArray();
591 }
592 template <typename T, Value::ValueKind kind>
593 std::vector<T> const& GetImpl(JsonTypedArray<T, kind> const& val) {
594  return val.GetArray();
595 }
596 
597 // Object
598 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonObject>>* = nullptr>
599 JsonObject::Map& GetImpl(T& val) { // NOLINT
600  return val.GetObject();
601 }
602 template <typename T, typename std::enable_if_t<std::is_same_v<T, JsonObject const>>* = nullptr>
603 JsonObject::Map const& GetImpl(T& val) { // NOLINT
604  return val.GetObject();
605 }
606 } // namespace detail
607 
616 template <typename T, typename U>
617 auto get(U& json) -> decltype(detail::GetImpl(*Cast<T>(&json.GetValue())))& { // NOLINT
618  auto& value = *Cast<T>(&json.GetValue());
619  return detail::GetImpl(value);
620 }
621 
623 using Array = JsonArray;
628 using Null = JsonNull;
629 
639 template <typename Parameter>
640 Object ToJson(Parameter const& param) {
641  Object obj;
642  for (auto const& kv : param.__DICT__()) {
643  obj[kv.first] = kv.second;
644  }
645  return obj;
646 }
647 
658 template <typename Parameter>
659 Args FromJson(Json const& obj, Parameter* param) {
660  auto const& j_param = get<Object const>(obj);
661  Args args;
662  for (auto const& kv : j_param) {
663  args.emplace_back(kv.first, get<String const>(kv.second));
664  }
665  return param->UpdateAllowUnknown(args);
666 }
667 } // namespace xgboost
668 #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:120
JsonArray(std::vector< Json > const &arr)
Definition: json.h:127
JsonArray(JsonArray const &that)=delete
Json & operator[](std::string const &key) override
Definition: json.h:136
std::vector< Json > const & GetArray() &&
Definition: json.h:138
std::vector< Json > & GetArray() &
Definition: json.h:140
JsonArray(std::vector< Json > &&arr) noexcept
Definition: json.h:125
JsonArray(JsonArray &&that) noexcept
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonArray()
Definition: json.h:124
Json & operator[](int ind) override
Definition: json.h:134
Value & operator=(Value const &rhs) override=delete
static bool IsClassOf(Value const *value)
Definition: json.h:145
std::vector< Json > const & GetArray() const &
Definition: json.h:139
Describes both true and false.
Definition: json.h:354
bool const & GetBoolean() const &
Definition: json.h:368
static bool IsClassOf(Value const *value)
Definition: json.h:374
Value & operator=(Value const &rhs) override=delete
bool const & GetBoolean() &&
Definition: json.h:367
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonBoolean()
Definition: json.h:358
JsonBoolean(Bool value)
Definition: json.h:361
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:362
bool & GetBoolean() &
Definition: json.h:369
Definition: json.h:299
void Save(JsonWriter *writer) const override
JsonInteger(IntT value)
Definition: json.h:309
int64_t Int
Definition: json.h:301
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:321
Value & operator=(Value const &rhs) override=delete
Int const & GetInteger() const &
Definition: json.h:328
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:332
JsonInteger()
Definition: json.h:307
Int & GetInteger() &
Definition: json.h:329
Int const & GetInteger() &&
Definition: json.h:327
Definition: json.h:337
JsonNull(std::nullptr_t)
Definition: json.h:340
JsonNull()
Definition: json.h:339
bool operator==(Value const &rhs) const override
JsonNull(JsonNull &&) noexcept
Definition: json.h:341
Value & operator=(Value const &rhs) override=delete
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:348
Definition: json.h:261
Float const & GetNumber() const &
Definition: json.h:281
Float const & GetNumber() &&
Definition: json.h:280
Value & operator=(Value const &rhs) override=delete
JsonNumber(JsonNumber const &that)=delete
Float & GetNumber() &
Definition: json.h:282
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:276
bool operator==(Value const &rhs) const override
float Float
Definition: json.h:263
JsonNumber()
Definition: json.h:269
static bool IsClassOf(Value const *value)
Definition: json.h:287
JsonNumber(FloatT value)
Definition: json.h:271
void Save(JsonWriter *writer) const override
Definition: json.h:223
Json & operator[](int ind) override
Definition: json.h:239
Map const & GetObject() &&
Definition: json.h:242
JsonObject(JsonObject &&that) noexcept
void Save(JsonWriter *writer) const override
Map const & GetObject() const &
Definition: json.h:243
Json & operator[](std::string const &key) override
Definition: json.h:240
bool operator==(Value const &rhs) const override
~JsonObject() override=default
std::map< std::string, Json, std::less<> > Map
Definition: json.h:225
Value & operator=(Value const &rhs) override=delete
Map & GetObject() &
Definition: json.h:244
JsonObject(JsonObject const &that)=delete
static bool IsClassOf(Value const *value)
Definition: json.h:249
JsonObject()
Definition: json.h:231
JsonObject(Map &&object) noexcept
A json reader, currently error checking and utf-8 is not fully supported.
Definition: json_io.h:21
Definition: json.h:93
static bool IsClassOf(Value const *value)
Definition: json.h:115
JsonString(std::string &&str) noexcept
Definition: json.h:100
void Save(JsonWriter *writer) const override
std::string const & GetString() &&
Definition: json.h:108
std::string & GetString() &
Definition: json.h:110
bool operator==(Value const &rhs) const override
Value & operator=(Value const &rhs) override=delete
JsonString(std::string const &str)
Definition: json.h:98
JsonString(JsonString &&str) noexcept
Definition: json.h:102
std::string const & GetString() const &
Definition: json.h:109
JsonString()
Definition: json.h:97
Typed array for Universal Binary JSON.
Definition: json.h:157
void Set(size_t i, T v)
Definition: json.h:170
T value_type
Definition: json.h:161
std::vector< T > & GetArray() &
Definition: json.h:177
std::vector< T > const & GetArray() &&
Definition: json.h:175
size_t Size() const
Definition: json.h:171
void Save(JsonWriter *writer) const override
bool operator==(Value const &rhs) const override
JsonTypedArray(std::size_t n)
Definition: json.h:164
Value & operator=(Value const &rhs) override=delete
static bool IsClassOf(Value const *value)
Definition: json.h:179
JsonTypedArray(JsonTypedArray &&that) noexcept
Definition: json.h:165
JsonTypedArray()
Definition: json.h:163
std::vector< T > const & GetArray() const &
Definition: json.h:176
Definition: json_io.h:111
Data structure representing JSON format.
Definition: json.h:396
Json & operator=(JsonNumber number)
Definition: json.h:431
Json(JsonNull null)
Definition: json.h:476
Json & operator=(JsonTypedArray< T, kind > &&array)
Definition: json.h:452
Json & operator=(JsonString &&str)
Definition: json.h:464
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:494
Json(JsonArray &&list)
Definition: json.h:442
Json(JsonObject &&object)
Definition: json.h:457
static void Dump(Json json, std::vector< char > *out, std::ios::openmode mode=std::ios::out)
Json(Json &&other) noexcept
Definition: json.h:487
Json(Json const &other)=default
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:499
Json & operator=(Json const &other)=default
Json(JsonString &&str)
Definition: json.h:463
Json(JsonInteger integer)
Definition: json.h:436
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:496
Json & operator=(JsonObject &&object)
Definition: json.h:458
Json & operator=(JsonArray &&array)
Definition: json.h:443
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:507
Json & operator=(JsonInteger integer)
Definition: json.h:437
Json & operator=(JsonBoolean boolean)
Definition: json.h:471
static Json Load(JsonReader *reader)
Pass your own JsonReader.
Json()=default
Json(JsonBoolean boolean)
Definition: json.h:469
IntrusivePtr< Value > const & Ptr() const
Definition: json.h:514
Json(JsonTypedArray< T, kind > &&list)
Definition: json.h:449
Json(JsonNumber number)
Definition: json.h:430
Json & operator=(JsonNull null)
Definition: json.h:478
static Container Dump(Json json)
Definition: json.h:415
bool operator==(Json const &rhs) const
Definition: json.h:503
static void Dump(Json json, JsonWriter *writer)
Use your own JsonWriter.
Json & operator=(Json &&other) noexcept
Definition: json.h:488
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:500
Value & GetValue() &
Definition: json.h:501
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:59
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:38
virtual Json & operator[](std::string const &key)
virtual ~Value()=default
virtual Json & operator[](int ind)
friend IntrusivePtrCell & IntrusivePtrRefCount(xgboost::Value const *t) noexcept
Definition: json.h:28
ValueKind Type() const
Definition: json.h:61
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
std::enable_if_t< std::is_same_v< IntT, std::uint32_t > &&!std::is_same_v< std::size_t, std::uint32_t > > Not32SizeT
Definition: json.h:295
std::enable_if_t< std::is_same_v< T, double > > IsF64T
Definition: json.h:258
std::enable_if_t< std::is_same_v< std::remove_cv_t< T >, std::remove_cv_t< U > >> IsSameT
Definition: json.h:255
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:538
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:89
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:306
bool IsA(Value const *value)
Definition: json.h:79
auto get(U &json) -> decltype(detail::GetImpl(*Cast< T >(&json.GetValue())))&
Get Json value.
Definition: json.h:617
T * Cast(U *value)
Definition: json.h:84
Object ToJson(Parameter const &param)
Convert XGBoost parameter to JSON object.
Definition: json.h:640
Args FromJson(Json const &obj, Parameter *param)
Load a XGBoost parameter from a JSON object.
Definition: json.h:659
macro for using C++11 enum class as DMLC parameter
Definition: string_view.h:16