4 #ifndef XGBOOST_JSON_H_
5 #define XGBOOST_JSON_H_
8 #include <xgboost/logging.h>
16 #include <type_traits>
62 #if !defined(__APPLE__)
74 return T::IsClassOf(value);
77 template <
typename T,
typename U>
80 return dynamic_cast<T*
>(value);
82 LOG(FATAL) <<
"Invalid cast, from " + value->TypeStr() +
" to " + T().TypeStr();
84 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_; }
148 template <
typename T, Value::ValueKind kind>
161 void Set(
size_t i, T v) { vec_[i] = v; }
162 size_t Size()
const {
return vec_.size(); }
166 std::vector<T>
const&
GetArray() && {
return vec_; }
167 std::vector<T>
const&
GetArray() const& {
return vec_; }
192 using Map = std::map<std::string, Json, std::less<>>;
228 template <
typename FloatT,
229 typename std::enable_if<std::is_same<FloatT, Float>::value>::type* =
nullptr>
233 template <
typename FloatT,
234 typename std::enable_if<std::is_same<FloatT, double>::value>::type* =
nullptr>
236 number_{static_cast<
Float>(value)} {}
262 template <
typename IntT,
263 typename std::enable_if<std::is_same<IntT, Int>::value>::type* =
nullptr>
265 template <
typename IntT,
266 typename std::enable_if<std::is_same<IntT, size_t>::value>::type* =
nullptr>
268 integer_{static_cast<
Int>(value)} {}
269 template <
typename IntT,
270 typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* =
nullptr>
272 integer_{static_cast<
Int>(value)} {}
273 template <
typename IntT,
274 typename std::enable_if<
275 std::is_same<IntT, uint32_t>::value &&
276 !std::is_same<std::size_t, uint32_t>::value>::type * =
nullptr>
279 integer_{static_cast<
Int>(value)} {}
313 bool boolean_ =
false;
318 template <
typename Bool,
319 typename std::enable_if<
320 std::is_same<Bool, bool>::value ||
321 std::is_same<Bool, bool const>::value>::type* =
nullptr>
370 static void Dump(
Json json, std::string* out, std::ios::openmode mode = std::ios::out);
371 static void Dump(
Json json, std::vector<char>* out, std::ios::openmode mode = std::ios::out);
380 ptr_.reset(
new JsonNumber(std::move(number)));
392 ptr_.reset(
new JsonArray(std::forward<JsonArray>(array)));
396 template <
typename T, Value::ValueKind kind>
399 template <
typename T, Value::ValueKind kind>
407 ptr_.reset(
new JsonObject(std::forward<JsonObject>(
object)));
413 ptr_.reset(
new JsonString(std::forward<JsonString>(str)));
427 ptr_.reset(
new JsonNull(std::move(
null)));
452 return *ptr_ == *(rhs.ptr_);
477 template <
typename T>
485 template <
typename T,
486 typename std::enable_if<
487 std::is_same<T, JsonNumber>::value>::type* =
nullptr>
489 return val.GetNumber();
491 template <
typename T,
492 typename std::enable_if<
493 std::is_same<T, JsonNumber const>::value>::type* =
nullptr>
495 return val.GetNumber();
499 template <
typename T,
500 typename std::enable_if<
501 std::is_same<T, JsonInteger>::value>::type* =
nullptr>
503 return val.GetInteger();
505 template <
typename T,
506 typename std::enable_if<
507 std::is_same<T, JsonInteger const>::value>::type* =
nullptr>
509 return val.GetInteger();
513 template <
typename T,
514 typename std::enable_if<
515 std::is_same<T, JsonString>::value>::type* =
nullptr>
517 return val.GetString();
519 template <
typename T,
520 typename std::enable_if<
521 std::is_same<T, JsonString const>::value>::type* =
nullptr>
523 return val.GetString();
527 template <
typename T,
528 typename std::enable_if<
529 std::is_same<T, JsonBoolean>::value>::type* =
nullptr>
531 return val.GetBoolean();
533 template <
typename T,
534 typename std::enable_if<
535 std::is_same<T, JsonBoolean const>::value>::type* =
nullptr>
537 return val.GetBoolean();
541 template <
typename T,
542 typename std::enable_if<
543 std::is_same<T, JsonArray>::value>::type* =
nullptr>
545 return val.GetArray();
547 template <
typename T,
548 typename std::enable_if<
549 std::is_same<T, JsonArray const>::value>::type* =
nullptr>
551 return val.GetArray();
555 template <
typename T, Value::ValueKind kind>
559 template <
typename T, Value::ValueKind kind>
565 template <typename T, typename std::enable_if<std::is_same<T, JsonObject>::value>::type* =
nullptr>
567 return val.GetObject();
569 template <
typename T,
570 typename std::enable_if<std::is_same<T, JsonObject const>::value>::type* =
nullptr>
572 return val.GetObject();
584 template <
typename T,
typename U>
586 auto& value = *Cast<T>(&json.GetValue());
600 template <
typename Head>
602 return IsA<Head>(value);
605 template <
typename Head,
typename... JT>
610 template <
typename Head>
612 return "`" + Head{}.TypeStr() +
"`";
615 template <
typename Head,
typename... JT>
627 template <
typename... JT>
629 if (!detail::TypeCheckImpl<JT...>(value)) {
630 LOG(FATAL) <<
"Invalid type for: `" << name <<
"`, expecting one of the: {`"
645 template <
typename Parameter>
648 for (
auto const& kv : param.__DICT__()) {
649 obj[kv.first] = kv.second;
664 template <
typename Parameter>
666 auto const& j_param = get<Object const>(obj);
668 for (
auto const& kv : j_param) {
669 args.emplace_back(kv.first, get<String const>(kv.second));
671 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
static bool IsClassOf(Value const *value)
Definition: json.h:137
std::vector< Json > const & GetArray() const &
Definition: json.h:132
Describes both true and false.
Definition: json.h:312
bool const & GetBoolean() const &
Definition: json.h:330
static bool IsClassOf(Value const *value)
Definition: json.h:335
bool const & GetBoolean() &&
Definition: json.h:329
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonBoolean()
Definition: json.h:316
JsonBoolean(Bool value)
Definition: json.h:322
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:324
bool & GetBoolean() &
Definition: json.h:331
void Save(JsonWriter *writer) const override
JsonInteger(IntT value)
Definition: json.h:264
int64_t Int
Definition: json.h:255
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:281
Int const & GetInteger() const &
Definition: json.h:287
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:291
JsonInteger()
Definition: json.h:261
Int & GetInteger() &
Definition: json.h:288
Int const & GetInteger() &&
Definition: json.h:286
JsonNull(std::nullptr_t)
Definition: json.h:299
JsonNull()
Definition: json.h:298
bool operator==(Value const &rhs) const override
JsonNull(JsonNull &&) noexcept
Definition: json.h:300
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:306
Float const & GetNumber() const &
Definition: json.h:243
Float const & GetNumber() &&
Definition: json.h:242
JsonNumber(JsonNumber const &that)=delete
Float & GetNumber() &
Definition: json.h:244
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:238
bool operator==(Value const &rhs) const override
float Float
Definition: json.h:221
JsonNumber()
Definition: json.h:227
static bool IsClassOf(Value const *value)
Definition: json.h:248
JsonNumber(FloatT value)
Definition: json.h:230
void Save(JsonWriter *writer) const override
Json & operator[](int ind) override
Definition: json.h:206
Map const & GetObject() &&
Definition: json.h:209
JsonObject(JsonObject &&that) noexcept
void Save(JsonWriter *writer) const override
Map const & GetObject() const &
Definition: json.h:210
Json & operator[](std::string const &key) override
Definition: json.h:207
bool operator==(Value const &rhs) const override
~JsonObject() override=default
std::map< std::string, Json, std::less<> > Map
Definition: json.h:192
Map & GetObject() &
Definition: json.h:211
JsonObject(JsonObject const &that)=delete
static bool IsClassOf(Value const *value)
Definition: json.h:215
JsonObject()
Definition: json.h:198
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:94
void Save(JsonWriter *writer) const override
std::string const & GetString() &&
Definition: json.h:102
std::string & GetString() &
Definition: json.h:104
bool operator==(Value const &rhs) const override
JsonString(std::string const &str)
Definition: json.h:92
JsonString(JsonString &&str) noexcept
Definition: json.h:96
std::string const & GetString() const &
Definition: json.h:103
JsonString()
Definition: json.h:91
Typed array for Universal Binary JSON.
Definition: json.h:149
void Set(size_t i, T v)
Definition: json.h:161
std::vector< T > & GetArray() &
Definition: json.h:168
std::vector< T > const & GetArray() &&
Definition: json.h:166
size_t Size() const
Definition: json.h:162
T Type
Definition: json.h:153
JsonTypedArray(size_t n)
Definition: json.h:156
void Save(JsonWriter *writer) const override
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:170
JsonTypedArray(JsonTypedArray &&that) noexcept
Definition: json.h:157
JsonTypedArray()
Definition: json.h:155
std::vector< T > const & GetArray() const &
Definition: json.h:167
Definition: json_io.h:113
Data structure representing JSON format.
Definition: json.h:357
Json & operator=(JsonNumber number)
Definition: json.h:379
Json(JsonNull null)
Definition: json.h:424
Json & operator=(JsonTypedArray< T, kind > &&array)
Definition: json.h:400
Json & operator=(JsonString &&str)
Definition: json.h:412
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:442
Json(JsonArray &&list)
Definition: json.h:390
Json(JsonObject &&object)
Definition: json.h:405
static void Dump(Json json, std::vector< char > *out, std::ios::openmode mode=std::ios::out)
Json(Json &&other) noexcept
Definition: json.h:435
Json(Json const &other)=default
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:447
Json & operator=(Json const &other)=default
Json(JsonString &&str)
Definition: json.h:411
Json(JsonInteger integer)
Definition: json.h:384
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:444
Json & operator=(JsonObject &&object)
Definition: json.h:406
Json & operator=(JsonArray &&array)
Definition: json.h:391
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:455
Json & operator=(JsonInteger integer)
Definition: json.h:385
Json & operator=(JsonBoolean boolean)
Definition: json.h:419
static Json Load(JsonReader *reader)
Pass your own JsonReader.
Json(JsonBoolean boolean)
Definition: json.h:417
IntrusivePtr< Value > const & Ptr() const
Definition: json.h:462
Json(JsonTypedArray< T, kind > &&list)
Definition: json.h:397
Json(JsonNumber number)
Definition: json.h:378
Json & operator=(JsonNull null)
Definition: json.h:426
bool operator==(Json const &rhs) const
Definition: json.h:451
static void Dump(Json json, JsonWriter *writer)
Use your own JsonWriter.
Json & operator=(Json &&other) noexcept
Definition: json.h:436
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:448
Value & GetValue() &
Definition: json.h:449
virtual Value & operator=(Value const &rhs)=delete
virtual bool operator==(Value const &rhs) const =0
Value(ValueKind _kind)
Definition: json.h:51
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:53
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
std::string TypeCheckError()
Definition: json.h:611
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:488
bool TypeCheckImpl(Json const &value)
Definition: json.h:601
namespace of xgboost
Definition: base.h:90
void TypeCheck(Json const &value, StringView name)
Type check for JSON-based parameters.
Definition: json.h:628
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
bool IsA(Value const *value)
Definition: json.h:73
auto get(U &json) -> decltype(detail::GetImpl(*Cast< T >(&json.GetValue())))&
Get Json value.
Definition: json.h:585
T * Cast(U *value)
Definition: json.h:78
Object ToJson(Parameter const ¶m)
Convert XGBoost parameter to JSON object.
Definition: json.h:646
Args FromJson(Json const &obj, Parameter *param)
Load a XGBoost parameter from a JSON object.
Definition: json.h:665
macro for using C++11 enum class as DMLC parameter
Definition: string_view.h:15