This commit is contained in:
nlohmann
2026-10-26 21:24:35 +00:00
parent 41c601a7d1
commit d2e264bd84
1400 changed files with 11859 additions and 5440 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ compile; see [`parse`](parse.md#notes).
#include <nlohmann/json.hpp>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -48,7 +48,7 @@ Example
#include <nlohmann/json.hpp>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
@@ -43,7 +43,7 @@ Constant.
#include <utility>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
@@ -38,7 +38,7 @@ The example below shows the default constructor and demonstrates that `basic_jso
#include <utility>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+4 -4
View File
@@ -12,7 +12,7 @@ void erase(view_type array, I idx);
std::size_t erase(const json_pointer& ptr);
```
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
has `erase`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
1. Removes every member of `object` whose key is `key` (see [Notes](#notes) on duplicate keys) and returns how many
@@ -109,9 +109,9 @@ by an erase, not only those at or after the removed element, since the container
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view;
using nlohmann::json;
using nlohmann::json_editable_document;
using nlohmann::json_editable_view;
int main()
{
File diff suppressed because one or more lines are too long
+4 -4
View File
@@ -12,7 +12,7 @@ void erase(view_type array, I idx);
std::size_t erase(const json_pointer& ptr);
```
Only an **editable** document (`Editable == true`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `erase`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
Only an **editable** document (`view_mode::editable`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `erase`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
1. Removes every member of `object` whose key is `key` (see [Notes](#notes) on duplicate keys) and returns how many were removed; `0` if `object` has no member with this key.
1. Removes the element at index `idx` of `array`, which must already exist (`idx < array.size()`).
@@ -72,9 +72,9 @@ The example below drops a deprecated field and a decommissioned entry from a con
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view;
using nlohmann::json;
using nlohmann::json_editable_document;
using nlohmann::json_editable_view;
int main()
{
File diff suppressed because one or more lines are too long
+9 -9
View File
@@ -3,7 +3,7 @@
Defined in header `<nlohmann/json_view.hpp>`
```
template<typename BasicJsonType, bool Editable = false>
template<typename BasicJsonType, view_mode Mode = view_mode::read_only>
class basic_json_document;
```
@@ -11,13 +11,13 @@ A parsed JSON text, held as a flat index of its values ([16 bytes per value](htt
A document may **borrow** the text it was parsed from (the caller's buffer must then outlive the document) or **own** it (a copy, or an rvalue `std::string` that was moved in); see [`owns_source`](https://json.nlohmann.me/api/basic_json_document/owns_source/index.md). `basic_json_document` is move-only: a copy would duplicate the whole index, the decoded-string buffer, and the owned text, and a move is enough in every use we know of. The index, the decoded strings, and the owned text live in one heap block that the document holds through a `std::unique_ptr`; this is also why views survive a move of the document.
With `Editable == true`, the document also offers [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md), [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), [`insert`](https://json.nlohmann.me/api/basic_json_document/insert/index.md), and [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md) to change values in place, see [Edits](#edits) below. The source text itself is never written; a read-only document (`Editable == false`, the default) does not carry any of the bookkeeping edits need, and calling any of them on one fails to compile (`static_assert`).
With `Mode == view_mode::editable`, the document also offers [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md), [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), [`insert`](https://json.nlohmann.me/api/basic_json_document/insert/index.md), and [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md) to change values in place, see [Edits](#edits) below. The source text itself is never written; a read-only document (`view_mode::read_only`, the default) does not carry any of the bookkeeping edits need, and calling any of them on one fails to compile (`static_assert`).
## Template parameters
`BasicJsonType` : a specialization of [`basic_json`](https://json.nlohmann.me/api/basic_json/index.md), for instance [`json`](https://json.nlohmann.me/api/json/index.md) or [`ordered_json`](https://json.nlohmann.me/api/ordered_json/index.md). Only 64-bit `number_integer_t`/`number_unsigned_t` types are supported; this is checked with a `static_assert`.
`Editable` : whether the document supports [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md), [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), [`insert`](https://json.nlohmann.me/api/basic_json_document/insert/index.md), and [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md) (optional, `false` by default). See [Edits](#edits) below.
`Mode` : a [`view_mode`](https://json.nlohmann.me/api/view_mode/index.md): `view_mode::editable` if the document supports [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md), [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), [`insert`](https://json.nlohmann.me/api/basic_json_document/insert/index.md), and [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md) (optional, `view_mode::read_only` by default). See [Edits](#edits) below.
## Specializations
@@ -28,7 +28,7 @@ With `Editable == true`, the document also offers [`set`](https://json.nlohmann.
## Member types
- **view_type** - the type of view returned by [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md) (`basic_json_view<BasicJsonType, Editable>`)
- **view_type** - the type of view returned by [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md) (`basic_json_view<BasicJsonType, Mode>`)
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](https://json.nlohmann.me/api/basic_json/value_t/index.md)
- **image_check** - how [`load()`](https://json.nlohmann.me/api/basic_json_document/load/index.md) validates an image (`full`, `bounds`, `none`), see [`image_check`](https://json.nlohmann.me/api/basic_json_document/load/#image_check)
@@ -60,14 +60,14 @@ With `Editable == true`, the document also offers [`set`](https://json.nlohmann.
### Edits
- [**set**](https://json.nlohmann.me/api/basic_json_document/set/index.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers to (`Editable` documents only)
- [**push_back**](https://json.nlohmann.me/api/basic_json_document/push_back/index.md) - append to an array (`Editable` documents only)
- [**insert**](https://json.nlohmann.me/api/basic_json_document/insert/index.md) - insert an element into an array before a given position (`Editable` documents only)
- [**erase**](https://json.nlohmann.me/api/basic_json_document/erase/index.md) - remove an object member, an array element, or the value a JSON pointer refers to (`Editable` documents only)
- [**set**](https://json.nlohmann.me/api/basic_json_document/set/index.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers to (editable documents only)
- [**push_back**](https://json.nlohmann.me/api/basic_json_document/push_back/index.md) - append to an array (editable documents only)
- [**insert**](https://json.nlohmann.me/api/basic_json_document/insert/index.md) - insert an element into an array before a given position (editable documents only)
- [**erase**](https://json.nlohmann.me/api/basic_json_document/erase/index.md) - remove an object member, an array element, or the value a JSON pointer refers to (editable documents only)
## Edits
An editable document (`Editable == true`) can be changed after parsing, with [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md), [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), [`insert`](https://json.nlohmann.me/api/basic_json_document/insert/index.md), and [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md); [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md) and [`ordered_json_editable_document`](https://json.nlohmann.me/api/ordered_json_editable_document/index.md) are the corresponding specializations. A few points apply to every edit:
An editable document (`view_mode::editable`) can be changed after parsing, with [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md), [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), [`insert`](https://json.nlohmann.me/api/basic_json_document/insert/index.md), and [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md); [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md) and [`ordered_json_editable_document`](https://json.nlohmann.me/api/ordered_json_editable_document/index.md) are the corresponding specializations. A few points apply to every edit:
- **The source text is never written**, and the parsed index never moves: every value keeps the node it was parsed into, so [views](https://json.nlohmann.me/api/basic_json_view/index.md) taken before an edit stay valid, including [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md). New values (and the element sequences of an edited array/object) go to storage owned by the document, allocated on demand.
- **A view keeps referring to the same value.** After [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md) replaces the value a view refers to, that view sees the new value; a view of a value that a later edit replaces or drops keeps showing what it last held. An edit of an array or object, however, **invalidates the iterators taken over it** (its members may now live in a different sequence), and a string obtained with [`get_string()`](https://json.nlohmann.me/api/basic_json_view/get_string/index.md) stays valid even as further edits happen (earlier buffers of edited text are kept alive, not overwritten).
+4 -4
View File
@@ -5,7 +5,7 @@ template<typename I, typename V>
view_type insert(view_type array, I idx, V&& value);
```
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
has `insert`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
Inserts `value` into `array` as a new element before position `idx`, which must not be past the end
@@ -95,9 +95,9 @@ an edit in general.
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view;
using nlohmann::json;
using nlohmann::json_editable_document;
using nlohmann::json_editable_view;
int main()
{
File diff suppressed because one or more lines are too long
+4 -4
View File
@@ -5,7 +5,7 @@ template<typename I, typename V>
view_type insert(view_type array, I idx, V&& value);
```
Only an **editable** document (`Editable == true`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `insert`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
Only an **editable** document (`view_mode::editable`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `insert`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
Inserts `value` into `array` as a new element before position `idx`, which must not be past the end (`idx <= array.size()`; `idx == array.size()` appends, like [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md)). Unlike [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), a [null](https://json.nlohmann.me/api/basic_json_view/is_null/index.md) `array` does *not* first become an empty array: `array` must already be an array.
@@ -55,9 +55,9 @@ The example below inserts a step into the middle of a deployment plan, without t
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view;
using nlohmann::json;
using nlohmann::json_editable_document;
using nlohmann::json_editable_view;
int main()
{
+1 -1
View File
@@ -33,7 +33,7 @@ When the document is discarded, [`root()`](root.md) returns a discarded view (it
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
@@ -30,7 +30,7 @@ Example
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+6 -6
View File
@@ -55,10 +55,7 @@ validate with overload 2 first, if the original vector must survive a failed loa
## Exceptions
On a big-endian target, throws [`type_error.320`](../../home/exceptions.md#jsonexceptiontype_error320) -- the same
exception [`save()`](save.md#exceptions) throws there, since the image format is little-endian only.
Otherwise throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if `image` is not one
Throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if `image` is not one
`save()` could have written, or fails the requested `check`:
| message | when |
@@ -95,6 +92,9 @@ are checked once, however many nodes refer to it.
## Notes
**Little-endian targets only.** On a big-endian target, `load()` does not exist, just like
[`save()`](save.md#notes): the image format is little-endian, so a call fails to compile.
### `image_check` { #image_check }
```cpp
@@ -164,8 +164,8 @@ returned document, and any [view](../basic_json_view/index.md) taken from it, is
#include <vector>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_document = nlohmann::json_document;
using nlohmann::json;
using nlohmann::json_document;
using image_check = json_document::image_check;
int main()
File diff suppressed because one or more lines are too long
+5 -5
View File
@@ -40,9 +40,7 @@ Overload 3 moves `image` into the document *before* validating it, so that a goo
## Exceptions
On a big-endian target, throws [`type_error.320`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error320) -- the same exception [`save()`](https://json.nlohmann.me/api/basic_json_document/save/#exceptions) throws there, since the image format is little-endian only.
Otherwise throws [`parse_error.116`](https://json.nlohmann.me/home/exceptions/#jsonexceptionparse_error116) if `image` is not one `save()` could have written, or fails the requested `check`:
Throws [`parse_error.116`](https://json.nlohmann.me/home/exceptions/#jsonexceptionparse_error116) if `image` is not one `save()` could have written, or fails the requested `check`:
| message | when |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -77,6 +75,8 @@ With `image_check::full`, linear in the size of the image (the nodes, the text,
## Notes
**Little-endian targets only.** On a big-endian target, `load()` does not exist, just like [`save()`](https://json.nlohmann.me/api/basic_json_document/save/#notes): the image format is little-endian, so a call fails to compile.
### `image_check`
```
@@ -117,8 +117,8 @@ The example below saves a parsed document as an image, checks that `load()` repr
#include <vector>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_document = nlohmann::json_document;
using nlohmann::json;
using nlohmann::json_document;
using image_check = json_document::image_check;
int main()
+1 -1
View File
@@ -35,7 +35,7 @@ to observe the effect of [`shrink_to_fit()`](shrink_to_fit.md).
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
@@ -31,7 +31,7 @@ Example
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+1 -1
View File
@@ -33,7 +33,7 @@ strings and, for an owned document, the text).
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -30,7 +30,7 @@ Example
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+1 -1
View File
@@ -33,7 +33,7 @@ document (`#!cpp owns_source() == false`) is only valid while the buffer it was
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -31,7 +31,7 @@ Example
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+2 -2
View File
@@ -133,7 +133,7 @@ buffer of a given length, pass a pair of pointers: `#!cpp parse(ptr, ptr + len)`
#include <nlohmann/json.hpp>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
@@ -180,7 +180,7 @@ buffer of a given length, pass a pair of pointers: `#!cpp parse(ptr, ptr + len)`
#include <vector>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -97,7 +97,7 @@ Example: (1) borrowed vs. owned input, and errors identical to `BasicJsonType::p
#include <nlohmann/json.hpp>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
@@ -142,7 +142,7 @@ Example: (2) parse an iterator range (no NUL terminator required)
#include <vector>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+1 -1
View File
@@ -64,7 +64,7 @@ compile; see [`parse`](parse.md#notes).
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
json_document parse_from_temporary_buffer()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -52,7 +52,7 @@ The example below returns a document from a function whose local buffer would ot
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
json_document parse_from_temporary_buffer()
{
+3 -3
View File
@@ -13,7 +13,7 @@ editable, and it does not have to be `array`'s own document -- which is copied s
source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers,
strings, `#!cpp bool`, `#!cpp nullptr`, containers, ...).
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
## Template parameters
@@ -82,8 +82,8 @@ Like [`set`](set.md) on a member or an element, `push_back` never moves an exist
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using nlohmann::json;
using nlohmann::json_editable_document;
int main()
{
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -9,7 +9,7 @@ Appends `value` as a new last element of `array`. A [null](https://json.nlohmann
`value` is accepted three ways: a [`basic_json_view`](https://json.nlohmann.me/api/basic_json_view/index.md) of *any* document -- read-only or editable, and it does not have to be `array`'s own document -- which is copied so that nothing is shared with the source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers, strings, `bool`, `nullptr`, containers, ...).
Only an **editable** document (`Editable == true`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
Only an **editable** document (`view_mode::editable`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
## Template parameters
@@ -51,8 +51,8 @@ The example below appends records to an array one at a time, as they might arriv
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using nlohmann::json;
using nlohmann::json_editable_document;
int main()
{
+1 -1
View File
@@ -72,7 +72,7 @@ compile; see [`parse`](parse.md#notes).
#include <vector>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -54,7 +54,7 @@ The example below parses a sequence of messages into the same document, reusing
#include <vector>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+1 -1
View File
@@ -50,7 +50,7 @@ returned view is valid under the same conditions as any other view of the docume
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -44,7 +44,7 @@ Example
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+5 -11
View File
@@ -12,7 +12,7 @@ An edited document is written in its *current* state, with its values in documen
parser would have produced them for that JSON text: a member [`set`](set.md) added goes at the end, an
[`erase`](erase.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity)
becomes null, the same substitution [`dump()`](../basic_json_view/dump.md) makes. The same document always saves to
the same bytes -- also across `BasicJsonType` and `#!cpp Editable`, since the image reflects document order and
the same bytes -- also across `BasicJsonType` and the [`view_mode`](../view_mode.md), since the image reflects document order and
values only, not which specialization produced them.
## Return value
@@ -31,9 +31,6 @@ Throws [`type_error.320`](../../home/exceptions.md#jsonexceptiontype_error320) i
[discarded](is_discarded.md) -- a default-constructed document, or one a failed [`parse()`](parse.md)/
[`read()`](read.md) with `allow_exceptions == false` left discarded.
On a big-endian target, throws `type_error.320` with a different message instead: the image format is little-endian
only (see [Notes](#notes)).
Throws [`out_of_range.416`](../../home/exceptions.md#jsonexceptionout_of_range416) if the node count, the text, or
the decoded strings of the image would individually reach 4 GiB -- the same 32-bit offsets
[`parse()`](parse.md#exceptions) and, for edits, [`set`](set.md)/[`push_back`](push_back.md) are already limited to.
@@ -44,9 +41,6 @@ the decoded strings of the image would individually reach 4 GiB -- the same 32-b
[json.exception.type_error.320] cannot save a discarded json_document
```
```
[json.exception.type_error.320] json_document images need a little-endian target
```
```
[json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document
```
@@ -70,9 +64,9 @@ else -- see [`load`'s Exceptions](load.md#exceptions).
the *same* build on the *same* (little-endian) machine -- not as a long-term storage format. Keep the original
JSON text if you need to read a saved document back with a future library version.
**Little-endian only.** The image is written as raw little-endian bytes, with no byte-swapping. `save()` (and
[`load`](load.md)) throw `type_error.320` on a big-endian target rather than silently produce bytes a big-endian
reader could not interpret correctly.
**Little-endian targets only.** The image is written as raw little-endian bytes, with no byte-swapping. On a
big-endian target, `save()` and [`load`](load.md) do not exist: a call fails to compile rather than produce bytes a
big-endian reader could not interpret correctly.
## Examples
@@ -89,7 +83,7 @@ reader could not interpret correctly.
#include <vector>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+3 -9
View File
@@ -6,7 +6,7 @@ std::vector<std::uint8_t> save() const;
Writes the document as an *image*: a byte buffer that [`load`](https://json.nlohmann.me/api/basic_json_document/load/index.md) reads back without parsing. The image holds the node index, the source text (plus, for an edited document, the number tokens edits wrote), and the decoded strings (plus the strings edits wrote) -- everything [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md) needs, with nothing left to parse.
An edited document is written in its *current* state, with its values in document order, the way the library's own parser would have produced them for that JSON text: a member [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md) added goes at the end, an [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity) becomes null, the same substitution [`dump()`](https://json.nlohmann.me/api/basic_json_view/dump/index.md) makes. The same document always saves to the same bytes -- also across `BasicJsonType` and `Editable`, since the image reflects document order and values only, not which specialization produced them.
An edited document is written in its *current* state, with its values in document order, the way the library's own parser would have produced them for that JSON text: a member [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md) added goes at the end, an [`erase`](https://json.nlohmann.me/api/basic_json_document/erase/index.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity) becomes null, the same substitution [`dump()`](https://json.nlohmann.me/api/basic_json_view/dump/index.md) makes. The same document always saves to the same bytes -- also across `BasicJsonType` and the [`view_mode`](https://json.nlohmann.me/api/view_mode/index.md), since the image reflects document order and values only, not which specialization produced them.
## Return value
@@ -20,8 +20,6 @@ Strong guarantee: `save()` does not modify `*this` (it is `const`), so if it thr
Throws [`type_error.320`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error320) if the document is [discarded](https://json.nlohmann.me/api/basic_json_document/is_discarded/index.md) -- a default-constructed document, or one a failed [`parse()`](https://json.nlohmann.me/api/basic_json_document/parse/index.md)/ [`read()`](https://json.nlohmann.me/api/basic_json_document/read/index.md) with `allow_exceptions == false` left discarded.
On a big-endian target, throws `type_error.320` with a different message instead: the image format is little-endian only (see [Notes](#notes)).
Throws [`out_of_range.416`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range416) if the node count, the text, or the decoded strings of the image would individually reach 4 GiB -- the same 32-bit offsets [`parse()`](https://json.nlohmann.me/api/basic_json_document/parse/#exceptions) and, for edits, [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md)/[`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md) are already limited to.
Example messages
@@ -30,10 +28,6 @@ Example messages
[json.exception.type_error.320] cannot save a discarded json_document
```
```
[json.exception.type_error.320] json_document images need a little-endian target
```
```
[json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document
```
@@ -50,7 +44,7 @@ Experimental
The image format is versioned but not yet stable: it may change in an incompatible way before it is declared stable. Use images to cache a document within one build of the library, or to hand one to another process running the *same* build on the *same* (little-endian) machine -- not as a long-term storage format. Keep the original JSON text if you need to read a saved document back with a future library version.
**Little-endian only.** The image is written as raw little-endian bytes, with no byte-swapping. `save()` (and [`load`](https://json.nlohmann.me/api/basic_json_document/load/index.md)) throw `type_error.320` on a big-endian target rather than silently produce bytes a big-endian reader could not interpret correctly.
**Little-endian targets only.** The image is written as raw little-endian bytes, with no byte-swapping. On a big-endian target, `save()` and [`load`](https://json.nlohmann.me/api/basic_json_document/load/index.md) do not exist: a call fails to compile rather than produce bytes a big-endian reader could not interpret correctly.
## Examples
@@ -65,7 +59,7 @@ The example below saves a parsed configuration as an image -- the way a service
#include <vector>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+4 -4
View File
@@ -18,7 +18,7 @@ template<typename V>
view_type set(const json_pointer& ptr, V&& value);
```
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
has `set`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
1. Replaces the value `target` refers to with `value`.
@@ -180,9 +180,9 @@ one-node scalar.
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view;
using nlohmann::json;
using nlohmann::json_editable_document;
using nlohmann::json_editable_view;
int main()
{
File diff suppressed because one or more lines are too long
+4 -4
View File
@@ -18,7 +18,7 @@ template<typename V>
view_type set(const json_pointer& ptr, V&& value);
```
Only an **editable** document (`Editable == true`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `set`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
Only an **editable** document (`view_mode::editable`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `set`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
1. Replaces the value `target` refers to with `value`.
1. Sets the member `key` of the object `object` to `value`: assigns it if `object` already has a member with this key -- the first one, should the key occur more than once, and the later duplicates are then dropped (see the [Notes](#notes) below) -- or appends a new member at the end otherwise. A [null](https://json.nlohmann.me/api/basic_json_view/is_null/index.md) `object` first becomes an empty object.
@@ -96,9 +96,9 @@ The example below edits a small configuration document -- replacing a value, add
#include <iostream>
#include <nlohmann/json_view.hpp>
using json = nlohmann::json;
using json_editable_document = nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view;
using nlohmann::json;
using nlohmann::json_editable_document;
using nlohmann::json_editable_view;
int main()
{
+1 -1
View File
@@ -43,7 +43,7 @@ possible would be an internal optimization only, not a guarantee to rely on.
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
@@ -36,7 +36,7 @@ Example
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
+1 -1
View File
@@ -33,7 +33,7 @@ is; see [`owns_source`](owns_source.md).
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -31,7 +31,7 @@ Example
#include <string>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using nlohmann::json_document;
int main()
{