Files
json/docs/mkdocs/docs/api/ordered_json.md
T
Niels Lohmann 52b72b5684 Improve documentation: byte layouts, release pages, See also, dark-mode images (#5906)
- Make the iterator range illustrations legible in dark mode (prefers-color-scheme
  rules inside the SVGs).
- Add a "Byte layout" section with a color-coded byte table to each binary format
  page (BJData, BON8, BSON, CBOR, MessagePack, UBJSON), and highlight the smallest
  size per file in the comparison table.
- Add "See also" sections to the overview pages that lacked one.
- Split the release notes into one page per release, grouped by minor version on
  the overview page, with an "API changes" section per release generated from the
  API pages' version histories (hooks/api_changes.py).
- Fact-check the release notes and version histories against the release tags and
  correct wrong or misleading entries (e.g., json_serializer 2.1.0, merge_patch
  3.1.0, items, count, operator[], to_bjdata, std::string_view key support).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 17:57:44 +02:00

1.3 KiB

nlohmann::ordered_json

using ordered_json = basic_json<ordered_map>;

This type preserves the insertion order of object keys.

Iterator invalidation

The type is based on ordered_map which in turn uses a std::vector to store object elements. Therefore, adding object elements can yield a reallocation in which case all iterators (including the end() iterator) and all references to the elements are invalidated. Also, any iterator or reference after the insertion point will point to the same index, which is now a different value.

Complexity

ordered_map has no lookup index: every key-based object operation is a linear scan, so building or parsing an object of n keys costs O(n²) rather than O(n log n). See ordered_map complexity for the per-operation table and for measured numbers.

Examples

??? example

The example below demonstrates how `ordered_json` preserves the insertion order of object keys.

```cpp
--8<-- "examples/ordered_json.cpp"
```

Output:

```json
--8<-- "examples/ordered_json.output"
```

See also

Version history

Added in version 3.9.0.