Skip to content

nlohmann::ordered_json_editable_document

Defined in header <nlohmann/json_view.hpp>

using ordered_json_editable_document = basic_json_document<ordered_json, true>;

This type is an editable basic_json_document of the ordered_json specialization: set and push_back change values after parsing, as for json_editable_document, and materialize() preserves the document order of object members -- including members set added -- instead of sorting them like json_editable_document does.

Examples

Example

The example below edits a document with set, then shows that materialize() keeps the member order of the source text (with the new member at the end) for ordered_json_editable_document, where it would sort the members alphabetically for json_editable_document.

#include <iostream>
#include <nlohmann/json_view.hpp>

using ordered_json_editable_document = nlohmann::ordered_json_editable_document;

int main()
{
    // ordered_json_editable_document is basic_json_document<nlohmann::ordered_json, true>
    ordered_json_editable_document doc = ordered_json_editable_document::parse(R"({"z": 1, "a": 2, "m": 3})");
    doc.set(doc.root(), "b", 4); // set() always appends a new member at the end

    // materialize() preserves the document order (with "b" at the end),
    // instead of sorting the keys the way json_editable_document does
    std::cout << doc.root().materialize().dump() << '\n';
}

Output:

{"z":1,"a":2,"m":3,"b":4}

See also

Version history

Since version 3.13.0 unreleased.