Go to the documentation of this file.
4 #ifndef XGBOOST_JSON_H_
5 #define XGBOOST_JSON_H_
8 #include <xgboost/logging.h>
53 virtual ~Value() =
default;
71 return T::IsClassOf(value);
74 template <
typename T,
typename U>
77 return dynamic_cast<T*
>(value);
79 LOG(FATAL) <<
"Invalid cast, from " + value->TypeStr() +
" to " + T().TypeStr();
81 return dynamic_cast<T*
>(value);
111 std::vector<Json> vec_;
122 void Save(JsonWriter* writer)
const override;
128 std::vector<Json>
const&
GetArray() && {
return vec_; }
129 std::vector<Json>
const&
GetArray() const & {
return vec_; }
145 template <
typename T, Value::ValueKind kind>
158 void Set(
size_t i, T v) { vec_[i] = v; }
159 size_t Size()
const {
return vec_.size(); }
163 std::vector<T>
const&
GetArray() && {
return vec_; }
164 std::vector<T>
const&
GetArray() const& {
return vec_; }
188 std::map<std::string, Json> object_;
192 JsonObject(std::map<std::string, Json>&&
object) noexcept;
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_; }
223 template <
typename FloatT,
224 typename std::enable_if<std::is_same<FloatT, Float>::value>::type* =
nullptr>
228 template <
typename FloatT,
229 typename std::enable_if<std::is_same<FloatT, double>::value>::type* =
nullptr>
231 number_{
static_cast<Float>(value)} {}
235 void Save(JsonWriter* writer)
const override;
257 template <
typename IntT,
258 typename std::enable_if<std::is_same<IntT, Int>::value>::type* =
nullptr>
260 template <
typename IntT,
261 typename std::enable_if<std::is_same<IntT, size_t>::value>::type* =
nullptr>
263 integer_{
static_cast<Int>(value)} {}
264 template <
typename IntT,
265 typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* =
nullptr>
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>
274 integer_{
static_cast<Int>(value)} {}
308 bool boolean_ =
false;
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>
322 void Save(JsonWriter* writer)
const override;
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);
375 ptr_.reset(
new JsonNumber(std::move(number)));
387 ptr_.reset(
new JsonArray(std::forward<JsonArray>(array)));
391 template <
typename T, Value::ValueKind kind>
394 template <
typename T, Value::ValueKind kind>
402 ptr_.reset(
new JsonObject(std::forward<JsonObject>(
object)));
408 ptr_.reset(
new JsonString(std::forward<JsonString>(str)));
420 ptr_{
new JsonNull(std::move(
null))} {}
422 ptr_.reset(
new JsonNull(std::move(
null)));
447 return *ptr_ == *(rhs.ptr_);
472 template <
typename T>
480 template <
typename T,
481 typename std::enable_if<
482 std::is_same<T, JsonNumber>::value>::type* =
nullptr>
484 return val.GetNumber();
486 template <
typename T,
487 typename std::enable_if<
488 std::is_same<T, JsonNumber const>::value>::type* =
nullptr>
490 return val.GetNumber();
494 template <
typename T,
495 typename std::enable_if<
496 std::is_same<T, JsonInteger>::value>::type* =
nullptr>
498 return val.GetInteger();
500 template <
typename T,
501 typename std::enable_if<
502 std::is_same<T, JsonInteger const>::value>::type* =
nullptr>
504 return val.GetInteger();
508 template <
typename T,
509 typename std::enable_if<
510 std::is_same<T, JsonString>::value>::type* =
nullptr>
512 return val.GetString();
514 template <
typename T,
515 typename std::enable_if<
516 std::is_same<T, JsonString const>::value>::type* =
nullptr>
518 return val.GetString();
522 template <
typename T,
523 typename std::enable_if<
524 std::is_same<T, JsonBoolean>::value>::type* =
nullptr>
526 return val.GetBoolean();
528 template <
typename T,
529 typename std::enable_if<
530 std::is_same<T, JsonBoolean const>::value>::type* =
nullptr>
532 return val.GetBoolean();
536 template <
typename T,
537 typename std::enable_if<
538 std::is_same<T, JsonArray>::value>::type* =
nullptr>
540 return val.GetArray();
542 template <
typename T,
543 typename std::enable_if<
544 std::is_same<T, JsonArray const>::value>::type* =
nullptr>
546 return val.GetArray();
550 template <
typename T, Value::ValueKind kind>
554 template <
typename T, Value::ValueKind kind>
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) {
564 return val.GetObject();
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) {
570 return val.GetObject();
582 template <
typename T,
typename U>
584 auto& value = *Cast<T>(&json.GetValue());
597 template <
typename Parameter>
600 for (
auto const& kv : param.__DICT__()) {
601 obj[kv.first] = kv.second;
606 template <
typename Parameter>
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);
613 return param->UpdateAllowUnknown(m);
616 #endif // XGBOOST_JSON_H_
Describes both true and false.
Definition: json.h:307
Args FromJson(Json const &obj, Parameter *param)
Definition: json.h:607
Implementation of Intrusive Pointer. A smart pointer that points to an object with an embedded refere...
Definition: intrusive_ptr.h:73
Json & operator=(JsonBoolean boolean)
Definition: json.h:414
virtual Json & operator[](std::string const &key)
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:437
Json & operator=(JsonNumber number)
Definition: json.h:374
Float & GetNumber() &
Definition: json.h:239
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:276
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:265
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:483
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:319
static bool IsClassOf(Value const *value)
Definition: json.h:330
std::vector< Json > & GetArray() &
Definition: json.h:130
void Save(JsonWriter *writer) const override
Typed array for Universal Binary JSON.
Definition: json.h:146
JsonInteger()
Definition: json.h:256
JsonTypedArray()
Definition: json.h:152
Json & operator=(JsonString &&str)
Definition: json.h:407
static bool IsClassOf(Value const *value)
Definition: json.h:208
Definition: string_view.h:14
void Save(JsonWriter *writer) const override
void swap(xgboost::IntrusivePtr< T > &x, xgboost::IntrusivePtr< T > &y) noexcept
Definition: intrusive_ptr.h:209
JsonString(std::string &&str) noexcept
Definition: json.h:91
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:35
macro for using C++11 enum class as DMLC parameter
T Type
Definition: json.h:150
Json & operator=(JsonObject &&object)
Definition: json.h:401
JsonNull(JsonNull &&) noexcept
Definition: json.h:295
Json & operator[](int ind) override
Definition: json.h:124
virtual bool operator==(Value const &rhs) const =0
IntrusivePtr< Value > const & Ptr() const
Definition: json.h:457
std::string & GetString() &
Definition: json.h:101
bool operator==(Value const &rhs) const override
Json(JsonString &&str)
Definition: json.h:406
Json(JsonBoolean boolean)
Definition: json.h:412
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:167
bool operator==(Value const &rhs) const override
std::string TypeStr() const
bool operator==(Value const &rhs) const override
float Float
Definition: json.h:216
std::vector< T > const & GetArray() &&
Definition: json.h:163
bool operator==(Json const &rhs) const
Definition: json.h:446
size_t Size() const
Definition: json.h:159
void Save(JsonWriter *writer) const override
std::map< std::string, Json > const & GetObject() const &
Definition: json.h:203
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:442
virtual Value & operator=(Value const &rhs)=delete
Json & operator[](std::string const &key) override
Definition: json.h:126
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.
Implementation of Intrusive Ptr.
int64_t Int
Definition: json.h:250
Json & operator[](std::string const &key) override
Definition: json.h:200
Int const & GetInteger() &&
Definition: json.h:281
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
Json & operator=(JsonNull null)
Definition: json.h:421
JsonNull(std::nullptr_t)
Definition: json.h:294
JsonArray(std::vector< Json > const &arr)
Definition: json.h:117
Json(JsonNull null)
Definition: json.h:419
bool operator==(Value const &rhs) const override
bool const & GetBoolean() const &
Definition: json.h:325
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:243
std::string const & GetString() const &
Definition: json.h:100
JsonNumber()
Definition: json.h:222
bool & GetBoolean() &
Definition: json.h:326
std::vector< T > & GetArray() &
Definition: json.h:165
Json(JsonNumber number)
Definition: json.h:373
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:439
Value const & GetValue() &&
Definition: json.h:443
Float const & GetNumber() const &
Definition: json.h:238
Json(Json &&other) noexcept
Definition: json.h:430
Json(JsonArray &&list)
Definition: json.h:385
JsonString(std::string const &str)
Definition: json.h:89
Value(ValueKind _kind)
Definition: json.h:50
Int & GetInteger() &
Definition: json.h:283
T * Cast(U *value)
Definition: json.h:75
bool operator==(Value const &rhs) const override
friend IntrusivePtrCell & IntrusivePtrRefCount(xgboost::Value const *t) noexcept
Definition: json.h:29
bool const & GetBoolean() &&
Definition: json.h:324
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:286
JsonBoolean()
Definition: json.h:311
JsonArray()
Definition: json.h:114
JsonTypedArray(size_t n)
Definition: json.h:153
Helper class for embedding reference counting into client objects. See https://www....
Definition: intrusive_ptr.h:20
JsonObject()
Definition: json.h:191
Json(JsonObject &&object)
Definition: json.h:400
JsonString(JsonString &&str) noexcept
Definition: json.h:93
static bool IsClassOf(Value const *value)
Definition: json.h:105
std::string const & GetString() &&
Definition: json.h:99
Json(JsonInteger integer)
Definition: json.h:379
std::vector< Json > const & GetArray() &&
Definition: json.h:128
JsonBoolean(Bool value)
Definition: json.h:317
Json & operator=(JsonTypedArray< T, kind > &&array)
Definition: json.h:395
JsonNumber(FloatT value)
Definition: json.h:225
Json & operator=(Json &&other) noexcept
Definition: json.h:431
Int const & GetInteger() const &
Definition: json.h:282
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:233
std::map< std::string, Json > const & GetObject() &&
Definition: json.h:202
JsonInteger(IntT value)
Definition: json.h:259
Value & GetValue() &
Definition: json.h:444
ValueKind Type() const
Definition: json.h:52
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.
void Save(JsonWriter *writer) const override
Float const & GetNumber() &&
Definition: json.h:237
Json(JsonTypedArray< T, kind > &&list)
Definition: json.h:392
JsonArray(std::vector< Json > &&arr) noexcept
Definition: json.h:115
JsonString()
Definition: json.h:88
JsonNull()
Definition: json.h:293
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:450
Json & operator[](int ind) override
Definition: json.h:199
void Save(JsonWriter *writer) const override
Object ToJson(Parameter const ¶m)
Definition: json.h:598
void Set(size_t i, T v)
Definition: json.h:158
Json & operator=(JsonInteger integer)
Definition: json.h:380
virtual void Save(JsonWriter *writer) const =0
Definition: json_io.h:115
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:301
Data structure representing JSON format.
Definition: json.h:352
static bool IsClassOf(Value const *value)
Definition: json.h:134
bool IsA(Value const *value)
Definition: json.h:70
auto get(U &json) -> decltype(detail::GetImpl(*Cast< T >(&json.GetValue())))&
Get Json value.
Definition: json.h:583
~JsonObject() override=default
std::vector< Json > const & GetArray() const &
Definition: json.h:129
JsonTypedArray(JsonTypedArray &&that) noexcept
Definition: json.h:154
Json()
Definition: json.h:370
std::vector< T > const & GetArray() const &
Definition: json.h:164
std::map< std::string, Json > & GetObject() &
Definition: json.h:204
namespace of xgboost
Definition: base.h:110
Json & operator=(JsonArray &&array)
Definition: json.h:386