Compare commits

..

1 Commits

Author SHA1 Message Date
Vitaliy Filippov 3e0040d737 Mutable v2 2022-03-13 12:18:34 +03:00
2 changed files with 57 additions and 29 deletions

View File

@ -131,6 +131,27 @@ void Json::dump(string &out) const {
m_ptr->dump(out);
}
/* * * * * * * * * * * * * * * * * * * *
* Indexed wrapper to prevent autovivification
*/
JsonIndex::JsonIndex(Json obj, const std::string & key)
{
m_object = obj;
m_key = key;
}
Json & JsonIndex::operator =(const Json & value)
{
auto & obj = m_object.object_items();
return obj[m_key] = value;
}
JsonIndex::operator Json()
{
return m_object[m_key];
}
/* * * * * * * * * * * * * * * * * * * *
* Value wrappers
*/
@ -156,7 +177,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); }
};
@ -236,16 +257,16 @@ public:
};
class JsonArray final : public Value<Json::ARRAY, Json::array> {
const Json::array &array_items() const override { return m_value; }
const Json & operator[](size_t i) const override;
Json::array &array_items() override { return m_value; }
Json & operator[](size_t i) override;
public:
explicit JsonArray(const Json::array &value) : Value(value) {}
explicit JsonArray(Json::array &&value) : Value(move(value)) {}
};
class JsonObject final : public Value<Json::OBJECT, Json::object> {
const Json::object &object_items() const override { return m_value; }
const Json & operator[](const string &key) const override;
Json::object &object_items() override { return m_value; }
Json & operator[](const string &key) override;
public:
explicit JsonObject(const Json::object &value) : Value(value) {}
explicit JsonObject(Json::object &&value) : Value(move(value)) {}
@ -320,14 +341,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]; }
vector<Json> & Json::array_items() { return m_ptr->array_items(); }
map<string, Json> & Json::object_items() { return m_ptr->object_items(); }
Json & Json::operator[] (size_t i) { return (*m_ptr)[i]; }
Json & Json::operator[] (const string &key) { return (*m_ptr)[key]; }
double JsonValue::number_value() const { return 0; }
int64_ JsonValue::int64_value() const { return 0; }
@ -335,16 +356,16 @@ uint64_ JsonValue::uint64_value() const { return
bool JsonValue::bool_value() const { return false; }
string JsonValue::as_string() const { return statics().empty_string; }
const string & JsonValue::string_value() const { return statics().empty_string; }
const vector<Json> & JsonValue::array_items() const { return statics().empty_vector; }
const map<string, Json> & JsonValue::object_items() const { return statics().empty_map; }
const Json & JsonValue::operator[] (size_t) const { return static_null(); }
const Json & JsonValue::operator[] (const string &) const { return static_null(); }
vector<Json> & JsonValue::array_items() { return statics().empty_vector; }
map<string, Json> & JsonValue::object_items() { return statics().empty_map; }
Json & JsonValue::operator[] (size_t) { return static_null(); }
Json & JsonValue::operator[] (const string &) { return static_null(); }
const Json & JsonObject::operator[] (const string &key) const {
Json & JsonObject::operator[] (const string &key) {
auto iter = m_value.find(key);
return (iter == m_value.end()) ? static_null() : iter->second;
}
const Json & JsonArray::operator[] (size_t i) const {
Json & JsonArray::operator[] (size_t i) {
if (i >= m_value.size()) return static_null();
else return m_value[i];
}
@ -769,8 +790,6 @@ struct JsonParser final {
return fail("expected ',' in object, got " + esc(ch));
ch = get_next_token();
if (ch == '}')
break;
}
return data;
}
@ -794,8 +813,6 @@ struct JsonParser final {
return fail("expected ',' in list, got " + esc(ch));
ch = get_next_token();
if (ch == ']')
break;
(void)ch;
}
return data;

View File

@ -150,7 +150,7 @@ public:
int64_ int64_value() const;
uint64_ uint64_value() const;
// Return the enclosed string if this is a string, "" otherwise.
// Return the enclosed value as string.
std::string as_string() const;
// Return the enclosed value if this is a boolean, false otherwise.
@ -158,14 +158,14 @@ public:
// Return the enclosed string if this is a string, "" otherwise.
const std::string &string_value() const;
// Return the enclosed std::vector if this is an array, or an empty vector otherwise.
const array &array_items() const;
array &array_items();
// Return the enclosed std::map if this is an object, or an empty map otherwise.
const object &object_items() const;
object &object_items();
// Return a reference to arr[i] if this is an array, Json() otherwise.
const Json & operator[](size_t i) const;
Json & operator[](size_t i);
// Return a reference to obj[key] if this is an object, Json() otherwise.
const Json & operator[](const std::string &key) const;
Json & operator[](const std::string &key);
// Serialize.
void dump(std::string &out) const;
@ -220,9 +220,20 @@ public:
bool has_shape(const shape & types, std::string & err) const;
private:
friend class JsonIndex;
std::shared_ptr<JsonValue> m_ptr;
};
class JsonIndex {
protected:
Json m_object;
std::string m_key;
public:
JsonIndex(Json obj, const std::string & key);
Json &operator = (const Json & value);
operator Json ();
};
// Internal class hierarchy - JsonValue objects are not exposed to users of this API.
class JsonValue {
protected:
@ -240,10 +251,10 @@ protected:
virtual bool bool_value() const;
virtual const std::string &string_value() const;
virtual std::string as_string() const;
virtual const Json::array &array_items() const;
virtual const Json &operator[](size_t i) const;
virtual const Json::object &object_items() const;
virtual const Json &operator[](const std::string &key) const;
virtual Json::array &array_items();
virtual Json &operator[](size_t i);
virtual Json::object &object_items();
virtual Json &operator[](const std::string &key);
virtual ~JsonValue() {}
};