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 CharT const* str_;
305 size_t size_;
306
307 public:
308 StringView() = default;
309 StringView(CharT const* str, size_t size) : str_{str}, size_{size} {}
310
311 CharT const& operator[](size_t p) const { return str_[p]; }
312 CharT const& at(size_t p) const { // NOLINT
313 CHECK_LT(p, size_);
314 return str_[p];
315 }
316 size_t size() const { return size_; } // NOLINT
317 // Copies a portion of string. Since we don't have std::from_chars and friends here, so
318 // copying substring is necessary for appending `\0`. It's not too bad since string by
319 // default has small vector optimization, which is enabled by most if not all modern
320 // compilers for numeric values.
321 std::string substr(size_t beg, size_t n) const { // NOLINT
322 CHECK_LE(beg, size_);
323 return std::string {str_ + beg, n < (size_ - beg) ? n : (size_ - beg)};
324 }
325 char const* c_str() const { return str_; } // NOLINT
326 };
327
345 class Json {
346 friend JsonWriter;
347
348 public:
350 static Json Load(StringView str);
352 static Json Load(JsonReader* reader);
353 static void Dump(Json json, std::string* out);
354
355 Json() : ptr_{new JsonNull} {}
356
357 // number
358 explicit Json(JsonNumber number) : ptr_{new JsonNumber(std::move(number))} {}
359 Json& operator=(JsonNumber number) {
360 ptr_.reset(new JsonNumber(std::move(number)));
361 return *this;
362 }
363
364 // integer
365 explicit Json(JsonInteger integer) : ptr_{new JsonInteger(std::move(integer))} {}
366 Json& operator=(JsonInteger integer) {
367 ptr_.reset(new JsonInteger(std::move(integer)));
368 return *this;
369 }
370
371 // array
372 explicit Json(JsonArray list) :
373 ptr_ {new JsonArray(std::move(list))} {}
374 Json& operator=(JsonArray array) {
375 ptr_.reset(new JsonArray(std::move(array)));
376 return *this;
377 }
378
379 // object
380 explicit Json(JsonObject object) :
381 ptr_{new JsonObject(std::move(object))} {}
382 Json& operator=(JsonObject object) {
383 ptr_.reset(new JsonObject(std::move(object)));
384 return *this;
385 }
386 // string
387 explicit Json(JsonString str) :
388 ptr_{new JsonString(std::move(str))} {}
389 Json& operator=(JsonString str) {
390 ptr_.reset(new JsonString(std::move(str)));
391 return *this;
392 }
393 // bool
394 explicit Json(JsonBoolean boolean) :
395 ptr_{new JsonBoolean(std::move(boolean))} {}
396 Json& operator=(JsonBoolean boolean) {
397 ptr_.reset(new JsonBoolean(std::move(boolean)));
398 return *this;
399 }
400 // null
401 explicit Json(JsonNull null) :
402 ptr_{new JsonNull(std::move(null))} {}
403 Json& operator=(JsonNull null) {
404 ptr_.reset(new JsonNull(std::move(null)));
405 return *this;
406 }
407
408 // copy
409 Json(Json const& other) = default;
410 Json& operator=(Json const& other);
411 // move
412 Json(Json&& other) : ptr_{std::move(other.ptr_)} {}
413 Json& operator=(Json&& other) {
414 ptr_ = std::move(other.ptr_);
415 return *this;
416 }
417
419 Json& operator[](std::string const & key) const { return (*ptr_)[key]; }
421 Json& operator[](int ind) const { return (*ptr_)[ind]; }
422
424 Value const& GetValue() const & { return *ptr_; }
425 Value const& GetValue() && { return *ptr_; }
426 Value& GetValue() & { return *ptr_; }
427
428 bool operator==(Json const& rhs) const {
429 return *ptr_ == *(rhs.ptr_);
430 }
431
432 friend std::ostream& operator<<(std::ostream& os, Json const& j) {
433 std::string str;
434 Json::Dump(j, &str);
435 os << str;
436 return os;
437 }
438
439 private:
440 IntrusivePtr<Value> ptr_;
441 };
442
443 template <typename T>
444 bool IsA(Json const j) {
445 auto const& v = j.GetValue();
446 return IsA<T>(&v);
447 }
448
449 namespace detail {
450
451 // Number
452 template <typename T,
453 typename std::enable_if<
454 std::is_same<T, JsonNumber>::value>::type* = nullptr>
455 JsonNumber::Float& GetImpl(T& val) { // NOLINT
456 return val.GetNumber();
457 }
458 template <typename T,
459 typename std::enable_if<
460 std::is_same<T, JsonNumber const>::value>::type* = nullptr>
461 JsonNumber::Float const& GetImpl(T& val) { // NOLINT
462 return val.GetNumber();
463 }
464
465 // Integer
466 template <typename T,
467 typename std::enable_if<
468 std::is_same<T, JsonInteger>::value>::type* = nullptr>
469 JsonInteger::Int& GetImpl(T& val) { // NOLINT
470 return val.GetInteger();
471 }
472 template <typename T,
473 typename std::enable_if<
474 std::is_same<T, JsonInteger const>::value>::type* = nullptr>
475 JsonInteger::Int const& GetImpl(T& val) { // NOLINT
476 return val.GetInteger();
477 }
478
479 // String
480 template <typename T,
481 typename std::enable_if<
482 std::is_same<T, JsonString>::value>::type* = nullptr>
483 std::string& GetImpl(T& val) { // NOLINT
484 return val.GetString();
485 }
486 template <typename T,
487 typename std::enable_if<
488 std::is_same<T, JsonString const>::value>::type* = nullptr>
489 std::string const& GetImpl(T& val) { // NOLINT
490 return val.GetString();
491 }
492
493 // Boolean
494 template <typename T,
495 typename std::enable_if<
496 std::is_same<T, JsonBoolean>::value>::type* = nullptr>
497 bool& GetImpl(T& val) { // NOLINT
498 return val.GetBoolean();
499 }
500 template <typename T,
501 typename std::enable_if<
502 std::is_same<T, JsonBoolean const>::value>::type* = nullptr>
503 bool const& GetImpl(T& val) { // NOLINT
504 return val.GetBoolean();
505 }
506
507 // Array
508 template <typename T,
509 typename std::enable_if<
510 std::is_same<T, JsonArray>::value>::type* = nullptr>
511 std::vector<Json>& GetImpl(T& val) { // NOLINT
512 return val.GetArray();
513 }
514 template <typename T,
515 typename std::enable_if<
516 std::is_same<T, JsonArray const>::value>::type* = nullptr>
517 std::vector<Json> const& GetImpl(T& val) { // NOLINT
518 return val.GetArray();
519 }
520
521 // Object
522 template <typename T,
523 typename std::enable_if<
524 std::is_same<T, JsonObject>::value>::type* = nullptr>
525 std::map<std::string, Json>& GetImpl(T& val) { // NOLINT
526 return val.GetObject();
527 }
528 template <typename T,
529 typename std::enable_if<
530 std::is_same<T, JsonObject const>::value>::type* = nullptr>
531 std::map<std::string, Json> const& GetImpl(T& val) { // NOLINT
532 return val.GetObject();
533 }
534
535 } // namespace detail
536
545 template <typename T, typename U>
546 auto get(U& json) -> decltype(detail::GetImpl(*Cast<T>(&json.GetValue())))& { // NOLINT
547 auto& value = *Cast<T>(&json.GetValue());
548 return detail::GetImpl(value);
549 }
550
551 using Object = JsonObject;
552 using Array = JsonArray;
553 using Number = JsonNumber;
554 using Integer = JsonInteger;
555 using Boolean = JsonBoolean;
556 using String = JsonString;
557 using Null = JsonNull;
558
559 // Utils tailored for XGBoost.
560
561 template <typename Parameter>
562 Object ToJson(Parameter const& param) {
563 Object obj;
564 for (auto const& kv : param.__DICT__()) {
565 obj[kv.first] = kv.second;
566 }
567 return obj;
568 }
569
570 template <typename Parameter>
571 void FromJson(Json const& obj, Parameter* param) {
572 auto const& j_param = get<Object const>(obj);
573 std::map<std::string, std::string> m;
574 for (auto const& kv : j_param) {
575 m[kv.first] = get<String const>(kv.second);
576 }
577 param->UpdateAllowUnknown(m);
578 }
579 } // namespace xgboost
580 #endif // XGBOOST_JSON_H_
xgboost::JsonNull::Save
void Save(JsonWriter *writer) override
xgboost::JsonBoolean
Describes both true and false.
Definition: json.h:269
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:396
xgboost::Json::operator[]
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:419
xgboost::Json::operator=
Json & operator=(JsonNumber number)
Definition: json.h:359
xgboost::JsonNumber::GetNumber
Float & GetNumber() &
Definition: json.h:191
xgboost::JsonInteger::JsonInteger
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:230
xgboost::JsonBoolean::Save
void Save(JsonWriter *writer) override
xgboost::detail::GetImpl
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:455
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:311
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:309
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::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:394
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:382
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:428
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:321
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:424
xgboost::JsonArray::operator[]
Json & operator[](std::string const &key) override
intrusive_ptr.h
Implementation of Intrusive Ptr.
xgboost::Json::operator=
Json & operator=(Json &&other)
Definition: json.h:413
xgboost::JsonInteger::Int
int64_t Int
Definition: json.h:204
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:374
xgboost::Json::operator=
Json & operator=(JsonNull null)
Definition: json.h:403
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:401
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:387
xgboost::JsonBoolean::GetBoolean
bool & GetBoolean() &
Definition: json.h:291
xgboost::Value::ValueKind::kObject
@ kObject
xgboost::Json::Json
Json(JsonNumber number)
Definition: json.h:358
xgboost::JsonString::JsonString
JsonString(std::string &&str)
Definition: json.h:85
xgboost::Json::Json
Json(JsonObject object)
Definition: json.h:380
xgboost::Json::operator[]
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:421
xgboost::Json::GetValue
Value const & GetValue() &&
Definition: json.h:425
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:312
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:412
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:372
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:365
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::StringView::c_str
char const * c_str() const
Definition: json.h:325
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::Json::GetValue
Value & GetValue() &
Definition: json.h:426
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:432
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:389
xgboost::JsonString
Definition: json.h:78
xgboost::ToJson
Object ToJson(Parameter const &param)
Definition: json.h:562
xgboost::JsonArray::Save
void Save(JsonWriter *writer) override
xgboost::Json::operator=
Json & operator=(JsonInteger integer)
Definition: json.h:366
xgboost::JsonWriter
Definition: json_io.h:115
xgboost::FromJson
void FromJson(Json const &obj, Parameter *param)
Definition: json.h:571
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:345
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:546
xgboost::StringView::size
size_t size() const
Definition: json.h:316
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:355
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