Compare commits

...

1 Commits

Author SHA1 Message Date
Vitaliy Filippov f88fdab2ab Add json object and array mutation methods 2022-03-13 16:30:25 +03:00
2 changed files with 21 additions and 1 deletions

View File

@ -156,7 +156,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); }
};
@ -238,6 +238,7 @@ 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;
const Json & set(size_t i, Json v) override;
public:
explicit JsonArray(const Json::array &value) : Value(value) {}
explicit JsonArray(Json::array &&value) : Value(move(value)) {}
@ -246,6 +247,7 @@ public:
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;
const Json & set(const string &key, Json v) override;
public:
explicit JsonObject(const Json::object &value) : Value(value) {}
explicit JsonObject(Json::object &&value) : Value(move(value)) {}
@ -328,6 +330,8 @@ const vector<Json> & Json::array_items() const { return m_ptr->array_it
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]; }
const Json & Json::set(size_t i, Json v) { return (*m_ptr).set(i, v); }
const Json & Json::set(const std::string &k, Json v) { return (*m_ptr).set(k, v); }
double JsonValue::number_value() const { return 0; }
int64_ JsonValue::int64_value() const { return 0; }
@ -339,6 +343,8 @@ const vector<Json> & JsonValue::array_items() const { return
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(); }
const Json & JsonValue::set(size_t i, Json v) { return static_null(); }
const Json & JsonValue::set(const std::string &k, Json v) { return static_null(); }
const Json & JsonObject::operator[] (const string &key) const {
auto iter = m_value.find(key);
@ -348,6 +354,13 @@ const Json & JsonArray::operator[] (size_t i) const {
if (i >= m_value.size()) return static_null();
else return m_value[i];
}
const Json & JsonObject::set(const string &key, Json v) {
return (m_value[key] = v);
}
const Json & JsonArray::set(size_t i, Json v) {
if (i >= m_value.size()) m_value.resize(i+1);
return (m_value[i] = v);
}
/* * * * * * * * * * * * * * * * * * * *
* Comparison

View File

@ -167,6 +167,11 @@ public:
// Return a reference to obj[key] if this is an object, Json() otherwise.
const Json & operator[](const std::string &key) const;
// Set array item if this is an array, return Json() otherwise.
const Json & set(size_t i, Json value);
// Set object item if this is an object, return Json() otherwise.
const Json & set(const std::string &key, Json value);
// Serialize.
void dump(std::string &out) const;
std::string dump() const {
@ -244,6 +249,8 @@ protected:
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 const Json & set(size_t i, Json value);
virtual const Json & set(const std::string &key, Json value);
virtual ~JsonValue() {}
};