4 #ifndef XGBOOST_JSON_H_
5 #define XGBOOST_JSON_H_
8 #include <xgboost/logging.h>
16 #include <type_traits>
73 return T::IsClassOf(value);
76 template <
typename T,
typename U>
79 return dynamic_cast<T*
>(value);
81 LOG(FATAL) <<
"Invalid cast, from " + value->TypeStr() +
" to " + T().TypeStr();
83 return dynamic_cast<T*
>(value);
114 std::vector<Json> vec_;
131 std::vector<Json>
const&
GetArray() && {
return vec_; }
132 std::vector<Json>
const&
GetArray() const & {
return vec_; }
149 template <
typename T, Value::ValueKind kind>
163 void Set(
size_t i, T v) { vec_[i] = v; }
164 size_t Size()
const {
return vec_.size(); }
168 std::vector<T>
const&
GetArray() && {
return vec_; }
169 std::vector<T>
const&
GetArray() const& {
return vec_; }
198 using Map = std::map<std::string, Json, std::less<>>;
235 template <
typename FloatT,
236 typename std::enable_if<std::is_same<FloatT, Float>::value>::type* =
nullptr>
240 template <
typename FloatT,
241 typename std::enable_if<std::is_same<FloatT, double>::value>::type* =
nullptr>
243 number_{static_cast<
Float>(value)} {}
270 template <
typename IntT,
271 typename std::enable_if<std::is_same<IntT, Int>::value>::type* =
nullptr>
273 template <
typename IntT,
274 typename std::enable_if<std::is_same<IntT, size_t>::value>::type* =
nullptr>
276 integer_{static_cast<
Int>(value)} {}
277 template <
typename IntT,
278 typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* =
nullptr>
280 integer_{static_cast<
Int>(value)} {}
281 template <
typename IntT,
282 typename std::enable_if<
283 std::is_same<IntT, uint32_t>::value &&
284 !std::is_same<std::size_t, uint32_t>::value>::type * =
nullptr>
287 integer_{static_cast<
Int>(value)} {}
323 bool boolean_ =
false;
328 template <
typename Bool,
329 typename std::enable_if<
330 std::is_same<Bool, bool>::value ||
331 std::is_same<Bool, bool const>::value>::type* =
nullptr>
381 static void Dump(
Json json, std::string* out, std::ios::openmode mode = std::ios::out);
382 static void Dump(
Json json, std::vector<char>* out, std::ios::openmode mode = std::ios::out);
386 template <
typename Container = std::
string>
388 if constexpr (std::is_same_v<Container, std::string>) {
393 std::vector<char> str;
404 ptr_.reset(
new JsonNumber(std::move(number)));
416 ptr_.reset(
new JsonArray(std::forward<JsonArray>(array)));
420 template <
typename T, Value::ValueKind kind>
423 template <
typename T, Value::ValueKind kind>
431 ptr_.reset(
new JsonObject(std::forward<JsonObject>(
object)));
437 ptr_.reset(
new JsonString(std::forward<JsonString>(str)));
451 ptr_.reset(
new JsonNull(std::move(
null)));
476 return *ptr_ == *(rhs.ptr_);
501 template <
typename T>
509 template <
typename T,
510 typename std::enable_if<
511 std::is_same<T, JsonNumber>::value>::type* =
nullptr>
513 return val.GetNumber();
515 template <
typename T,
516 typename std::enable_if<
517 std::is_same<T, JsonNumber const>::value>::type* =
nullptr>
519 return val.GetNumber();
523 template <
typename T,
524 typename std::enable_if<
525 std::is_same<T, JsonInteger>::value>::type* =
nullptr>
527 return val.GetInteger();
529 template <
typename T,
530 typename std::enable_if<
531 std::is_same<T, JsonInteger const>::value>::type* =
nullptr>
533 return val.GetInteger();
537 template <
typename T,
538 typename std::enable_if<
539 std::is_same<T, JsonString>::value>::type* =
nullptr>
541 return val.GetString();
543 template <
typename T,
544 typename std::enable_if<
545 std::is_same<T, JsonString const>::value>::type* =
nullptr>
547 return val.GetString();
551 template <
typename T,
552 typename std::enable_if<
553 std::is_same<T, JsonBoolean>::value>::type* =
nullptr>
555 return val.GetBoolean();
557 template <
typename T,
558 typename std::enable_if<
559 std::is_same<T, JsonBoolean const>::value>::type* =
nullptr>
561 return val.GetBoolean();
565 template <
typename T,
566 typename std::enable_if<
567 std::is_same<T, JsonArray>::value>::type* =
nullptr>
569 return val.GetArray();
571 template <
typename T,
572 typename std::enable_if<
573 std::is_same<T, JsonArray const>::value>::type* =
nullptr>
575 return val.GetArray();
579 template <
typename T, Value::ValueKind kind>
583 template <
typename T, Value::ValueKind kind>
589 template <typename T, typename std::enable_if<std::is_same<T, JsonObject>::value>::type* =
nullptr>
591 return val.GetObject();
593 template <
typename T,
594 typename std::enable_if<std::is_same<T, JsonObject const>::value>::type* =
nullptr>
596 return val.GetObject();
608 template <
typename T,
typename U>
610 auto& value = *Cast<T>(&json.GetValue());
631 template <
typename Parameter>
634 for (
auto const& kv : param.__DICT__()) {
635 obj[kv.first] = kv.second;
650 template <
typename Parameter>
652 auto const& j_param = get<Object const>(obj);
654 for (
auto const& kv : j_param) {
655 args.emplace_back(kv.first, get<String const>(kv.second));
657 return param->UpdateAllowUnknown(args);
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
JsonArray(std::vector< Json > const &arr)
Definition: json.h:120
JsonArray(JsonArray const &that)=delete
Json & operator[](std::string const &key) override
Definition: json.h:129
std::vector< Json > const & GetArray() &&
Definition: json.h:131
std::vector< Json > & GetArray() &
Definition: json.h:133
JsonArray(std::vector< Json > &&arr) noexcept
Definition: json.h:118
JsonArray(JsonArray &&that) noexcept
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonArray()
Definition: json.h:117
Json & operator[](int ind) override
Definition: json.h:127
Value & operator=(Value const &rhs) override=delete
static bool IsClassOf(Value const *value)
Definition: json.h:138
std::vector< Json > const & GetArray() const &
Definition: json.h:132
Describes both true and false.
Definition: json.h:322
bool const & GetBoolean() const &
Definition: json.h:340
static bool IsClassOf(Value const *value)
Definition: json.h:346
Value & operator=(Value const &rhs) override=delete
bool const & GetBoolean() &&
Definition: json.h:339
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonBoolean()
Definition: json.h:326
JsonBoolean(Bool value)
Definition: json.h:332
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:334
bool & GetBoolean() &
Definition: json.h:341
void Save(JsonWriter *writer) const override
JsonInteger(IntT value)
Definition: json.h:272
int64_t Int
Definition: json.h:263
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:289
Value & operator=(Value const &rhs) override=delete
Int const & GetInteger() const &
Definition: json.h:296
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:300
JsonInteger()
Definition: json.h:269
Int & GetInteger() &
Definition: json.h:297
Int const & GetInteger() &&
Definition: json.h:295
JsonNull(std::nullptr_t)
Definition: json.h:308
JsonNull()
Definition: json.h:307
bool operator==(Value const &rhs) const override
JsonNull(JsonNull &&) noexcept
Definition: json.h:309
Value & operator=(Value const &rhs) override=delete
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:316
Float const & GetNumber() const &
Definition: json.h:250
Float const & GetNumber() &&
Definition: json.h:249
Value & operator=(Value const &rhs) override=delete
JsonNumber(JsonNumber const &that)=delete
Float & GetNumber() &
Definition: json.h:251
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:245
bool operator==(Value const &rhs) const override
float Float
Definition: json.h:228
JsonNumber()
Definition: json.h:234
static bool IsClassOf(Value const *value)
Definition: json.h:256
JsonNumber(FloatT value)
Definition: json.h:237
void Save(JsonWriter *writer) const override
Json & operator[](int ind) override
Definition: json.h:212
Map const & GetObject() &&
Definition: json.h:215
JsonObject(JsonObject &&that) noexcept
void Save(JsonWriter *writer) const override
Map const & GetObject() const &
Definition: json.h:216
Json & operator[](std::string const &key) override
Definition: json.h:213
bool operator==(Value const &rhs) const override
~JsonObject() override=default
std::map< std::string, Json, std::less<> > Map
Definition: json.h:198
Value & operator=(Value const &rhs) override=delete
Map & GetObject() &
Definition: json.h:217
JsonObject(JsonObject const &that)=delete
static bool IsClassOf(Value const *value)
Definition: json.h:222
JsonObject()
Definition: json.h:204
JsonObject(Map &&object) noexcept
A json reader, currently error checking and utf-8 is not fully supported.
Definition: json_io.h:23
static bool IsClassOf(Value const *value)
Definition: json.h:108
JsonString(std::string &&str) noexcept
Definition: json.h:93
void Save(JsonWriter *writer) const override
std::string const & GetString() &&
Definition: json.h:101
std::string & GetString() &
Definition: json.h:103
bool operator==(Value const &rhs) const override
Value & operator=(Value const &rhs) override=delete
JsonString(std::string const &str)
Definition: json.h:91
JsonString(JsonString &&str) noexcept
Definition: json.h:95
std::string const & GetString() const &
Definition: json.h:102
JsonString()
Definition: json.h:90
Typed array for Universal Binary JSON.
Definition: json.h:150
void Set(size_t i, T v)
Definition: json.h:163
std::vector< T > & GetArray() &
Definition: json.h:170
std::vector< T > const & GetArray() &&
Definition: json.h:168
size_t Size() const
Definition: json.h:164
T Type
Definition: json.h:154
void Save(JsonWriter *writer) const override
bool operator==(Value const &rhs) const override
JsonTypedArray(std::size_t n)
Definition: json.h:157
Value & operator=(Value const &rhs) override=delete
static bool IsClassOf(Value const *value)
Definition: json.h:172
JsonTypedArray(JsonTypedArray &&that) noexcept
Definition: json.h:158
JsonTypedArray()
Definition: json.h:156
std::vector< T > const & GetArray() const &
Definition: json.h:169
Definition: json_io.h:113
Data structure representing JSON format.
Definition: json.h:368
Json & operator=(JsonNumber number)
Definition: json.h:403
Json(JsonNull null)
Definition: json.h:448
Json & operator=(JsonTypedArray< T, kind > &&array)
Definition: json.h:424
Json & operator=(JsonString &&str)
Definition: json.h:436
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:466
Json(JsonArray &&list)
Definition: json.h:414
Json(JsonObject &&object)
Definition: json.h:429
static void Dump(Json json, std::vector< char > *out, std::ios::openmode mode=std::ios::out)
Json(Json &&other) noexcept
Definition: json.h:459
Json(Json const &other)=default
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:471
Json & operator=(Json const &other)=default
Json(JsonString &&str)
Definition: json.h:435
Json(JsonInteger integer)
Definition: json.h:408
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:468
Json & operator=(JsonObject &&object)
Definition: json.h:430
Json & operator=(JsonArray &&array)
Definition: json.h:415
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:479
Json & operator=(JsonInteger integer)
Definition: json.h:409
Json & operator=(JsonBoolean boolean)
Definition: json.h:443
static Json Load(JsonReader *reader)
Pass your own JsonReader.
Json(JsonBoolean boolean)
Definition: json.h:441
IntrusivePtr< Value > const & Ptr() const
Definition: json.h:486
Json(JsonTypedArray< T, kind > &&list)
Definition: json.h:421
Json(JsonNumber number)
Definition: json.h:402
Json & operator=(JsonNull null)
Definition: json.h:450
static Container Dump(Json json)
Definition: json.h:387
bool operator==(Json const &rhs) const
Definition: json.h:475
static void Dump(Json json, JsonWriter *writer)
Use your own JsonWriter.
Json & operator=(Json &&other) noexcept
Definition: json.h:460
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:472
Value & GetValue() &
Definition: json.h:473
virtual Value & operator=(Value const &rhs)=delete
virtual bool operator==(Value const &rhs) const =0
Value(ValueKind _kind)
Definition: json.h:52
virtual Json & operator[](std::string const &key)
virtual Json & operator[](int ind)
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:36
friend IntrusivePtrCell & IntrusivePtrRefCount(xgboost::Value const *t) noexcept
Definition: json.h:30
ValueKind Type() const
Definition: json.h:54
std::string TypeStr() const
virtual void Save(JsonWriter *writer) const =0
Implementation of Intrusive Ptr.
Definition: intrusive_ptr.h:207
void swap(xgboost::IntrusivePtr< T > &x, xgboost::IntrusivePtr< T > &y) noexcept
Definition: intrusive_ptr.h:209
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:512
Core data structure for multi-target trees.
Definition: base.h:87
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:310
bool IsA(Value const *value)
Definition: json.h:72
auto get(U &json) -> decltype(detail::GetImpl(*Cast< T >(&json.GetValue())))&
Get Json value.
Definition: json.h:609
T * Cast(U *value)
Definition: json.h:77
Object ToJson(Parameter const ¶m)
Convert XGBoost parameter to JSON object.
Definition: json.h:632
Args FromJson(Json const &obj, Parameter *param)
Load a XGBoost parameter from a JSON object.
Definition: json.h:651
macro for using C++11 enum class as DMLC parameter
Definition: string_view.h:16