#include #include using nlohmann::basic_json_view; using nlohmann::view_mode; // accepts the views of read-only and of editable documents, of json and of ordered_json template std::size_t count_members(basic_json_view v) { return v.is_object() ? v.size() : 0; } // accepts only the views of editable documents template bool is_edited(basic_json_view v) { return v.is_string() && v.get_string() == "edited"; } // a function (no template) taking a json_editable_view also accepts the view of a read-only document std::size_t size_of(nlohmann::json_editable_view v) { return v.size(); } int main() { const nlohmann::json_document doc = nlohmann::json_document::parse(R"({"a": 1, "b": 2})"); nlohmann::json_editable_document edoc = nlohmann::json_editable_document::parse(R"({"a": 1})"); edoc.set(edoc.root()["a"], "edited"); std::cout << count_members(doc.root()) << ' ' << count_members(edoc.root()) << '\n'; std::cout << std::boolalpha << is_edited(edoc.root()["a"]) << '\n'; std::cout << size_of(doc.root()) << ' ' << size_of(edoc.root()) << '\n'; }