Skip to content

Commit

Permalink
Add setter functions to change array/object items
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Nov 30, 2023
1 parent 818c5e0 commit e5868ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 21 additions & 1 deletion json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 166,7 @@ class Value : public JsonValue {
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); }
};

Expand Down Expand Up @@ -204,6 204,7 @@ class JsonString final : public Value<Json::STRING, string> {
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;
void set_array_item(size_t i, const Json &value) override;
public:
explicit JsonArray(const Json::array &value) : Value(value) {}
explicit JsonArray(Json::array &&value) : Value(move(value)) {}
Expand All @@ -212,6 213,7 @@ class JsonArray final : public Value<Json::ARRAY, Json::array> {
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;
void set_object_item(const std::string &key, const Json &value) override;
public:
explicit JsonObject(const Json::object &value) : Value(value) {}
explicit JsonObject(Json::object &&value) : Value(move(value)) {}
Expand Down Expand Up @@ -277,6 279,13 @@ const map<string, Json> & Json::object_items() const { return m_ptr->object_i
const Json & Json::operator[] (size_t i) const { return (*m_ptr)[i]; }
const Json & Json::operator[] (const string &key) const { return (*m_ptr)[key]; }

void Json::set_array_item(size_t i, const Json &value) {
m_ptr->set_array_item(i, value);
}
void Json::set_object_item(const std::string &key, const Json &value) {
m_ptr->set_object_item(key, value);
}

double JsonValue::number_value() const { return 0; }
int JsonValue::int_value() const { return 0; }
bool JsonValue::bool_value() const { return false; }
Expand All @@ -285,6 294,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(); }
void JsonValue::set_array_item(size_t, const Json &) { }
void JsonValue::set_object_item(const std::string &, const Json &) { }

const Json & JsonObject::operator[] (const string &key) const {
auto iter = m_value.find(key);
Expand All @@ -295,6 306,15 @@ const Json & JsonArray::operator[] (size_t i) const {
else return m_value[i];
}

void JsonObject::set_object_item(const string &key, const Json &value) {
m_value[key] = value;
}
void JsonArray::set_array_item(size_t i, const Json &value) {
if (i >= m_value.size())
m_value.resize(i 1);
m_value[i] = value;
}

/* * * * * * * * * * * * * * * * * * * *
* Comparison
*/
Expand Down
6 changes: 6 additions & 0 deletions json11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 152,10 @@ class Json final {
// Return a reference to obj[key] if this is an object, Json() otherwise.
const Json & operator[](const std::string &key) const;

// Setters
void set_array_item(size_t i, const Json &value);
void set_object_item(const std::string &key, const Json &value);

// Serialize.
void dump(std::string &out) const;
std::string dump() const {
Expand Down Expand Up @@ -226,6 230,8 @@ class JsonValue {
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 void set_array_item(size_t i, const Json &value);
virtual void set_object_item(const std::string &key, const Json &value);
virtual ~JsonValue() {}
};

Expand Down

0 comments on commit e5868ff

Please sign in to comment.