Convenient methods for JSON integer. (#5089)
* Fix parsing empty object.
This commit is contained in:
parent
dcde433402
commit
f0ca53d9ec
@ -202,6 +202,17 @@ class JsonInteger : public Value {
|
||||
typename std::enable_if<std::is_same<IntT, size_t>::value>::type* = nullptr>
|
||||
JsonInteger(IntT value) : Value(ValueKind::Integer), // NOLINT
|
||||
integer_{static_cast<Int>(value)} {}
|
||||
template <typename IntT,
|
||||
typename std::enable_if<std::is_same<IntT, int32_t>::value>::type* = nullptr>
|
||||
JsonInteger(IntT value) : Value(ValueKind::Integer), // NOLINT
|
||||
integer_{static_cast<Int>(value)} {}
|
||||
template <typename IntT,
|
||||
typename std::enable_if<
|
||||
std::is_same<IntT, uint32_t>::value &&
|
||||
!std::is_same<std::size_t, uint32_t>::value>::type * = nullptr>
|
||||
JsonInteger(IntT value) // NOLINT
|
||||
: Value(ValueKind::Integer),
|
||||
integer_{static_cast<Int>(value)} {}
|
||||
|
||||
Json& operator[](std::string const & key) override;
|
||||
Json& operator[](int ind) override;
|
||||
@ -533,8 +544,8 @@ using Null = JsonNull;
|
||||
|
||||
// Utils tailored for XGBoost.
|
||||
|
||||
template <typename Type>
|
||||
Object toJson(XGBoostParameter<Type> const& param) {
|
||||
template <typename Parameter>
|
||||
Object toJson(Parameter const& param) {
|
||||
Object obj;
|
||||
for (auto const& kv : param.__DICT__()) {
|
||||
obj[kv.first] = kv.second;
|
||||
@ -542,8 +553,8 @@ Object toJson(XGBoostParameter<Type> const& param) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void fromJson(Json const& obj, XGBoostParameter<Type>* param) {
|
||||
template <typename Parameter>
|
||||
void fromJson(Json const& obj, Parameter* param) {
|
||||
auto const& j_param = get<Object const>(obj);
|
||||
std::map<std::string, std::string> m;
|
||||
for (auto const& kv : j_param) {
|
||||
|
||||
@ -243,7 +243,7 @@ Json& JsonNumber::operator[](int ind) {
|
||||
|
||||
bool JsonNumber::operator==(Value const& rhs) const {
|
||||
if (!IsA<JsonNumber>(&rhs)) { return false; }
|
||||
return number_ == Cast<JsonNumber const>(&rhs)->getNumber();
|
||||
return std::abs(number_ - Cast<JsonNumber const>(&rhs)->getNumber()) < kRtEps;
|
||||
}
|
||||
|
||||
Value & JsonNumber::operator=(Value const &rhs) {
|
||||
@ -504,7 +504,10 @@ Json JsonReader::ParseObject() {
|
||||
SkipSpaces();
|
||||
char ch = PeekNextChar();
|
||||
|
||||
if (ch == '}') return Json(std::move(data));
|
||||
if (ch == '}') {
|
||||
GetChar('}');
|
||||
return Json(std::move(data));
|
||||
}
|
||||
|
||||
while (true) {
|
||||
SkipSpaces();
|
||||
|
||||
@ -226,6 +226,10 @@ TEST(Json, EmptyObject) {
|
||||
std::stringstream iss(str);
|
||||
auto json = Json::Load(StringView{str.c_str(), str.size()});
|
||||
ASSERT_TRUE(IsA<Object>(json["statistic"]));
|
||||
|
||||
str = R"json({"Config": {},"Model": {}})json"; // NOLINT
|
||||
json = Json::Load(StringView{str.c_str(), str.size()});
|
||||
ASSERT_TRUE(IsA<Object>(json["Model"]));
|
||||
}
|
||||
|
||||
TEST(Json, EmptyArray) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user