xgboost

json.h
Go to the documentation of this file.
1
4 #ifndef XGBOOST_JSON_H_
5 #define XGBOOST_JSON_H_
6
7 #include <xgboost/logging.h>
8 #include <xgboost/parameter.h>
9 #include <xgboost/intrusive_ptr.h>
10
11 #include <map>
12 #include <memory>
13 #include <vector>
14 #include <functional>
15 #include <utility>
16 #include <string>
17
18 namespace xgboost {
19
20 class Json;
21 class JsonReader;
22 class JsonWriter;
23
24 class Value {
25 private:
26 mutable class IntrusivePtrCell ref_;
27 friend IntrusivePtrCell &
28 IntrusivePtrRefCount(xgboost::Value const *t) noexcept {
29 return t->ref_;
30 }
31
32 public:
34 enum class ValueKind {
35 kString,
36 kNumber,
37 kInteger,
38 kObject, // std::map
39 kArray, // std::vector
40 kBoolean,
41 kNull
42 };
43
44 explicit Value(ValueKind _kind) : kind_{_kind} {}
45
46 ValueKind Type() const { return kind_; }
47 virtual ~Value() = default;
48
49 virtual void Save(JsonWriter* writer) = 0;
50
51 virtual Json& operator[](std::string const & key) = 0;
52 virtual Json& operator[](int ind) = 0;
53
54 virtual bool operator==(Value const& rhs) const = 0;
55 virtual Value& operator=(Value const& rhs) = 0;
56
57 std::string TypeStr() const;
58
59 private:
60 ValueKind kind_;
61 };
62
63 template <typename T>
64 bool IsA(Value const* value) {
65 return T::IsClassOf(value);
66 }
67
68 template <typename T, typename U>
69 T* Cast(U* value) {
70 if (IsA<T>(value)) {
71 return dynamic_cast<T*>(value);
72 } else {
73 LOG(FATAL) << "Invalid cast, from " + value->TypeStr() + " to " + T().TypeStr();
74 }
75 return dynamic_cast<T*>(value); // supress compiler warning.
76 }
77
78 class JsonString : public Value {
79 std::string str_;
80
81 public:
82 JsonString() : Value(ValueKind::kString) {}
83 JsonString(std::string const& str) : // NOLINT
84 Value(ValueKind::kString), str_{str} {}
85 JsonString(std::string&& str) : // NOLINT
86 Value(ValueKind::kString), str_{std::move(str)} {}
87 JsonString(JsonString&& str) noexcept : // NOLINT
88 Value(ValueKind::kString), str_{std::move(str.str_)} {}
89
90 void Save(JsonWriter* writer) override;
91
92 Json& operator[](std::string const & key) override;
93 Json& operator[](int ind) override;
94
95 std::string const& GetString() && { return str_; }
96 std::string const& GetString() const & { return str_; }
97 std::string& GetString() & { return str_; }
98
99 bool operator==(Value const& rhs) const override;
100 Value& operator=(Value const& rhs) override;
101
102 static bool IsClassOf(Value const* value) {
103 return value->Type() == ValueKind::kString;
104 }
105 };
106
107 class JsonArray : public Value {
108 std::vector<Json> vec_;
109
110 public:
111 JsonArray() : Value(ValueKind::kArray) {}
112 JsonArray(std::vector<Json>&& arr) : // NOLINT
113 Value(ValueKind::kArray), vec_{std::move(arr)} {}
114 JsonArray(std::vector<Json> const& arr) : // NOLINT
115 Value(ValueKind::kArray), vec_{arr} {}
116 JsonArray(JsonArray const& that) = delete;
117 JsonArray(JsonArray && that);
118
119 void Save(JsonWriter* writer) override;
120
121 Json& operator[](std::string const & key) override;
122 Json& operator[](int ind) override;
123
124 std::vector<Json> const& GetArray() && { return vec_; }
125 std::vector<Json> const& GetArray() const & { return vec_; }
126 std::vector<Json>& GetArray() & { return vec_; }
127
128 bool operator==(Value const& rhs) const override;
129 Value& operator=(Value const& rhs) override;
130
131 static bool IsClassOf(Value const* value) {
132 return value->Type() == ValueKind::kArray;
133 }
134 };
135
136 class JsonObject : public Value {
137 std::map<std::string, Json> object_;
138
139 public:
140 JsonObject() : Value(ValueKind::kObject) {}
141 JsonObject(std::map<std::string, Json>&& object); // NOLINT
142 JsonObject(JsonObject const& that) = delete;
143 JsonObject(JsonObject && that);
144
145 void Save(JsonWriter* writer) override;
146
147 Json& operator[](std::string const & key) override;
148 Json& operator[](int ind) override;
149
150 std::map<std::string, Json> const& GetObject() && { return object_; }
151 std::map<std::string, Json> const& GetObject() const & { return object_; }
152 std::map<std::string, Json> & GetObject() & { return object_; }
153
154 bool operator==(Value const& rhs) const override;
155 Value& operator=(Value const& rhs) override;
156
157 static bool IsClassOf(Value const* value) {
158 return value->Type() == ValueKind::kObject;
159 }
160 ~JsonObject() override = default;
161 };
162
163 class JsonNumber : public Value {
164 public:
165 using Float = float;
166
167 private:
168 Float number_ { 0 };
169
170 public:
171 JsonNumber() : Value(ValueKind::kNumber) {}
172 template <typename FloatT,
173 typename std::enable_if<std::is_same<FloatT, Float>::value>::type* = nullptr>
174 JsonNumber(FloatT value) : Value(ValueKind::kNumber) { // NOLINT
175 number_ = value;
176 }
177 template <typename FloatT,
178 typename std::enable_if<std::is_same<FloatT, double>::value>::type* = nullptr>
179 JsonNumber(FloatT value) : Value{ValueKind::kNumber}, // NOLINT
180 number_{static_cast<Float>(value)} {}
181 JsonNumber(JsonNumber const& that) = delete;
182 JsonNumber(JsonNumber&& that) noexcept : Value{ValueKind::kNumber}, number_{that.number_} {}
183
184 void Save(JsonWriter* writer) override;
185
186 Json& operator[](std::string const & key) override;
187 Json& operator[](int ind) override;
188
189 Float const& GetNumber() && { return number_; }
190 Float const& GetNumber() const & { return number_; }
191 Float& GetNumber() & { return number_; }
192
193
194 bool operator==(Value const& rhs) const override;
195 Value& operator=(Value const& rhs) override;
196
197 static bool IsClassOf(Value const* value) {
198 return value->Type() == ValueKind::kNumber;
199 }
200 };
201
202 class JsonInteger : public Value {
203 public:
204 using Int = int64_t;
205
206 private:
207 Int integer_ {0};
208
209 public:
210 JsonInteger() : Value(ValueKind::kInteger) {} // NOLINT
211 template <typename IntT,
212 typename std::enable_if<std::is_same<IntT, Int>::value>::type* = nullptr>
213 JsonInteger(IntT value) : Value(ValueKind::kInteger), integer_{value} {} // NOLINT
214 template <typename IntT,
215 typename std::enable_if<std::is_same<IntT, size_t>::value>::type* = nullptr>
216 JsonInteger(IntT value) : Value(ValueKind::kInteger), // NOLINT
217 integer_{static_cast<Int>(value)} {}
218 template <typename IntT,
219 typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* = nullptr>
220 JsonInteger(IntT value) : Value(ValueKind::kInteger), // NOLINT
221 integer_{static_cast<Int>(value)} {}
222 template <typename IntT,
223 typename std::enable_if<
224 std::is_same<IntT, uint32_t>::value &&
225 !std::is_same<std::size_t, uint32_t>::value>::type * = nullptr>
226 JsonInteger(IntT value) // NOLINT
227 : Value(ValueKind::kInteger),
228 integer_{static_cast<Int>(value)} {}
229
230 JsonInteger(JsonInteger &&that) noexcept
231 : Value{ValueKind::kInteger}, integer_{that.integer_} {}
232
233 Json& operator[](std::string const & key) override;
234 Json& operator[](int ind) override;
235
236 bool operator==(Value const& rhs) const override;
237 Value& operator=(Value const& rhs) override;
238
239 Int const& GetInteger() && { return integer_; }
240 Int const& GetInteger() const & { return integer_; }
241 Int& GetInteger() & { return integer_; }
242 void Save(JsonWriter* writer) override;
243
244 static bool IsClassOf(Value const* value) {
245 return value->Type() == ValueKind::kInteger;
246 }
247 };
248
249 class JsonNull : public Value {
250 public:
251 JsonNull() : Value(ValueKind::kNull) {}
252 JsonNull(std::nullptr_t) : Value(ValueKind::kNull) {} // NOLINT
253 JsonNull(JsonNull&&) noexcept : Value(ValueKind::kNull) {}
254
255 void Save(JsonWriter* writer) override;
256
257 Json& operator[](std::string const & key) override;
258 Json& operator[](int ind) override;
259
260 bool operator==(Value const& rhs) const override;
261 Value& operator=(Value const& rhs) override;
262
263 static bool IsClassOf(Value const* value) {
264 return value->Type() == ValueKind::kNull;
265 }
266 };
267
269 class JsonBoolean : public Value {
270 bool boolean_ = false;
271
272 public:
273 JsonBoolean() : Value(ValueKind::kBoolean) {} // NOLINT
274 // Ambigious with JsonNumber.
275 template <typename Bool,
276 typename std::enable_if<
277 std::is_same<Bool, bool>::value ||
278 std::is_same<Bool, bool const>::value>::type* = nullptr>
279 JsonBoolean(Bool value) : // NOLINT
280 Value(ValueKind::kBoolean), boolean_{value} {}
281 JsonBoolean(JsonBoolean&& value) noexcept: // NOLINT
282 Value(ValueKind::kBoolean), boolean_{value.boolean_} {}
283
284 void Save(JsonWriter* writer) override;
285
286 Json& operator[](std::string const & key) override;
287 Json& operator[](int ind) override;
288
289 bool const& GetBoolean() && { return boolean_; }
290 bool const& GetBoolean() const & { return boolean_; }
291 bool& GetBoolean() & { return boolean_; }
292
293 bool operator==(Value const& rhs) const override;
294 Value& operator=(Value const& rhs) override;
295
296 static bool IsClassOf(Value const* value) {
297 return value->Type() == ValueKind::kBoolean;
298 }
299 };
300
301 struct StringView {
302 private:
303 using CharT = char; // unsigned char
304 using Traits = std::char_traits<CharT>;
305 CharT const* str_;
306 size_t size_;
307
308 public:
309 StringView() = default;
310 StringView(CharT const* str, size_t size) : str_{str}, size_{size} {}
311 explicit StringView(std::string const& str): str_{str.c_str()}, size_{str.size()} {}
312 explicit StringView(CharT const* str) : str_{str}, size_{Traits::length(str)} {}
313
314 CharT const& operator[](size_t p) const { return str_[p]; }
315 CharT const& at(size_t p) const { // NOLINT
316 CHECK_LT(p, size_);
317 return str_[p];
318 }
319 size_t size() const { return size_; } // NOLINT
320 // Copies a portion of string. Since we don't have std::from_chars and friends here, so
321 // copying substring is necessary for appending `\0`. It's not too bad since string by
322 // default has small vector optimization, which is enabled by most if not all modern
323 // compilers for numeric values.
324 std::string substr(size_t beg, size_t n) const { // NOLINT
325 CHECK_LE(beg, size_);
326 return std::string {str_ + beg, n < (size_ - beg) ? n : (size_ - beg)};
327 }
328 CharT const* c_str() const { return str_; } // NOLINT
329
330 CharT const* cbegin() const { return str_; } // NOLINT
331 CharT const* cend() const { return str_ + size(); } // NOLINT
332 CharT const* begin() const { return str_; } // NOLINT
333 CharT const* end() const { return str_ + size(); } // NOLINT
334 };
335
336 std::ostream &operator<<(std::ostream &os, StringView const v);
337
355 class Json {
356 friend JsonWriter;
357
358 public:
360 static Json Load(StringView str);
362 static Json Load(JsonReader* reader);
363 static void Dump(Json json, std::string* out);
364
365 Json() : ptr_{new JsonNull} {}
366
367 // number
368 explicit Json(JsonNumber number) : ptr_{new JsonNumber(std::move(number))} {}
369 Json& operator=(JsonNumber number) {
370 ptr_.reset(new JsonNumber(std::move(number)));
371 return *this;
372 }
373
374 // integer
375 explicit Json(JsonInteger integer) : ptr_{new JsonInteger(std::move(integer))} {}
376 Json& operator=(JsonInteger integer) {
377 ptr_.reset(new JsonInteger(std::move(integer)));
378 return *this;
379 }
380
381 // array
382 explicit Json(JsonArray list) :
383 ptr_ {new JsonArray(std::move(list))} {}
384 Json& operator=(JsonArray array) {
385 ptr_.reset(new JsonArray(std::move(array)));
386 return *this;
387 }
388
389 // object
390 explicit Json(JsonObject object) :
391 ptr_{new JsonObject(std::move(object))} {}
392 Json& operator=(JsonObject object) {
393 ptr_.reset(new JsonObject(std::move(object)));
394 return *this;
395 }
396 // string
397 explicit Json(JsonString str) :
398 ptr_{new JsonString(std::move(str))} {}
399 Json& operator=(JsonString str) {
400 ptr_.reset(new JsonString(std::move(str)));
401 return *this;
402 }
403 // bool
404 explicit Json(JsonBoolean boolean) :
405 ptr_{new JsonBoolean(std::move(boolean))} {}
406 Json& operator=(JsonBoolean boolean) {
407 ptr_.reset(new JsonBoolean(std::move(boolean)));
408 return *this;
409 }
410 // null
411 explicit Json(JsonNull null) :
412 ptr_{new JsonNull(std::move(null))} {}
413 Json& operator=(JsonNull null) {
414 ptr_.reset(new JsonNull(std::move(null)));
415 return *this;
416 }
417
418 // copy
419 Json(Json const& other) = default;
420 Json& operator=(Json const& other);
421 // move
422 Json(Json&& other) : ptr_{std::move(other.ptr_)} {}
423 Json& operator=(Json&& other) {
424 ptr_ = std::move(other.ptr_);
425 return *this;
426 }
427
429 Json& operator[](std::string const & key) const { return (*ptr_)[key]; }
431 Json& operator[](int ind) const { return (*ptr_)[ind]; }
432
434 Value const& GetValue() const & { return *ptr_; }
435 Value const& GetValue() && { return *ptr_; }
436 Value& GetValue() & { return *ptr_; }
437
438 bool operator==(Json const& rhs) const {
439 return *ptr_ == *(rhs.ptr_);
440 }
441
442 friend std::ostream& operator<<(std::ostream& os, Json const& j) {
443 std::string str;
444 Json::Dump(j, &str);
445 os << str;
446 return os;
447 }
448
449 private:
450 IntrusivePtr<Value> ptr_;
451 };
452
453 template <typename T>
454 bool IsA(Json const j) {
455 auto const& v = j.GetValue();
456 return IsA<T>(&v);
457 }
458
459 namespace detail {
460
461 // Number
462 template <typename T,
463 typename std::enable_if<
464 std::is_same<T, JsonNumber>::value>::type* = nullptr>
465 JsonNumber::Float& GetImpl(T& val) { // NOLINT
466 return val.GetNumber();
467 }
468 template <typename T,
469 typename std::enable_if<
470 std::is_same<T, JsonNumber const>::value>::type* = nullptr>
471 JsonNumber::Float const& GetImpl(T& val) { // NOLINT
472 return val.GetNumber();
473 }
474
475 // Integer
476 template <typename T,
477 typename std::enable_if<
478 std::is_same<T, JsonInteger>::value>::type* = nullptr>
479 JsonInteger::Int& GetImpl(T& val) { // NOLINT
480 return val.GetInteger();
481 }
482 template <typename T,
483 typename std::enable_if<
484 std::is_same<T, JsonInteger const>::value>::type* = nullptr>
485 JsonInteger::Int const& GetImpl(T& val) { // NOLINT
486 return val.GetInteger();
487 }
488
489 // String
490 template <typename T,
491 typename std::enable_if<
492 std::is_same<T, JsonString>::value>::type* = nullptr>
493 std::string& GetImpl(T& val) { // NOLINT
494 return val.GetString();
495 }
496 template <typename T,
497 typename std::enable_if<
498 std::is_same<T, JsonString const>::value>::type* = nullptr>
499 std::string const& GetImpl(T& val) { // NOLINT
500 return val.GetString();
501 }
502
503 // Boolean
504 template <typename T,
505 typename std::enable_if<
506 std::is_same<T, JsonBoolean>::value>::type* = nullptr>
507 bool& GetImpl(T& val) { // NOLINT
508 return val.GetBoolean();
509 }
510 template <typename T,
511 typename std::enable_if<
512 std::is_same<T, JsonBoolean const>::value>::type* = nullptr>
513 bool const& GetImpl(T& val) { // NOLINT
514 return val.GetBoolean();
515 }
516
517 // Array
518 template <typename T,
519 typename std::enable_if<
520 std::is_same<T, JsonArray>::value>::type* = nullptr>
521 std::vector<Json>& GetImpl(T& val) { // NOLINT
522 return val.GetArray();
523 }
524 template <typename T,
525 typename std::enable_if<
526 std::is_same<T, JsonArray const>::value>::type* = nullptr>
527 std::vector<Json> const& GetImpl(T& val) { // NOLINT
528 return val.GetArray();
529 }
530
531 // Object
532 template <typename T,
533 typename std::enable_if<
534 std::is_same<T, JsonObject>::value>::type* = nullptr>
535 std::map<std::string, Json>& GetImpl(T& val) { // NOLINT
536 return val.GetObject();
537 }
538 template <typename T,
539 typename std::enable_if<
540 std::is_same<T, JsonObject const>::value>::type* = nullptr>
541 std::map<std::string, Json> const& GetImpl(T& val) { // NOLINT
542 return val.GetObject();
543 }
544
545 } // namespace detail
546
555 template <typename T, typename U>
556 auto get(U& json) -> decltype(detail::GetImpl(*Cast<T>(&json.GetValue())))& { // NOLINT
557 auto& value = *Cast<T>(&json.GetValue());
558 return detail::GetImpl(value);
559 }
560
561 using Object = JsonObject;
562 using Array = JsonArray;
563 using Number = JsonNumber;
564 using Integer = JsonInteger;
565 using Boolean = JsonBoolean;
566 using String = JsonString;
567 using Null = JsonNull;
568
569 // Utils tailored for XGBoost.
570 template <typename Parameter>
571 Object ToJson(Parameter const& param) {
572 Object obj;
573 for (auto const& kv : param.__DICT__()) {
574 obj[kv.first] = kv.second;
575 }
576 return obj;
577 }
578
579 template <typename Parameter>
580 Args FromJson(Json const& obj, Parameter* param) {
581 auto const& j_param = get<Object const>(obj);
582 std::map<std::string, std::string> m;
583 for (auto const& kv : j_param) {
584 m[kv.first] = get<String const>(kv.second);
585 }
586 return param->UpdateAllowUnknown(m);
587 }
588 } // namespace xgboost
589 #endif // XGBOOST_JSON_H_
xgboost::JsonNull::Save
void Save(JsonWriter *writer) override
xgboost::JsonBoolean
Describes both true and false.
Definition: json.h:269
xgboost::FromJson
Args FromJson(Json const &obj, Parameter *param)
Definition: json.h:580
xgboost::IntrusivePtr
Implementation of Intrusive Pointer. A smart pointer that points to an object with an embedded refere...
Definition: intrusive_ptr.h:72
xgboost::Json::operator=
Json & operator=(JsonBoolean boolean)
Definition: json.h:406
xgboost::Json::operator[]
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:429
xgboost::Json::operator=
Json & operator=(JsonNumber number)
Definition: json.h:369
xgboost::JsonNumber::GetNumber
Float & GetNumber() &
Definition: json.h:191
xgboost::JsonInteger::JsonInteger
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:230
xgboost::Args
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:262
xgboost::JsonBoolean::Save
void Save(JsonWriter *writer) override
xgboost::detail::GetImpl
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:465
xgboost::JsonNumber
Definition: json.h:163
xgboost::JsonBoolean::operator=
Value & operator=(Value const &rhs) override
xgboost::JsonBoolean::JsonBoolean
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:281
xgboost::JsonInteger::Save
void Save(JsonWriter *writer) override
xgboost::JsonBoolean::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:296
xgboost::JsonArray::GetArray
std::vector< Json > & GetArray() &
Definition: json.h:126
xgboost::StringView::operator[]
CharT const & operator[](size_t p) const
Definition: json.h:314
xgboost::Value::ValueKind::kNull
@ kNull
xgboost::JsonInteger::JsonInteger
JsonInteger()
Definition: json.h:210
xgboost::JsonString::operator=
Value & operator=(Value const &rhs) override
xgboost::StringView::StringView
StringView()=default
xgboost::JsonNull::operator=
Value & operator=(Value const &rhs) override
xgboost::JsonObject::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:157
xgboost::StringView::StringView
StringView(CharT const *str, size_t size)
Definition: json.h:310
xgboost::StringView
Definition: json.h:301
xgboost::Json::Dump
static void Dump(Json json, std::string *out)
xgboost::Value::ValueKind
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:34
parameter.h
macro for using C++11 enum class as DMLC parameter
xgboost::JsonNull::JsonNull
JsonNull(JsonNull &&) noexcept
Definition: json.h:253
xgboost::StringView::StringView
StringView(CharT const *str)
Definition: json.h:312
xgboost::Value::operator==
virtual bool operator==(Value const &rhs) const =0
xgboost::JsonNull::operator[]
Json & operator[](std::string const &key) override
xgboost::JsonString::GetString
std::string & GetString() &
Definition: json.h:97
xgboost::Json::Json
Json(JsonBoolean boolean)
Definition: json.h:404
xgboost::JsonObject::operator==
bool operator==(Value const &rhs) const override
xgboost::Value::TypeStr
std::string TypeStr() const
xgboost::Json::operator=
Json & operator=(JsonObject object)
Definition: json.h:392
xgboost::JsonString::operator==
bool operator==(Value const &rhs) const override
xgboost::JsonNumber::Float
float Float
Definition: json.h:165
xgboost::Json::operator==
bool operator==(Json const &rhs) const
Definition: json.h:438
xgboost::Value::operator[]
virtual Json & operator[](std::string const &key)=0
xgboost::JsonReader
Definition: json_io.h:23
xgboost::StringView::substr
std::string substr(size_t beg, size_t n) const
Definition: json.h:324
xgboost::JsonObject::GetObject
std::map< std::string, Json > const & GetObject() const &
Definition: json.h:151
xgboost::Json::GetValue
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:434
xgboost::JsonArray::operator[]
Json & operator[](std::string const &key) override
xgboost::operator<<
std::basic_ostream< E, T > & operator<<(std::basic_ostream< E, T > &os, IntrusivePtr< Y > const &p)
Definition: intrusive_ptr.h:199
intrusive_ptr.h
Implementation of Intrusive Ptr.
xgboost::Json::operator=
Json & operator=(Json &&other)
Definition: json.h:423
xgboost::JsonInteger::Int
int64_t Int
Definition: json.h:204
xgboost::StringView::cend
CharT const * cend() const
Definition: json.h:331
xgboost::StringView::cbegin
CharT const * cbegin() const
Definition: json.h:330
xgboost::JsonObject::operator[]
Json & operator[](std::string const &key) override
xgboost::JsonInteger::GetInteger
Int const & GetInteger() &&
Definition: json.h:239
xgboost::JsonBoolean::operator==
bool operator==(Value const &rhs) const override
xgboost::Json::operator=
Json & operator=(JsonArray array)
Definition: json.h:384
xgboost::Json::operator=
Json & operator=(JsonNull null)
Definition: json.h:413
xgboost::Value::~Value
virtual ~Value()=default
xgboost::Value::ValueKind::kInteger
@ kInteger
xgboost::JsonNull::JsonNull
JsonNull(std::nullptr_t)
Definition: json.h:252
xgboost::JsonNumber::Save
void Save(JsonWriter *writer) override
xgboost::JsonArray::JsonArray
JsonArray(std::vector< Json > const &arr)
Definition: json.h:114
xgboost::Json::Json
Json(JsonNull null)
Definition: json.h:411
xgboost::JsonNumber::operator==
bool operator==(Value const &rhs) const override
xgboost::JsonArray::operator=
Value & operator=(Value const &rhs) override
xgboost::Value::operator=
virtual Value & operator=(Value const &rhs)=0
xgboost::JsonBoolean::GetBoolean
bool const & GetBoolean() const &
Definition: json.h:290
xgboost::JsonArray::operator==
bool operator==(Value const &rhs) const override
xgboost::JsonNull
Definition: json.h:249
xgboost::JsonNumber::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:197
xgboost::Value::Save
virtual void Save(JsonWriter *writer)=0
xgboost::JsonString::GetString
std::string const & GetString() const &
Definition: json.h:96
xgboost::JsonNumber::JsonNumber
JsonNumber()
Definition: json.h:171
xgboost::Json::Json
Json(JsonString str)
Definition: json.h:397
xgboost::JsonBoolean::GetBoolean
bool & GetBoolean() &
Definition: json.h:291
xgboost::Value::ValueKind::kObject
@ kObject
xgboost::Json::Json
Json(JsonNumber number)
Definition: json.h:368
xgboost::JsonString::JsonString
JsonString(std::string &&str)
Definition: json.h:85
xgboost::Json::Json
Json(JsonObject object)
Definition: json.h:390
xgboost::Json::operator[]
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:431
xgboost::Json::GetValue
Value const & GetValue() &&
Definition: json.h:435
xgboost::JsonNumber::GetNumber
Float const & GetNumber() const &
Definition: json.h:190
xgboost::JsonString::Save
void Save(JsonWriter *writer) override
xgboost::JsonInteger::operator=
Value & operator=(Value const &rhs) override
xgboost::JsonString::JsonString
JsonString(std::string const &str)
Definition: json.h:83
xgboost::Value::Value
Value(ValueKind _kind)
Definition: json.h:44
xgboost::JsonInteger::GetInteger
Int & GetInteger() &
Definition: json.h:241
xgboost::JsonInteger::operator[]
Json & operator[](std::string const &key) override
xgboost::StringView::at
CharT const & at(size_t p) const
Definition: json.h:315
xgboost::Cast
T * Cast(U *value)
Definition: json.h:69
xgboost::JsonNull::operator==
bool operator==(Value const &rhs) const override
xgboost::Json::Json
Json(Json &&other)
Definition: json.h:422
xgboost::JsonNumber::operator=
Value & operator=(Value const &rhs) override
xgboost::Value::IntrusivePtrRefCount
friend IntrusivePtrCell & IntrusivePtrRefCount(xgboost::Value const *t) noexcept
Definition: json.h:28
xgboost::JsonBoolean::GetBoolean
bool const & GetBoolean() &&
Definition: json.h:289
xgboost::JsonInteger::operator==
bool operator==(Value const &rhs) const override
xgboost::JsonBoolean::operator[]
Json & operator[](std::string const &key) override
xgboost::JsonInteger::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:244
xgboost::JsonBoolean::JsonBoolean
JsonBoolean()
Definition: json.h:273
xgboost::JsonArray::JsonArray
JsonArray()
Definition: json.h:111
xgboost::Value::ValueKind::kBoolean
@ kBoolean
xgboost::Json::Json
Json(JsonArray list)
Definition: json.h:382
xgboost::IntrusivePtrCell
Helper class for embedding reference counting into client objects. See https://www....
Definition: intrusive_ptr.h:19
xgboost::JsonObject::JsonObject
JsonObject()
Definition: json.h:140
xgboost::Value::ValueKind::kString
@ kString
xgboost::JsonString::JsonString
JsonString(JsonString &&str) noexcept
Definition: json.h:87
xgboost::JsonObject::Save
void Save(JsonWriter *writer) override
xgboost::JsonString::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:102
xgboost::JsonString::GetString
std::string const & GetString() &&
Definition: json.h:95
xgboost::Json::Json
Json(JsonInteger integer)
Definition: json.h:375
xgboost::JsonArray::GetArray
std::vector< Json > const & GetArray() &&
Definition: json.h:124
xgboost::JsonBoolean::JsonBoolean
JsonBoolean(Bool value)
Definition: json.h:279
xgboost::JsonInteger
Definition: json.h:202
xgboost::JsonNumber::JsonNumber
JsonNumber(FloatT value)
Definition: json.h:174
xgboost::Json::Load
static Json Load(StringView str)
Load a Json object from string.
xgboost::JsonString::operator[]
Json & operator[](std::string const &key) override
xgboost::JsonInteger::GetInteger
Int const & GetInteger() const &
Definition: json.h:240
xgboost::JsonNumber::JsonNumber
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:182
xgboost::JsonObject::GetObject
std::map< std::string, Json > const & GetObject() &&
Definition: json.h:150
xgboost::JsonInteger::JsonInteger
JsonInteger(IntT value)
Definition: json.h:213
xgboost::StringView::end
CharT const * end() const
Definition: json.h:333
xgboost::Json::GetValue
Value & GetValue() &
Definition: json.h:436
xgboost::JsonNumber::operator[]
Json & operator[](std::string const &key) override
xgboost::Value::Type
ValueKind Type() const
Definition: json.h:46
xgboost::JsonNumber::GetNumber
Float const & GetNumber() &&
Definition: json.h:189
xgboost::JsonString::JsonString
JsonString()
Definition: json.h:82
xgboost::JsonObject::operator=
Value & operator=(Value const &rhs) override
xgboost::JsonNull::JsonNull
JsonNull()
Definition: json.h:251
xgboost::Json::operator<<
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:442
xgboost::JsonObject
Definition: json.h:136
xgboost::JsonArray
Definition: json.h:107
xgboost::Value::ValueKind::kArray
@ kArray
xgboost::Json::operator=
Json & operator=(JsonString str)
Definition: json.h:399
xgboost::JsonString
Definition: json.h:78
xgboost::ToJson
Object ToJson(Parameter const &param)
Definition: json.h:571
xgboost::JsonArray::Save
void Save(JsonWriter *writer) override
xgboost::Json::operator=
Json & operator=(JsonInteger integer)
Definition: json.h:376
xgboost::StringView::c_str
CharT const * c_str() const
Definition: json.h:328
xgboost::JsonWriter
Definition: json_io.h:115
xgboost::Value
Definition: json.h:24
xgboost::JsonNull::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:263
xgboost::Json
Data structure representing JSON format.
Definition: json.h:355
xgboost::JsonArray::IsClassOf
static bool IsClassOf(Value const *value)
Definition: json.h:131
xgboost::IsA
bool IsA(Value const *value)
Definition: json.h:64
xgboost::get
auto get(U &json) -> decltype(detail::GetImpl(*Cast< T >(&json.GetValue())))&
Get Json value.
Definition: json.h:556
xgboost::StringView::size
size_t size() const
Definition: json.h:319
xgboost::JsonObject::~JsonObject
~JsonObject() override=default
xgboost::JsonArray::GetArray
std::vector< Json > const & GetArray() const &
Definition: json.h:125
xgboost::Value::ValueKind::kNumber
@ kNumber
xgboost::Json::Json
Json()
Definition: json.h:365
xgboost::StringView::StringView
StringView(std::string const &str)
Definition: json.h:311
xgboost::JsonArray::JsonArray
JsonArray(std::vector< Json > &&arr)
Definition: json.h:112
xgboost::JsonObject::GetObject
std::map< std::string, Json > & GetObject() &
Definition: json.h:152
xgboost
namespace of xgboost
Definition: base.h:110
xgboost::StringView::begin
CharT const * begin() const
Definition: json.h:332