dropbox / json11

A tiny JSON library for C++11.
MIT License
2.55k stars 616 forks source link

Making object_items(), array_items() not const or alternatively support mutable_object_items() #143

Closed mohitmv closed 5 years ago

mohitmv commented 5 years ago

It will be highly useful if you make object_items() and array_items() not-const reference. or if you support mutable_object_items() ?

Thanks.

artwyman commented 5 years ago

The Json object is immutable by design, which has its own set of benefits particularly around safety and concurrency. That said, there's nothing stopping you from extracting the map of keys to values and mutating it freely (the Json::object type is a map) then wrapping it back in a Json object when you're done. Something like this (not compiled, so meant to be demonstrative not fully correct):

auto mutable_map = json_obj.object_items()
mutable_map["a"] = Json(1);
json_obj = Json(std::move(mutable_map))