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_; }
335 static void Dump(
Json json, std::ostream* stream,
336 bool pretty = ConsoleLogger::ShouldLog(
337 ConsoleLogger::LogVerbosity::kDebug));
338 static void Dump(
Json json, std::string* out,
339 bool pretty = ConsoleLogger::ShouldLog(
340 ConsoleLogger::LogVerbosity::kDebug));
347 ptr_.reset(
new JsonNumber(std::move(number)));
362 ptr_.reset(
new JsonArray(std::move(array)));
370 ptr_.reset(
new JsonObject(std::move(
object)));
389 ptr_{
new JsonNull(std::move(null))} {}
391 ptr_.reset(
new JsonNull(std::move(null)));
399 Json(
Json&& other) : ptr_{std::move(other.ptr_)} {}
401 ptr_ = std::move(other.ptr_);
416 return *ptr_ == *(rhs.ptr_);
427 std::shared_ptr<Value> ptr_;
430 template <
typename T>
439 template <
typename T,
440 typename std::enable_if<
441 std::is_same<T, JsonNumber>::value>::type* =
nullptr>
443 return val.GetNumber();
445 template <
typename T,
446 typename std::enable_if<
447 std::is_same<T, JsonNumber const>::value>::type* =
nullptr>
449 return val.GetNumber();
453 template <
typename T,
454 typename std::enable_if<
455 std::is_same<T, JsonInteger>::value>::type* =
nullptr>
457 return val.GetInteger();
459 template <
typename T,
460 typename std::enable_if<
461 std::is_same<T, JsonInteger const>::value>::type* =
nullptr>
463 return val.GetInteger();
467 template <
typename T,
468 typename std::enable_if<
469 std::is_same<T, JsonString>::value>::type* =
nullptr>
471 return val.GetString();
473 template <
typename T,
474 typename std::enable_if<
475 std::is_same<T, JsonString const>::value>::type* =
nullptr>
477 return val.GetString();
481 template <
typename T,
482 typename std::enable_if<
483 std::is_same<T, JsonBoolean>::value>::type* =
nullptr>
485 return val.GetBoolean();
487 template <
typename T,
488 typename std::enable_if<
489 std::is_same<T, JsonBoolean const>::value>::type* =
nullptr>
491 return val.GetBoolean();
495 template <
typename T,
496 typename std::enable_if<
497 std::is_same<T, JsonArray>::value>::type* =
nullptr>
499 return val.GetArray();
501 template <
typename T,
502 typename std::enable_if<
503 std::is_same<T, JsonArray const>::value>::type* =
nullptr>
505 return val.GetArray();
509 template <
typename T,
510 typename std::enable_if<
511 std::is_same<T, JsonObject>::value>::type* =
nullptr>
512 std::map<std::string, Json>&
GetImpl(T& val) {
513 return val.GetObject();
515 template <
typename T,
516 typename std::enable_if<
517 std::is_same<T, JsonObject const>::value>::type* =
nullptr>
518 std::map<std::string, Json>
const&
GetImpl(T& val) {
519 return val.GetObject();
532 template <
typename T,
typename U>
534 auto& value = *Cast<T>(&json.GetValue());
548 template <
typename Parameter>
551 for (
auto const& kv : param.__DICT__()) {
552 obj[kv.first] = kv.second;
557 template <
typename Parameter>
559 auto const& j_param = get<Object const>(obj);
560 std::map<std::string, std::string> m;
561 for (
auto const& kv : j_param) {
562 m[kv.first] = get<String const>(kv.second);
564 param->UpdateAllowUnknown(m);
567 #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:558
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:411
JsonNumber::Float & GetImpl(T &val)
Definition: json.h:442
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:353
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:419
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:361
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:408
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:518
T * Cast(U *value)
Definition: json.h:61
std::string const & GetString() &&
Definition: json.h:84
Json(JsonInteger integer)
Definition: json.h:352
JsonInteger()
Definition: json.h:197
Json & operator=(JsonString str)
Definition: json.h:376
Value const & GetValue() &&
Definition: json.h:412
Json & operator=(JsonNumber number)
Definition: json.h:346
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:390
std::map< std::string, Json > const & GetObject() &&
Definition: json.h:139
Json(Json &&other)
Definition: json.h:399
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:374
Json()
Definition: json.h:342
namespace of xgboost
Definition: base.h:102
Json(JsonArray list)
Definition: json.h:359
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:367
Value & GetValue() &
Definition: json.h:413
std::vector< Json > const & GetArray() const &
Definition: json.h:114
Json & operator=(Json &&other)
Definition: json.h:400
JsonObject()
Definition: json.h:129
static bool IsClassOf(Value const *value)
Definition: json.h:184
bool operator==(Json const &rhs) const
Definition: json.h:415
Definition: json_io.h:120
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:406
char const * c_str() const
Definition: json.h:306
Json & operator=(JsonBoolean boolean)
Definition: json.h:383
JsonNumber(FloatT value)
Definition: json.h:163
static void Dump(Json json, std::ostream *stream, bool pretty=ConsoleLogger::ShouldLog(ConsoleLogger::LogVerbosity::kDebug))
Dump json into stream.
JsonNumber()
Definition: json.h:160
Json(JsonNull null)
Definition: json.h:388
Json & operator=(JsonObject object)
Definition: json.h:369
Float const & GetNumber() const &
Definition: json.h:177
bool const & GetBoolean() const &
Definition: json.h:271
Json(JsonNumber number)
Definition: json.h:345
Object ToJson(Parameter const ¶m)
Definition: json.h:549
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:381
std::vector< Json > & GetArray() &
Definition: json.h:115
virtual Value & operator=(Value const &rhs)=0