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