4 #ifndef XGBOOST_JSON_H_ 5 #define XGBOOST_JSON_H_ 7 #include <xgboost/logging.h> 39 virtual ~Value() =
default;
57 return T::IsClassOf(value);
60 template <
typename T,
typename U>
63 return dynamic_cast<T*
>(value);
65 LOG(FATAL) <<
"Invalid cast, from " + value->TypeStr() +
" to " + T().TypeStr();
67 return dynamic_cast<T*
>(value);
85 std::string
const&
GetString() const & {
return str_; }
97 std::vector<Json> vec_;
113 std::vector<Json>
const&
GetArray() && {
return vec_; }
114 std::vector<Json>
const&
GetArray() const & {
return vec_; }
126 std::map<std::string, Json> object_;
130 JsonObject(std::map<std::string, Json>&&
object);
139 std::map<std::string, Json>
const&
GetObject() && {
return object_; }
140 std::map<std::string, Json>
const&
GetObject() const & {
return object_; }
141 std::map<std::string, Json> &
GetObject() & {
return object_; }
161 template <
typename FloatT,
162 typename std::enable_if<std::is_same<FloatT, Float>::value>::type* =
nullptr>
166 template <
typename FloatT,
167 typename std::enable_if<std::is_same<FloatT, double>::value>::type* =
nullptr>
169 number_{
static_cast<Float>(value)} {}
198 template <
typename IntT,
199 typename std::enable_if<std::is_same<IntT, Int>::value>::type* =
nullptr>
201 template <
typename IntT,
202 typename std::enable_if<std::is_same<IntT, size_t>::value>::type* =
nullptr>
204 integer_{
static_cast<Int>(value)} {}
205 template <
typename IntT,
206 typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* =
nullptr>
208 integer_{
static_cast<Int>(value)} {}
209 template <
typename IntT,
210 typename std::enable_if<
211 std::is_same<IntT, uint32_t>::value &&
212 !std::is_same<std::size_t, uint32_t>::value>::type * =
nullptr>
215 integer_{
static_cast<Int>(value)} {}
258 template <
typename Bool,
259 typename std::enable_if<
260 std::is_same<Bool, bool>::value ||
261 std::is_same<Bool, bool const>::value>::type* =
nullptr>
290 StringView(CharT
const* str,
size_t size) : str_{str}, size_{size} {}
293 CharT
const&
at(
size_t p)
const {
297 size_t size()
const {
return size_; }
302 std::string
substr(
size_t beg,
size_t n)
const {
303 CHECK_LE(beg, size_);
304 return std::string {str_ + beg, n < (size_ - beg) ? n : (size_ - beg)};
306 char const*
c_str()
const {
return str_; }
334 static void Dump(
Json json, std::string* out);
341 ptr_.reset(
new JsonNumber(std::move(number)));
356 ptr_.reset(
new JsonArray(std::move(array)));
364 ptr_.reset(
new JsonObject(std::move(
object)));
383 ptr_{
new JsonNull(std::move(null))} {}
385 ptr_.reset(
new JsonNull(std::move(null)));
393 Json(
Json&& other) : ptr_{std::move(other.ptr_)} {}
395 ptr_ = std::move(other.ptr_);
410 return *ptr_ == *(rhs.ptr_);
421 std::shared_ptr<Value> ptr_;
424 template <
typename T>
433 template <
typename T,
434 typename std::enable_if<
435 std::is_same<T, JsonNumber>::value>::type* =
nullptr>
437 return val.GetNumber();
439 template <
typename T,
440 typename std::enable_if<
441 std::is_same<T, JsonNumber const>::value>::type* =
nullptr>
443 return val.GetNumber();
447 template <
typename T,
448 typename std::enable_if<
449 std::is_same<T, JsonInteger>::value>::type* =
nullptr>
451 return val.GetInteger();
453 template <
typename T,
454 typename std::enable_if<
455 std::is_same<T, JsonInteger const>::value>::type* =
nullptr>
457 return val.GetInteger();
461 template <
typename T,
462 typename std::enable_if<
463 std::is_same<T, JsonString>::value>::type* =
nullptr>
465 return val.GetString();
467 template <
typename T,
468 typename std::enable_if<
469 std::is_same<T, JsonString const>::value>::type* =
nullptr>
471 return val.GetString();
475 template <
typename T,
476 typename std::enable_if<
477 std::is_same<T, JsonBoolean>::value>::type* =
nullptr>
479 return val.GetBoolean();
481 template <
typename T,
482 typename std::enable_if<
483 std::is_same<T, JsonBoolean const>::value>::type* =
nullptr>
485 return val.GetBoolean();
489 template <
typename T,
490 typename std::enable_if<
491 std::is_same<T, JsonArray>::value>::type* =
nullptr>
493 return val.GetArray();
495 template <
typename T,
496 typename std::enable_if<
497 std::is_same<T, JsonArray const>::value>::type* =
nullptr>
499 return val.GetArray();
503 template <
typename T,
504 typename std::enable_if<
505 std::is_same<T, JsonObject>::value>::type* =
nullptr>
506 std::map<std::string, Json>&
GetImpl(T& val) {
507 return val.GetObject();
509 template <
typename T,
510 typename std::enable_if<
511 std::is_same<T, JsonObject const>::value>::type* =
nullptr>
512 std::map<std::string, Json>
const&
GetImpl(T& val) {
513 return val.GetObject();
526 template <
typename T,
typename U>
528 auto& value = *Cast<T>(&json.GetValue());
542 template <
typename Parameter>
545 for (
auto const& kv : param.__DICT__()) {
546 obj[kv.first] = kv.second;
551 template <
typename Parameter>
553 auto const& j_param = get<Object const>(obj);
554 std::map<std::string, std::string> m;
555 for (
auto const& kv : j_param) {
556 m[kv.first] = get<String const>(kv.second);
558 param->UpdateAllowUnknown(m);
561 #endif // XGBOOST_JSON_H_ JsonInteger(IntT value)
Definition: json.h:200
Int const & GetInteger() const &
Definition: json.h:224
void FromJson(Json const &obj, Parameter *param)
Definition: json.h:552
virtual void Save(JsonWriter *writer)=0
Int const & GetInteger() &&
Definition: json.h:223
bool const & GetBoolean() &&
Definition: json.h:270
int64_t Int
Definition: json.h:191
bool IsA(Value const *value)
Definition: json.h:56
Value const & GetValue() const &
Return the reference to stored Json value.
Definition: json.h:405
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:436
Value(ValueKind _kind)
Definition: json.h:36
JsonString(std::string const &str)
Definition: json.h:74
JsonBoolean()
Definition: json.h:256
Json & operator=(JsonInteger integer)
Definition: json.h:347
StringView(CharT const *str, size_t size)
Definition: json.h:290
Float & GetNumber() &
Definition: json.h:178
CharT const & at(size_t p) const
Definition: json.h:293
friend std::ostream & operator<<(std::ostream &os, Json const &j)
Definition: json.h:413
CharT const & operator[](size_t p) const
Definition: json.h:292
JsonNull(std::nullptr_t)
Definition: json.h:236
JsonArray(std::vector< Json > const &arr)
Definition: json.h:103
Describes both true and false.
Definition: json.h:252
Int & GetInteger() &
Definition: json.h:225
std::map< std::string, Json > & GetObject() &
Definition: json.h:141
std::string substr(size_t beg, size_t n) const
Definition: json.h:302
ValueKind Type() const
Definition: json.h:38
Json & operator=(JsonArray array)
Definition: json.h:355
static bool IsClassOf(Value const *value)
Definition: json.h:246
JsonArray()
Definition: json.h:100
Json & operator[](int ind) const
Index Json object with int, used for Json Array.
Definition: json.h:402
static bool IsClassOf(Value const *value)
Definition: json.h:146
JsonBoolean(Bool value)
Definition: json.h:262
static bool IsClassOf(Value const *value)
Definition: json.h:277
JsonNull()
Definition: json.h:235
std::map< std::string, Json > const & GetImpl(T &val)
Definition: json.h:512
T * Cast(U *value)
Definition: json.h:61
std::string const & GetString() &&
Definition: json.h:84
Json(JsonInteger integer)
Definition: json.h:346
static void Dump(Json json, std::string *out)
JsonInteger()
Definition: json.h:197
Json & operator=(JsonString str)
Definition: json.h:370
Value const & GetValue() &&
Definition: json.h:406
Json & operator=(JsonNumber number)
Definition: json.h:340
size_t size() const
Definition: json.h:297
float Float
Definition: json.h:154
static bool IsClassOf(Value const *value)
Definition: json.h:91
Json & operator=(JsonNull null)
Definition: json.h:384
std::map< std::string, Json > const & GetObject() &&
Definition: json.h:139
Json(Json &&other)
Definition: json.h:393
virtual bool operator==(Value const &rhs) const =0
JsonString(std::string &&str)
Definition: json.h:76
static bool IsClassOf(Value const *value)
Definition: json.h:120
ValueKind
Simplified implementation of LLVM RTTI.
Definition: json.h:26
bool & GetBoolean() &
Definition: json.h:272
Json(JsonString str)
Definition: json.h:368
Json()
Definition: json.h:336
namespace of xgboost
Definition: base.h:102
Json(JsonArray list)
Definition: json.h:353
std::vector< Json > const & GetArray() &&
Definition: json.h:113
std::string & GetString() &
Definition: json.h:86
Float const & GetNumber() &&
Definition: json.h:176
JsonArray(std::vector< Json > &&arr)
Definition: json.h:101
std::map< std::string, Json > const & GetObject() const &
Definition: json.h:140
Json(JsonObject object)
Definition: json.h:361
Value & GetValue() &
Definition: json.h:407
std::vector< Json > const & GetArray() const &
Definition: json.h:114
Json & operator=(Json &&other)
Definition: json.h:394
JsonObject()
Definition: json.h:129
static bool IsClassOf(Value const *value)
Definition: json.h:184
bool operator==(Json const &rhs) const
Definition: json.h:409
Definition: json_io.h:115
std::string TypeStr() const
virtual Json & operator[](std::string const &key)=0
Data structure representing JSON format.
Definition: json.h:326
Json & operator[](std::string const &key) const
Index Json object with a std::string, used for Json Object.
Definition: json.h:400
char const * c_str() const
Definition: json.h:306
Json & operator=(JsonBoolean boolean)
Definition: json.h:377
JsonNumber(FloatT value)
Definition: json.h:163
JsonNumber()
Definition: json.h:160
Json(JsonNull null)
Definition: json.h:382
Json & operator=(JsonObject object)
Definition: json.h:363
Float const & GetNumber() const &
Definition: json.h:177
bool const & GetBoolean() const &
Definition: json.h:271
Json(JsonNumber number)
Definition: json.h:339
Object ToJson(Parameter const ¶m)
Definition: json.h:543
static bool IsClassOf(Value const *value)
Definition: json.h:228
std::string const & GetString() const &
Definition: json.h:85
macro for using C++11 enum class as DMLC parameter
JsonString()
Definition: json.h:73
Json(JsonBoolean boolean)
Definition: json.h:375
std::vector< Json > & GetArray() &
Definition: json.h:115
virtual Value & operator=(Value const &rhs)=0