4 #ifndef XGBOOST_JSON_H_
5 #define XGBOOST_JSON_H_
8 #include <xgboost/logging.h>
61 #if !defined(__APPLE__)
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);
113 std::vector<Json> vec_;
130 std::vector<Json>
const&
GetArray() && {
return vec_; }
131 std::vector<Json>
const&
GetArray() const & {
return vec_; }
147 template <
typename T, Value::ValueKind kind>
160 void Set(
size_t i, T v) { vec_[i] = v; }
161 size_t Size()
const {
return vec_.size(); }
165 std::vector<T>
const&
GetArray() && {
return vec_; }
166 std::vector<T>
const&
GetArray() const& {
return vec_; }
191 using Map = std::map<std::string, Json, std::less<>>;
227 template <
typename FloatT,
228 typename std::enable_if<std::is_same<FloatT, Float>::value>::type* =
nullptr>
232 template <
typename FloatT,
233 typename std::enable_if<std::is_same<FloatT, double>::value>::type* =
nullptr>
235 number_{static_cast<
Float>(value)} {}
261 template <
typename IntT,
262 typename std::enable_if<std::is_same<IntT, Int>::value>::type* =
nullptr>
264 template <
typename IntT,
265 typename std::enable_if<std::is_same<IntT, size_t>::value>::type* =
nullptr>
267 integer_{static_cast<
Int>(value)} {}
268 template <
typename IntT,
269 typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* =
nullptr>
271 integer_{static_cast<
Int>(value)} {}
272 template <
typename IntT,
273 typename std::enable_if<
274 std::is_same<IntT, uint32_t>::value &&
275 !std::is_same<std::size_t, uint32_t>::value>::type * =
nullptr>
278 integer_{static_cast<
Int>(value)} {}
312 bool boolean_ =
false;
317 template <
typename Bool,
318 typename std::enable_if<
319 std::is_same<Bool, bool>::value ||
320 std::is_same<Bool, bool const>::value>::type* =
nullptr>
369 static void Dump(
Json json, std::string* out, std::ios::openmode mode = std::ios::out);
370 static void Dump(
Json json, std::vector<char>* out, std::ios::openmode mode = std::ios::out);
379 ptr_.reset(
new JsonNumber(std::move(number)));
391 ptr_.reset(
new JsonArray(std::forward<JsonArray>(array)));
395 template <
typename T, Value::ValueKind kind>
398 template <
typename T, Value::ValueKind kind>
406 ptr_.reset(
new JsonObject(std::forward<JsonObject>(
object)));
412 ptr_.reset(
new JsonString(std::forward<JsonString>(str)));
426 ptr_.reset(
new JsonNull(std::move(
null)));
451 return *ptr_ == *(rhs.ptr_);
476 template <
typename T>
484 template <
typename T,
485 typename std::enable_if<
486 std::is_same<T, JsonNumber>::value>::type* =
nullptr>
488 return val.GetNumber();
490 template <
typename T,
491 typename std::enable_if<
492 std::is_same<T, JsonNumber const>::value>::type* =
nullptr>
494 return val.GetNumber();
498 template <
typename T,
499 typename std::enable_if<
500 std::is_same<T, JsonInteger>::value>::type* =
nullptr>
502 return val.GetInteger();
504 template <
typename T,
505 typename std::enable_if<
506 std::is_same<T, JsonInteger const>::value>::type* =
nullptr>
508 return val.GetInteger();
512 template <
typename T,
513 typename std::enable_if<
514 std::is_same<T, JsonString>::value>::type* =
nullptr>
516 return val.GetString();
518 template <
typename T,
519 typename std::enable_if<
520 std::is_same<T, JsonString const>::value>::type* =
nullptr>
522 return val.GetString();
526 template <
typename T,
527 typename std::enable_if<
528 std::is_same<T, JsonBoolean>::value>::type* =
nullptr>
530 return val.GetBoolean();
532 template <
typename T,
533 typename std::enable_if<
534 std::is_same<T, JsonBoolean const>::value>::type* =
nullptr>
536 return val.GetBoolean();
540 template <
typename T,
541 typename std::enable_if<
542 std::is_same<T, JsonArray>::value>::type* =
nullptr>
544 return val.GetArray();
546 template <
typename T,
547 typename std::enable_if<
548 std::is_same<T, JsonArray const>::value>::type* =
nullptr>
550 return val.GetArray();
554 template <
typename T, Value::ValueKind kind>
558 template <
typename T, Value::ValueKind kind>
564 template <typename T, typename std::enable_if<std::is_same<T, JsonObject>::value>::type* =
nullptr>
566 return val.GetObject();
568 template <
typename T,
569 typename std::enable_if<std::is_same<T, JsonObject const>::value>::type* =
nullptr>
571 return val.GetObject();
583 template <
typename T,
typename U>
585 auto& value = *Cast<T>(&json.GetValue());
598 template <
typename Parameter>
601 for (
auto const& kv : param.__DICT__()) {
602 obj[kv.first] = kv.second;
607 template <
typename Parameter>
609 auto const& j_param = get<Object const>(obj);
610 std::map<std::string, std::string> m;
611 for (
auto const& kv : j_param) {
612 m[kv.first] = get<String const>(kv.second);
614 return param->UpdateAllowUnknown(m);
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:119
JsonArray(JsonArray const &that)=delete
Json & operator[](std::string const &key) override
Definition: json.h:128
std::vector< Json > const & GetArray() &&
Definition: json.h:130
std::vector< Json > & GetArray() &
Definition: json.h:132
JsonArray(std::vector< Json > &&arr) noexcept
Definition: json.h:117
JsonArray(JsonArray &&that) noexcept
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonArray()
Definition: json.h:116
Json & operator[](int ind) override
Definition: json.h:126
static bool IsClassOf(Value const *value)
Definition: json.h:136
std::vector< Json > const & GetArray() const &
Definition: json.h:131
Describes both true and false.
Definition: json.h:311
bool const & GetBoolean() const &
Definition: json.h:329
static bool IsClassOf(Value const *value)
Definition: json.h:334
bool const & GetBoolean() &&
Definition: json.h:328
bool operator==(Value const &rhs) const override
void Save(JsonWriter *writer) const override
JsonBoolean()
Definition: json.h:315
JsonBoolean(Bool value)
Definition: json.h:321
JsonBoolean(JsonBoolean &&value) noexcept
Definition: json.h:323
bool & GetBoolean() &
Definition: json.h:330
void Save(JsonWriter *writer) const override
JsonInteger(IntT value)
Definition: json.h:263
int64_t Int
Definition: json.h:254
JsonInteger(JsonInteger &&that) noexcept
Definition: json.h:280
Int const & GetInteger() const &
Definition: json.h:286
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:290
JsonInteger()
Definition: json.h:260
Int & GetInteger() &
Definition: json.h:287
Int const & GetInteger() &&
Definition: json.h:285
JsonNull(std::nullptr_t)
Definition: json.h:298
JsonNull()
Definition: json.h:297
bool operator==(Value const &rhs) const override
JsonNull(JsonNull &&) noexcept
Definition: json.h:299
void Save(JsonWriter *writer) const override
static bool IsClassOf(Value const *value)
Definition: json.h:305
Float const & GetNumber() const &
Definition: json.h:242
Float const & GetNumber() &&
Definition: json.h:241
JsonNumber(JsonNumber const &that)=delete
Float & GetNumber() &
Definition: json.h:243
JsonNumber(JsonNumber &&that) noexcept
Definition: json.h:237
bool operator==(Value const &rhs) const override
float Float
Definition: json.h:220
JsonNumber()
Definition: json.h:226
static bool IsClassOf(Value const *value)
Definition: json.h:247
JsonNumber(FloatT value)
Definition: json.h:229
void Save(JsonWriter *writer) const override
Json & operator[](int ind) override
Definition: json.h:205
Map const & GetObject() &&
Definition: json.h:208
JsonObject(JsonObject &&that) noexcept
void Save(JsonWriter *writer) const override
Map const & GetObject() const &
Definition: json.h:209
Json & operator[](std::string const &key) override
Definition: json.h:206
bool operator==(Value const &rhs) const override
~JsonObject() override=default
std::map< std::string, Json, std::less<> > Map
Definition: json.h:191
Map & GetObject() &
Definition: json.h:210
JsonObject(JsonObject const &that)=delete
static bool IsClassOf(Value const *value)
Definition: json.h:214
JsonObject()
Definition: json.h:197
JsonObject(Map &&object) noexcept
static bool IsClassOf(Value const *value)
Definition: json.h:107
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
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:148
void Set(size_t i, T v)
Definition: json.h:160
std::vector< T > & GetArray() &
Definition: json.h:167
std::vector< T > const & GetArray() &&
Definition: json.h:165
size_t Size() const
Definition: json.h:161
T Type
Definition: json.h:152
JsonTypedArray(size_t n)
Definition: json.h:155
void Save(JsonWriter *writer) const override
bool operator==(Value const &rhs) const override
static bool IsClassOf(Value const *value)
Definition: json.h:169
JsonTypedArray(JsonTypedArray &&that) noexcept
Definition: json.h:156
JsonTypedArray()
Definition: json.h:154
std::vector< T > const & GetArray() const &
Definition: json.h:166
Definition: json_io.h:131
Data structure representing JSON format.
Definition: json.h:356
Json()
Definition: json.h:374
Json & operator=(JsonNumber number)
Definition: json.h:378
Json(JsonNull null)
Definition: json.h:423
Json & operator=(JsonTypedArray< T, kind > &&array)
Definition: json.h:399
Json & operator=(JsonString &&str)
Definition: json.h:411
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:441
Json(JsonArray &&list)
Definition: json.h:389
Json(JsonObject &&object)
Definition: json.h:404
static void Dump(Json json, std::vector< char > *out, std::ios::openmode mode=std::ios::out)
Json(Json &&other) noexcept
Definition: json.h:434
Json(Json const &other)=default
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:446
Json & operator=(Json const &other)=default
Json(JsonString &&str)
Definition: json.h:410
Json(JsonInteger integer)
Definition: json.h:383
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:443
Json & operator=(JsonObject &&object)
Definition: json.h:405
Json & operator=(JsonArray &&array)
Definition: json.h:390
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:454
Json & operator=(JsonInteger integer)
Definition: json.h:384
Json & operator=(JsonBoolean boolean)
Definition: json.h:418
static Json Load(JsonReader *reader)
Pass your own JsonReader.
Json(JsonBoolean boolean)
Definition: json.h:416
IntrusivePtr< Value > const & Ptr() const
Definition: json.h:461
Json(JsonTypedArray< T, kind > &&list)
Definition: json.h:396
Json(JsonNumber number)
Definition: json.h:377
Json & operator=(JsonNull null)
Definition: json.h:425
bool operator==(Json const &rhs) const
Definition: json.h:450
static void Dump(Json json, JsonWriter *writer)
Use your own JsonWriter.
Json & operator=(Json &&other) noexcept
Definition: json.h:435
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:447
Value & GetValue() &
Definition: json.h:448
virtual Value & operator=(Value const &rhs)=delete
virtual bool operator==(Value const &rhs) const =0
Value(ValueKind _kind)
Definition: json.h:50
virtual Json & operator[](std::string const &key)
virtual Json & operator[](int ind)
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:35
friend IntrusivePtrCell & IntrusivePtrRefCount(xgboost::Value const *t) noexcept
Definition: json.h:29
ValueKind Type() const
Definition: json.h:52
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:487
namespace of xgboost
Definition: base.h:110
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:318
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:584
T * Cast(U *value)
Definition: json.h:77
Object ToJson(Parameter const ¶m)
Definition: json.h:599
Args FromJson(Json const &obj, Parameter *param)
Definition: json.h:608
macro for using C++11 enum class as DMLC parameter
Definition: string_view.h:15