Compare commits
1 Commits
master
...
mutable-v4
Author | SHA1 | Date |
---|---|---|
Vitaliy Filippov | 2273bdd966 |
36
json11.cpp
36
json11.cpp
|
@ -138,6 +138,7 @@ void Json::dump(string &out) const {
|
|||
template <Json::Type tag, typename T>
|
||||
class Value : public JsonValue {
|
||||
protected:
|
||||
friend class JsonIndex;
|
||||
|
||||
// Constructors
|
||||
explicit Value(const T &value) : m_value(value) {}
|
||||
|
@ -156,7 +157,7 @@ protected:
|
|||
return m_value < static_cast<const Value<tag, T> *>(other)->m_value;
|
||||
}
|
||||
|
||||
const T m_value;
|
||||
T m_value;
|
||||
void dump(string &out) const override { json11::dump(m_value, out); }
|
||||
};
|
||||
|
||||
|
@ -320,14 +321,14 @@ Json::Json(Json::object &&values) : m_ptr(make_shared<JsonObject>(move(valu
|
|||
Json::Type Json::type() const { return m_ptr->type(); }
|
||||
double Json::number_value() const { return m_ptr->number_value(); }
|
||||
int64_ Json::int64_value() const { return m_ptr->int64_value(); }
|
||||
uint64_ Json::uint64_value() const { return m_ptr->uint64_value(); }
|
||||
uint64_ Json::uint64_value() const { return m_ptr->uint64_value(); }
|
||||
bool Json::bool_value() const { return m_ptr->bool_value(); }
|
||||
string Json::as_string() const { return m_ptr->as_string(); }
|
||||
const string & Json::string_value() const { return m_ptr->string_value(); }
|
||||
const vector<Json> & Json::array_items() const { return m_ptr->array_items(); }
|
||||
const map<string, Json> & Json::object_items() const { return m_ptr->object_items(); }
|
||||
const Json & Json::operator[] (size_t i) const { return (*m_ptr)[i]; }
|
||||
const Json & Json::operator[] (const string &key) const { return (*m_ptr)[key]; }
|
||||
JsonIndex Json::operator[] (const string &key) { return (JsonIndex){ .m_object = *this, .m_key = key }; }
|
||||
|
||||
double JsonValue::number_value() const { return 0; }
|
||||
int64_ JsonValue::int64_value() const { return 0; }
|
||||
|
@ -769,8 +770,6 @@ struct JsonParser final {
|
|||
return fail("expected ',' in object, got " + esc(ch));
|
||||
|
||||
ch = get_next_token();
|
||||
if (ch == '}')
|
||||
break;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
@ -794,8 +793,6 @@ struct JsonParser final {
|
|||
return fail("expected ',' in list, got " + esc(ch));
|
||||
|
||||
ch = get_next_token();
|
||||
if (ch == ']')
|
||||
break;
|
||||
(void)ch;
|
||||
}
|
||||
return data;
|
||||
|
@ -846,8 +843,13 @@ bool Json::has_shape(const shape & types, string & err) const {
|
|||
return false;
|
||||
}
|
||||
|
||||
const auto & obj = object_items();
|
||||
for (auto & item : types) {
|
||||
if ((*this)[item.first].type() != item.second) {
|
||||
auto it = obj.find(item.first);
|
||||
if (it == obj.end()) {
|
||||
return false;
|
||||
}
|
||||
if (it->second.type() != item.second) {
|
||||
err = "bad type for " + item.first + " in " + dump();
|
||||
return false;
|
||||
}
|
||||
|
@ -856,4 +858,22 @@ bool Json::has_shape(const shape & types, string & err) const {
|
|||
return true;
|
||||
}
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * *
|
||||
* Indexed wrapper to prevent autovivification
|
||||
*/
|
||||
|
||||
const Json & JsonIndex::self() const {
|
||||
if (m_object.is_object()) {
|
||||
return ((JsonObject*)m_object.m_ptr.get())->m_value[m_key];
|
||||
}
|
||||
return static_null();
|
||||
}
|
||||
|
||||
Json JsonIndex::operator =(Json value) {
|
||||
if (m_object.is_object()) {
|
||||
return ((JsonObject*)m_object.m_ptr.get())->m_value[m_key] = value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace json11
|
||||
|
|
33
json11.hpp
33
json11.hpp
|
@ -77,6 +77,8 @@ enum JsonParse {
|
|||
|
||||
class JsonValue;
|
||||
|
||||
class JsonIndex;
|
||||
|
||||
#if __cplusplus < 201103L
|
||||
#error This project can only be compiled with a compiler that supports C++11
|
||||
#else
|
||||
|
@ -165,7 +167,7 @@ public:
|
|||
// Return a reference to arr[i] if this is an array, Json() otherwise.
|
||||
const Json & operator[](size_t i) const;
|
||||
// Return a reference to obj[key] if this is an object, Json() otherwise.
|
||||
const Json & operator[](const std::string &key) const;
|
||||
JsonIndex operator[](const std::string &key);
|
||||
|
||||
// Serialize.
|
||||
void dump(std::string &out) const;
|
||||
|
@ -220,9 +222,38 @@ public:
|
|||
bool has_shape(const shape & types, std::string & err) const;
|
||||
|
||||
private:
|
||||
friend class JsonIndex;
|
||||
std::shared_ptr<JsonValue> m_ptr;
|
||||
};
|
||||
|
||||
class JsonIndex {
|
||||
public:
|
||||
Json m_object;
|
||||
const std::string & m_key;
|
||||
|
||||
Json operator =(Json value);
|
||||
operator const Json &() { return m_object[m_key]; }
|
||||
const Json & self() const;
|
||||
|
||||
bool is_null() const { return self().is_null(); }
|
||||
bool is_number() const { return self().is_number(); }
|
||||
bool is_bool() const { return self().is_bool(); }
|
||||
bool is_string() const { return self().is_string(); }
|
||||
bool is_array() const { return self().is_array(); }
|
||||
bool is_object() const { return self().is_object(); }
|
||||
|
||||
double number_value() const { return self().number_value(); }
|
||||
int64_ int64_value() const { return self().int64_value(); }
|
||||
uint64_ uint64_value() const { return self().uint64_value(); }
|
||||
std::string as_string() const { return self().as_string(); }
|
||||
bool bool_value() const { return self().bool_value(); }
|
||||
const std::string &string_value() const { return self().string_value(); }
|
||||
const Json::array &array_items() const { return self().array_items(); }
|
||||
const Json::object &object_items() const { return self().object_items(); }
|
||||
|
||||
std::string dump() const { return self().dump(); }
|
||||
};
|
||||
|
||||
// Internal class hierarchy - JsonValue objects are not exposed to users of this API.
|
||||
class JsonValue {
|
||||
protected:
|
||||
|
|
Loading…
Reference in New Issue