deploy: 4155e2938f
This commit is contained in:
1 parent
be4d0a6f4b
commit
90f0c9d411
714 files changed
+22067
-622
No files matched your search
@@ -0,0 +1,220 @@
|
||||
# <small>nlohmann::basic_json_view::</small>at
|
||||
|
||||
```cpp
|
||||
// (1)
|
||||
basic_json_view at(string_view_t key) const;
|
||||
basic_json_view at(const char* key) const;
|
||||
basic_json_view at(const string_t& key) const;
|
||||
|
||||
// (2)
|
||||
basic_json_view at(size_type idx) const;
|
||||
template<typename IntegerType>
|
||||
basic_json_view at(IntegerType idx) const;
|
||||
|
||||
// (3)
|
||||
basic_json_view at(const json_pointer& ptr) const;
|
||||
```
|
||||
|
||||
1. Returns the value of the object member with key `key` -- the first one, should the key occur more than once (see
|
||||
[Notes on duplicate keys](operator[].md#notes)).
|
||||
2. Returns the array element at index `idx`. The template accepts every integer type except `#!cpp bool` and
|
||||
`#!cpp std::size_t` and forwards to the `size_type` overload, as for [`operator[]`](operator[].md); a negative
|
||||
`idx` is out of range.
|
||||
3. Returns the value a JSON pointer `ptr` refers to, starting at this value.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in)
|
||||
: object key of the element to access
|
||||
|
||||
`idx` (in)
|
||||
: index of the element to access
|
||||
|
||||
`ptr` (in)
|
||||
: JSON pointer to the element to access
|
||||
|
||||
## Return value
|
||||
|
||||
1. the value of the first member with key `key`
|
||||
2. the element at index `idx`
|
||||
3. the value `ptr` resolves to, starting at this value
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
1. The function can throw the following exceptions, both with the same message as the corresponding call to
|
||||
[`BasicJsonType::at`](../basic_json/at.md):
|
||||
- Throws [`type_error.304`](../../home/exceptions.md#jsonexceptiontype_error304) if the value is not an object.
|
||||
- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if no member has key `key`.
|
||||
2. The function can throw the following exceptions, both with the same message as the corresponding call to
|
||||
[`BasicJsonType::at`](../basic_json/at.md):
|
||||
- Throws [`type_error.304`](../../home/exceptions.md#jsonexceptiontype_error304) if the value is not an array.
|
||||
- Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) if `#!cpp idx >= size()`.
|
||||
3. The function can throw the following exceptions, all with the same message as the corresponding call to
|
||||
[`BasicJsonType::at`](../basic_json/at.md):
|
||||
- Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index in `ptr`
|
||||
begins with `#!cpp '0'`.
|
||||
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in `ptr` is
|
||||
not a number.
|
||||
- Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) if an array index in `ptr`
|
||||
is out of range.
|
||||
- Throws [`out_of_range.402`](../../home/exceptions.md#jsonexceptionout_of_range402) if a reference token is
|
||||
`#!cpp "-"` at an array -- `at` never inserts an element, so `#!cpp "-"` is always invalid.
|
||||
- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a reference token names
|
||||
an object member that does not exist.
|
||||
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if `ptr` cannot be resolved
|
||||
because a reference token is used on a primitive value.
|
||||
|
||||
None of these exceptions carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no
|
||||
`BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with
|
||||
`JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
1. Linear in the number of members: as for [`ordered_json`](../ordered_json.md), members are compared one after
|
||||
another, in document order, stopping at the first match. Each comparison first checks the
|
||||
key's length -- already known from the index, without reading the key bytes -- before comparing its content.
|
||||
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
||||
constant time on average.
|
||||
2. Linear in `idx`: elements are skipped one at a time from the first one, since they are not a fixed size in the
|
||||
index (unlike `BasicJsonType`'s array, which is random-access).
|
||||
3. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at
|
||||
that level (as 1.) or the index into the array (as 2.).
|
||||
|
||||
## Notes
|
||||
|
||||
!!! warning "Duplicate keys: the first member wins"
|
||||
|
||||
If the source text repeats a key, this resolves to the *first* member with it, not to the last one that
|
||||
[`materialize()`](materialize.md) and `parse()` keep. See the [Notes on duplicate keys](operator[].md#notes) of
|
||||
`operator[]` and [Duplicate keys](../../features/json_view.md#duplicate-keys) for the reasons and for how to get
|
||||
the last value.
|
||||
|
||||
Unlike [`operator[]`](operator[].md), which returns a [discarded](is_discarded.md) view for a missing key or an
|
||||
out-of-range index, `at` always throws -- exactly as `BasicJsonType::at` does, and with the same messages, so
|
||||
existing error handling written against `BasicJsonType::at` keeps working unchanged when switched to a view. This
|
||||
also holds for overload 3: unlike [`operator[]`](operator[].md) with a JSON pointer, which returns a discarded view
|
||||
for a missing key or an out-of-range index, `at` throws for those too (`out_of_range.403`/`out_of_range.401`).
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example: (1)/(2) access specified element with bounds checking"
|
||||
|
||||
The example below reads required fields out of a service configuration with `at`, and shows that the exceptions
|
||||
it throws -- for a wrong type and for a missing key -- carry the same messages
|
||||
[`BasicJsonType::at`](../basic_json/at.md) would produce for the same JSON text.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// required fields of a service configuration -- at() reports a missing
|
||||
// or wrong-typed field with the very same exception basic_json::at()
|
||||
// would throw for the equivalent nlohmann::json value, so error
|
||||
// handling written against basic_json::at() keeps working unchanged
|
||||
json_document config = json_document::parse(R"({"name": "cache", "port": "6379"})");
|
||||
const json_view service = config.root();
|
||||
|
||||
std::cout << service.at("port").materialize().dump() << '\n';
|
||||
|
||||
try
|
||||
{
|
||||
// "port" is a string, not an array
|
||||
static_cast<void>(service.at("port").at(0));
|
||||
}
|
||||
catch (const nlohmann::json::type_error& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
static_cast<void>(service.at("timeout"));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
"6379"
|
||||
[json.exception.type_error.304] cannot use at() with string
|
||||
[json.exception.out_of_range.403] key 'timeout' not found
|
||||
|
||||
```
|
||||
|
||||
??? example "Example: (3) access specified element via JSON pointer with bounds checking"
|
||||
|
||||
The example below shows that `at` with a JSON pointer throws exactly the exceptions, with exactly the messages,
|
||||
that [`BasicJsonType::at`](../basic_json/at.md) throws for the same pointer and the same document.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"region": "eu", "servers": ["eu-1", "eu-2"]})");
|
||||
const auto root = doc.root();
|
||||
|
||||
std::cout << root.at(json_pointer("/servers/1")).materialize().dump() << '\n';
|
||||
|
||||
// at() throws for every resolution failure -- with the very same
|
||||
// message json::at(ptr) would throw for the same pointer and the same
|
||||
// document
|
||||
try
|
||||
{
|
||||
static_cast<void>(root.at(json_pointer("/servers/5")));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
static_cast<void>(root.at(json_pointer("/missing")));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
"eu-2"
|
||||
[json.exception.out_of_range.401] array index 5 is out of range
|
||||
[json.exception.out_of_range.403] key 'missing' not found
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [operator[]](operator[].md) - access specified element (returns a discarded view instead of throwing)
|
||||
- [front](front.md), [back](back.md) - access the first or last element
|
||||
- [`BasicJsonType::at`](../basic_json/at.md) - the corresponding function of `basic_json`
|
||||
- [`json_pointer`](../json_pointer/index.md) - JSON pointer type used by overload 3
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,89 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/at/" rel="canonical"><link href=../basic_json_view/ rel=prev><link href=../back/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>at - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewat class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> at </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> Line truncated
|
||||
<span class=n>basic_json_view</span><span class=w> </span><span class=nf>at</span><span class=p>(</span><span class=n>string_view_t</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=n>basic_json_view</span><span class=w> </span><span class=nf>at</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=n>basic_json_view</span><span class=w> </span><span class=nf>at</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>string_t</span><span class=o>&</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
|
||||
<span class=c1>// (2)</span>
|
||||
<span class=n>basic_json_view</span><span class=w> </span><span class=nf>at</span><span class=p>(</span><span class=n>size_type</span><span class=w> </span><span class=n>idx</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=k>template</span><span class=o><</span><span class=k>typename</span><span class=w> </span><span class=nc>IntegerType</span><span class=o>></span>
|
||||
<span class=n>basic_json_view</span><span class=w> </span><span class=n>at</span><span class=p>(</span><span class=n>IntegerType</span><span class=w> </span><span class=n>idx</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
|
||||
<span class=c1>// (3)</span>
|
||||
<span class=n>basic_json_view</span><span class=w> </span><span class=nf>at</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>json_pointer</span><span class=o>&</span><span class=w> </span><span class=n>ptr</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <ol> <li>Returns the value of the object member with key <code>key</code> -- the first one, should the key occur more than once (see <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a>).</li> <li>Returns the array element at index <code>idx</code>. The template accepts every integer type except <code class=highlight><span class=kt>bool</span></code> and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span></code> and forwards to the <code>size_type</code> overload, as for <a href=../operator%5B%5D/ ><code>operator[]</code></a>; a negative <code>idx</code> is out of range.</li> <li>Returns the value a <abbr title="JavaScript Object Notation">JSON</abbr> pointer <code>ptr</code> refers to, starting at this value.</li> </ol> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>key</code> (in)</dt> <dd>object key of the element to access</dd> <dt><code>idx</code> (in)</dt> <dd>index of the element to access</dd> <dt><code>ptr</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> pointer to the element to access</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li>the value of the first member with key <code>key</code></li> <li>the element at index <code>idx</code></li> <li>the value <code>ptr</code> resolves to, starting at this value</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: there are no changes to the view or the document it refers to.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ol> <li>The function can throw the following exceptions, both with the same message as the corresponding call to <a href=../../basic_json/at/ ><code>BasicJsonType::at</code></a>:<ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error304><code>type_error.304</code></a> if the value is not an object.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range403><code>out_of_range.403</code></a> if no member has key <code>key</code>.</li> </ul> </li> <li>The function can throw the following exceptions, both with the same message as the corresponding call to <a href=../../basic_json/at/ ><code>BasicJsonType::at</code></a>:<ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error304><code>type_error.304</code></a> if the value is not an array.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range401><code>out_of_range.401</code></a> if <code class=highlight><span class=n>idx</span><span class=w> </span><span class=o>>=</span><span class=w> </span><span class=n>size</span><span class=p>()</span></code>.</li> </ul> </li> <li>The function can throw the following exceptions, all with the same message as the corresponding call to <a href=../../basic_json/at/ ><code>BasicJsonType::at</code></a>:<ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionparse_error106><code>parse_error.106</code></a> if an array index in <code>ptr</code> begins with <code class=highlight><span class=sc>'0'</span></code>.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionparse_error109><code>parse_error.109</code></a> if an array index in <code>ptr</code> is not a number.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range401><code>out_of_range.401</code></a> if an array index in <code>ptr</code> is out of range.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range402><code>out_of_range.402</code></a> if a reference token is <code class=highlight><span class=s>"-"</span></code> at an array -- <code>at</code> never inserts an element, so <code class=highlight><span class=s>"-"</span></code> is always invalid.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range403><code>out_of_range.403</code></a> if a reference token names an object member that does not exist.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range404><code>out_of_range.404</code></a> if <code>ptr</code> cannot be resolved because a reference token is used on a primitive value.</li> </ul> </li> </ol> <p>None of these exceptions carry a <a href=../../macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> path: the view has no <code>BasicJsonType</code> value to point at, so the exception is created without one, even if <code>BasicJsonType</code> was built with <code>JSON_DIAGNOSTICS</code> enabled.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <ol> <li>Linear in the number of members: as for <a href=../../ordered_json/ ><code>ordered_json</code></a>, members are compared one after another, in document order, stopping at the first match. Each compaLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// required fields of a service configuration -- at() reports a missing</span>
|
||||
<span class=w> </span><span class=c1>// or wrong-typed field with the very same exception basic_json::at()</span>
|
||||
<span class=w> </span><span class=c1>// would throw for the equivalent nlohmann::json value, so error</span>
|
||||
<span class=w> </span><span class=c1>// handling written against basic_json::at() keeps working unchanged</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>config</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"name": "cache", "port": "6379"}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>service</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>config</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>service</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"port"</span><span class=p>).</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=k>try</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// "port" is a string, not an array</span>
|
||||
<span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=kt>void</span><span class=o>></span><span class=p>(</span><span class=n>service</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"port"</span><span class=p>).</span><span class=n>at</span><span class=p>(</span><span class=mi>0</span><span class=p>));</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>catch</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>type_error</span><span class=o>&</span><span class=w> </span><span class=n>e</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
|
||||
<span class=w> </span><span class=k>try</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=kt>void</span><span class=o>></span><span class=p>(</span><span class=n>service</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"timeout"</span><span class=p>));</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>catch</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>out_of_range</span><span class=o>&</span><span class=w> </span><span class=n>e</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=s2>"6379"</span>
|
||||
<span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.</span><span class=kc>t</span><span class=err>ype_error.</span><span class=mi>304</span><span class=p>]</span><span class=w> </span><span class=err>ca</span><span class=kc>nn</span><span class=err>o</span><span class=kc>t</span><span class=w> </span><span class=err>use</span><span class=w> </span><span class=err>a</span><span class=kc>t</span><span class=err>()</span><span class=w> </span><span class=err>wi</span><span class=kc>t</span><span class=err>h</span><span class=w> </span><span class=err>s</span><span class=kc>tr</span><span class=err>i</span><span class=kc>n</span><span class=err>g</span>
|
||||
<span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.ou</span><span class=kc>t</span><span class=err>_o</span><span class=kc>f</span><span class=err>_ra</span><span class=kc>n</span><span class=err>ge.</span><span class=mi>403</span><span class=p>]</span><span class=w> </span><span class=err>key</span><span class=w> </span><span class=err>'</span><span class=kc>t</span><span class=err>imeou</span><span class=kc>t</span><span class=err>'</span><span class=w> </span><span class=kc>n</span><span class=err>o</span><span class=kc>t</span><span class=w> </span><span class=kc>f</span><span class=err>ou</span><span class=kc>n</span><span class=err>d</span>
|
||||
</code></pre></div> </details> <details class=example> <summary>Example: (3) access specified element via <abbr title="JavaScript Object Notation">JSON</abbr> pointer with bounds checking</summary> <p>The example below shows that <code>at</code> with a <abbr title="JavaScript Object Notation">JSON</abbr> pointer throws exactly the exceptions, with exactly the messages, that <a href=../../basic_json/at/ ><code>BasicJsonType::at</code></a> throws for the same pointer and the same document.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_pointer</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>json_pointer</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"region": "eu", "servers": ["eu-1", "eu-2"]}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>root</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>root</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=n>json_pointer</span><span class=p>(</span><span class=s>"/servers/1"</span><span class=p>)).</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// at() throws for every resolution failure -- with the very same</span>
|
||||
<span class=w> </span><span class=c1>// message json::at(ptr) would throw for the same pointer and the same</span>
|
||||
<span class=w> </span><span class=c1>// document</span>
|
||||
<span class=w> </span><span class=k>try</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=kt>void</span><span class=o>></span><span class=p>(</span><span class=n>root</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=n>json_pointer</span><span class=p>(</span><span class=s>"/servers/5"</span><span class=p>)));</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>catch</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>out_of_range</span><span class=o>&</span><span class=w> </span><span class=n>e</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
|
||||
<span class=w> </span><span class=k>try</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=kt>void</span><span class=o>></span><span class=p>(</span><span class=n>root</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=n>json_pointer</span><span class=p>(</span><span class=s>"/missing"</span><span class=p>)));</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>catch</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>out_of_range</span><span class=o>&</span><span class=w> </span><span class=n>e</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=s2>"eu-2"</span>
|
||||
<span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.ou</span><span class=kc>t</span><span class=err>_o</span><span class=kc>f</span><span class=err>_ra</span><span class=kc>n</span><span class=err>ge.</span><span class=mi>401</span><span class=p>]</span><span class=w> </span><span class=err>array</span><span class=w> </span><span class=err>i</span><span class=kc>n</span><span class=err>dex</span><span class=w> </span><span class=mi>5</span><span class=w> </span><span class=err>is</span><span class=w> </span><span class=err>ou</span><span class=kc>t</span><span class=w> </span><span class=err>o</span><span class=kc>f</span><span class=w> </span><span class=err>ra</span><span class=kc>n</span><span class=err>ge</span>
|
||||
<span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.ou</span><span class=kc>t</span><span class=err>_o</span><span class=kc>f</span><span class=err>_ra</span><span class=kc>n</span><span class=err>ge.</span><span class=mi>403</span><span class=p>]</span><span class=w> </span><span class=err>key</span><span class=w> </span><span class=err>'missi</span><span class=kc>n</span><span class=err>g'</span><span class=w> </span><span class=kc>n</span><span class=err>o</span><span class=kc>t</span><span class=w> </span><span class=kc>f</span><span class=err>ou</span><span class=kc>n</span><span class=err>d</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../operator%5B%5D/ >operator[]</a> - access specified element (returns a discarded view instead of throwing)</li> <li><a href=../front/ >front</a>, <a href=../back/ >back</a> - access the first or last element</li> <li><a href=../../basic_json/at/ ><code>BasicJsonType::at</code></a> - the corresponding function of <code>basic_json</code></li> <li><a href=../../json_pointer/ ><code>json_pointer</code></a> - <abbr title="JavaScript Object Notation">JSON</abbr> pointer type used by overload 3</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:28 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77Line truncated
|
||||
@@ -0,0 +1,183 @@
|
||||
# nlohmann::basic_json_view::at
|
||||
|
||||
```
|
||||
// (1)
|
||||
basic_json_view at(string_view_t key) const;
|
||||
basic_json_view at(const char* key) const;
|
||||
basic_json_view at(const string_t& key) const;
|
||||
|
||||
// (2)
|
||||
basic_json_view at(size_type idx) const;
|
||||
template<typename IntegerType>
|
||||
basic_json_view at(IntegerType idx) const;
|
||||
|
||||
// (3)
|
||||
basic_json_view at(const json_pointer& ptr) const;
|
||||
```
|
||||
|
||||
1. Returns the value of the object member with key `key` -- the first one, should the key occur more than once (see [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes)).
|
||||
1. Returns the array element at index `idx`. The template accepts every integer type except `bool` and `std::size_t` and forwards to the `size_type` overload, as for [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md); a negative `idx` is out of range.
|
||||
1. Returns the value a JSON pointer `ptr` refers to, starting at this value.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in) : object key of the element to access
|
||||
|
||||
`idx` (in) : index of the element to access
|
||||
|
||||
`ptr` (in) : JSON pointer to the element to access
|
||||
|
||||
## Return value
|
||||
|
||||
1. the value of the first member with key `key`
|
||||
1. the element at index `idx`
|
||||
1. the value `ptr` resolves to, starting at this value
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
1. The function can throw the following exceptions, both with the same message as the corresponding call to [`BasicJsonType::at`](https://json.nlohmann.me/api/basic_json/at/index.md):
|
||||
- Throws [`type_error.304`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error304) if the value is not an object.
|
||||
- Throws [`out_of_range.403`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range403) if no member has key `key`.
|
||||
1. The function can throw the following exceptions, both with the same message as the corresponding call to [`BasicJsonType::at`](https://json.nlohmann.me/api/basic_json/at/index.md):
|
||||
- Throws [`type_error.304`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error304) if the value is not an array.
|
||||
- Throws [`out_of_range.401`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range401) if `idx >= size()`.
|
||||
1. The function can throw the following exceptions, all with the same message as the corresponding call to [`BasicJsonType::at`](https://json.nlohmann.me/api/basic_json/at/index.md):
|
||||
- Throws [`parse_error.106`](https://json.nlohmann.me/home/exceptions/#jsonexceptionparse_error106) if an array index in `ptr` begins with `'0'`.
|
||||
- Throws [`parse_error.109`](https://json.nlohmann.me/home/exceptions/#jsonexceptionparse_error109) if an array index in `ptr` is not a number.
|
||||
- Throws [`out_of_range.401`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range401) if an array index in `ptr` is out of range.
|
||||
- Throws [`out_of_range.402`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range402) if a reference token is `"-"` at an array -- `at` never inserts an element, so `"-"` is always invalid.
|
||||
- Throws [`out_of_range.403`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range403) if a reference token names an object member that does not exist.
|
||||
- Throws [`out_of_range.404`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range404) if `ptr` cannot be resolved because a reference token is used on a primitive value.
|
||||
|
||||
None of these exceptions carry a [`JSON_DIAGNOSTICS`](https://json.nlohmann.me/api/macros/json_diagnostics/index.md) path: the view has no `BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with `JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
1. Linear in the number of members: as for [`ordered_json`](https://json.nlohmann.me/api/ordered_json/index.md), members are compared one after another, in document order, stopping at the first match. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes constant time on average.
|
||||
1. Linear in `idx`: elements are skipped one at a time from the first one, since they are not a fixed size in the index (unlike `BasicJsonType`'s array, which is random-access).
|
||||
1. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at that level (as 1.) or the index into the array (as 2.).
|
||||
|
||||
## Notes
|
||||
|
||||
Duplicate keys: the first member wins
|
||||
|
||||
If the source text repeats a key, this resolves to the *first* member with it, not to the last one that [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) and `parse()` keep. See the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes) of `operator[]` and [Duplicate keys](https://json.nlohmann.me/features/json_view/#duplicate-keys) for the reasons and for how to get the last value.
|
||||
|
||||
Unlike [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md), which returns a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view for a missing key or an out-of-range index, `at` always throws -- exactly as `BasicJsonType::at` does, and with the same messages, so existing error handling written against `BasicJsonType::at` keeps working unchanged when switched to a view. This also holds for overload 3: unlike [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) with a JSON pointer, which returns a discarded view for a missing key or an out-of-range index, `at` throws for those too (`out_of_range.403`/`out_of_range.401`).
|
||||
|
||||
## Examples
|
||||
|
||||
Example: (1)/(2) access specified element with bounds checking
|
||||
|
||||
The example below reads required fields out of a service configuration with `at`, and shows that the exceptions it throws -- for a wrong type and for a missing key -- carry the same messages [`BasicJsonType::at`](https://json.nlohmann.me/api/basic_json/at/index.md) would produce for the same JSON text.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// required fields of a service configuration -- at() reports a missing
|
||||
// or wrong-typed field with the very same exception basic_json::at()
|
||||
// would throw for the equivalent nlohmann::json value, so error
|
||||
// handling written against basic_json::at() keeps working unchanged
|
||||
json_document config = json_document::parse(R"({"name": "cache", "port": "6379"})");
|
||||
const json_view service = config.root();
|
||||
|
||||
std::cout << service.at("port").materialize().dump() << '\n';
|
||||
|
||||
try
|
||||
{
|
||||
// "port" is a string, not an array
|
||||
static_cast<void>(service.at("port").at(0));
|
||||
}
|
||||
catch (const nlohmann::json::type_error& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
static_cast<void>(service.at("timeout"));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
"6379"
|
||||
[json.exception.type_error.304] cannot use at() with string
|
||||
[json.exception.out_of_range.403] key 'timeout' not found
|
||||
```
|
||||
|
||||
Example: (3) access specified element via JSON pointer with bounds checking
|
||||
|
||||
The example below shows that `at` with a JSON pointer throws exactly the exceptions, with exactly the messages, that [`BasicJsonType::at`](https://json.nlohmann.me/api/basic_json/at/index.md) throws for the same pointer and the same document.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"region": "eu", "servers": ["eu-1", "eu-2"]})");
|
||||
const auto root = doc.root();
|
||||
|
||||
std::cout << root.at(json_pointer("/servers/1")).materialize().dump() << '\n';
|
||||
|
||||
// at() throws for every resolution failure -- with the very same
|
||||
// message json::at(ptr) would throw for the same pointer and the same
|
||||
// document
|
||||
try
|
||||
{
|
||||
static_cast<void>(root.at(json_pointer("/servers/5")));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
static_cast<void>(root.at(json_pointer("/missing")));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
"eu-2"
|
||||
[json.exception.out_of_range.401] array index 5 is out of range
|
||||
[json.exception.out_of_range.403] key 'missing' not found
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [operator[]](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) - access specified element (returns a discarded view instead of throwing)
|
||||
- [front](https://json.nlohmann.me/api/basic_json_view/front/index.md), [back](https://json.nlohmann.me/api/basic_json_view/back/index.md) - access the first or last element
|
||||
- [`BasicJsonType::at`](https://json.nlohmann.me/api/basic_json/at/index.md) - the corresponding function of `basic_json`
|
||||
- [`json_pointer`](https://json.nlohmann.me/api/json_pointer/index.md) - JSON pointer type used by overload 3
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,90 @@
|
||||
# <small>nlohmann::basic_json_view::</small>back
|
||||
|
||||
```cpp
|
||||
basic_json_view back() const;
|
||||
```
|
||||
|
||||
Returns the last element of an array, the last member value of an object, or the value itself if it is primitive
|
||||
(as for [`BasicJsonType::back()`](../basic_json/back.md), a primitive value is a range of one element).
|
||||
|
||||
## Return value
|
||||
|
||||
The last element or member value. For a primitive value (number, string, boolean), the value itself.
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`invalid_iterator.214`](../../home/exceptions.md#jsonexceptioninvalid_iterator214) if the view is
|
||||
[null](is_null.md) or [discarded](is_discarded.md), or if it is an empty array or object.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no
|
||||
`BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with
|
||||
`JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the size of the array or object: unlike [`front()`](front.md), which only ever looks at the first
|
||||
element, `back()` has to walk every element to find where they end, since elements are not a fixed size in the
|
||||
index.
|
||||
|
||||
## Notes
|
||||
|
||||
Unlike [`BasicJsonType::back()`](../basic_json/back.md), which has undefined behavior for an empty array or object,
|
||||
`back()` throws `invalid_iterator.214` for an empty array, an empty object, a null value, or a discarded view.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below reads only the final status of a build log with `back()`. Even though `back()` is linear in
|
||||
the number of events (unlike [`front()`](front.md), which is constant), it still avoids building a
|
||||
`BasicJsonType` value for the events that are not needed.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// the same build log; back() reads only the final status. It is linear
|
||||
// in the number of events (unlike front(), which is constant), but
|
||||
// still far less work than materializing the whole array
|
||||
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
|
||||
std::cout << log.root().back().materialize().dump() << '\n';
|
||||
|
||||
// an empty log -- back() throws instead of the undefined behavior
|
||||
// basic_json::back() has for an empty array
|
||||
json_document empty_log = json_document::parse("[]");
|
||||
try
|
||||
{
|
||||
static_cast<void>(empty_log.root().back());
|
||||
}
|
||||
catch (const nlohmann::json::invalid_iterator& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
"done"
|
||||
[json.exception.invalid_iterator.214] cannot get value
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [front](front.md) - access the first element
|
||||
- [`BasicJsonType::back`](../basic_json/back.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,29 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/back/" rel="canonical"><link href=../at/ rel=prev><link href=../begin/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>back - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewback class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> back </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <divLine truncated
|
||||
</code></pre></div> <p>Returns the last element of an array, the last member value of an object, or the value itself if it is primitive (as for <a href=../../basic_json/back/ ><code>BasicJsonType::back()</code></a>, a primitive value is a range of one element).</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>The last element or member value. For a primitive value (number, string, boolean), the value itself.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: there are no changes to the view or the document it refers to.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>Throws <a href=../../../home/exceptions/#jsonexceptioninvalid_iterator214><code>invalid_iterator.214</code></a> if the view is <a href=../is_null/ >null</a> or <a href=../is_discarded/ >discarded</a>, or if it is an empty array or object.</p> <p>This exception does not carry a <a href=../../macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> path: the view has no <code>BasicJsonType</code> value to point at, so the exception is created without one, even if <code>BasicJsonType</code> was built with <code>JSON_DIAGNOSTICS</code> enabled.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the array or object: unlike <a href=../front/ ><code>front()</code></a>, which only ever looks at the first element, <code>back()</code> has to walk every element to find where they end, since elements are not a fixed size in the index.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>Unlike <a href=../../basic_json/back/ ><code>BasicJsonType::back()</code></a>, which has undefined behavior for an empty array or object, <code>back()</code> throws <code>invalid_iterator.214</code> for an empty array, an empty object, a null value, or a discarded view.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below reads only the final status of a build log with <code>back()</code>. Even though <code>back()</code> is linear in the number of events (unlike <a href=../front/ ><code>front()</code></a>, which is constant), it still avoids building a <code>BasicJsonType</code> value for the events that are not needed.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// the same build log; back() reads only the final status. It is linear</span>
|
||||
<span class=w> </span><span class=c1>// in the number of events (unlike front(), which is constant), but</span>
|
||||
<span class=w> </span><span class=c1>// still far less work than materializing the whole array</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>log</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>["queued", "started", "compiling", "linking", "done"]</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>log</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>back</span><span class=p>().</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// an empty log -- back() throws instead of the undefined behavior</span>
|
||||
<span class=w> </span><span class=c1>// basic_json::back() has for an empty array</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>empty_log</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>try</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=kt>void</span><span class=o>></span><span class=p>(</span><span class=n>empty_log</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>back</span><span class=p>());</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>catch</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>invalid_iterator</span><span class=o>&</span><span class=w> </span><span class=n>e</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=s2>"done"</span>
|
||||
<span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.i</span><span class=kc>n</span><span class=err>valid_i</span><span class=kc>terat</span><span class=err>or.</span><span class=mi>214</span><span class=p>]</span><span class=w> </span><span class=err>ca</span><span class=kc>nn</span><span class=err>o</span><span class=kc>t</span><span class=w> </span><span class=err>ge</span><span class=kc>t</span><span class=w> </span><span class=err>value</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../front/ >front</a> - access the first element</li> <li><a href=../../basic_json/back/ ><code>BasicJsonType::back</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-sociaLine truncated
|
||||
@@ -0,0 +1,79 @@
|
||||
# nlohmann::basic_json_view::back
|
||||
|
||||
```
|
||||
basic_json_view back() const;
|
||||
```
|
||||
|
||||
Returns the last element of an array, the last member value of an object, or the value itself if it is primitive (as for [`BasicJsonType::back()`](https://json.nlohmann.me/api/basic_json/back/index.md), a primitive value is a range of one element).
|
||||
|
||||
## Return value
|
||||
|
||||
The last element or member value. For a primitive value (number, string, boolean), the value itself.
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`invalid_iterator.214`](https://json.nlohmann.me/home/exceptions/#jsonexceptioninvalid_iterator214) if the view is [null](https://json.nlohmann.me/api/basic_json_view/is_null/index.md) or [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md), or if it is an empty array or object.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](https://json.nlohmann.me/api/macros/json_diagnostics/index.md) path: the view has no `BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with `JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the size of the array or object: unlike [`front()`](https://json.nlohmann.me/api/basic_json_view/front/index.md), which only ever looks at the first element, `back()` has to walk every element to find where they end, since elements are not a fixed size in the index.
|
||||
|
||||
## Notes
|
||||
|
||||
Unlike [`BasicJsonType::back()`](https://json.nlohmann.me/api/basic_json/back/index.md), which has undefined behavior for an empty array or object, `back()` throws `invalid_iterator.214` for an empty array, an empty object, a null value, or a discarded view.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below reads only the final status of a build log with `back()`. Even though `back()` is linear in the number of events (unlike [`front()`](https://json.nlohmann.me/api/basic_json_view/front/index.md), which is constant), it still avoids building a `BasicJsonType` value for the events that are not needed.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// the same build log; back() reads only the final status. It is linear
|
||||
// in the number of events (unlike front(), which is constant), but
|
||||
// still far less work than materializing the whole array
|
||||
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
|
||||
std::cout << log.root().back().materialize().dump() << '\n';
|
||||
|
||||
// an empty log -- back() throws instead of the undefined behavior
|
||||
// basic_json::back() has for an empty array
|
||||
json_document empty_log = json_document::parse("[]");
|
||||
try
|
||||
{
|
||||
static_cast<void>(empty_log.root().back());
|
||||
}
|
||||
catch (const nlohmann::json::invalid_iterator& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
"done"
|
||||
[json.exception.invalid_iterator.214] cannot get value
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [front](https://json.nlohmann.me/api/basic_json_view/front/index.md) - access the first element
|
||||
- [`BasicJsonType::back`](https://json.nlohmann.me/api/basic_json/back/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,70 @@
|
||||
# <small>nlohmann::basic_json_view::</small>basic_json_view
|
||||
|
||||
```cpp
|
||||
basic_json_view() noexcept = default;
|
||||
```
|
||||
|
||||
Creates an invalid (discarded) view: [`type()`](type.md) is `#!cpp value_t::discarded` and
|
||||
[`is_discarded()`](is_discarded.md) is `#!cpp true`.
|
||||
|
||||
This is the only constructor a caller can use directly. Every other view is obtained from a
|
||||
[`basic_json_document`](../basic_json_document/index.md), via [`root()`](../basic_json_document/root.md) or by
|
||||
navigating into a container with [`operator[]`](operator[].md), [`at`](at.md), [`front`](front.md), [`back`](back.md),
|
||||
[`find`](find.md), or iteration.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this constructor never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`basic_json_view` is trivially copyable (it holds two pointers), so a default-constructed view can be used as a
|
||||
placeholder for "no value yet" and later be assigned a real view.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below shows the default constructor and that a `basic_json_view` is a small, copyable handle.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// the default constructor is the only public one: it creates an invalid
|
||||
// (discarded) view, useful as a "no value yet" placeholder
|
||||
nlohmann::json_view v;
|
||||
std::cout << v.is_discarded() << '\n';
|
||||
|
||||
// views are trivially copyable handles (two pointers); the document owns
|
||||
// the actual data
|
||||
nlohmann::json_view copy = v;
|
||||
std::cout << copy.is_discarded() << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_discarded](is_discarded.md) - return whether the view is invalid
|
||||
- [root](../basic_json_document/root.md) - the view of a document's root value
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,21 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/basic_json_view/" rel="canonical"><link href=../ rel=prev><link href=../at/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>(Constructor) - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewbasic_json_view class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> (Constructor) </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5Line truncated
|
||||
</code></pre></div> <p>Creates an invalid (discarded) view: <a href=../type/ ><code>type()</code></a> is <code class=highlight><span class=n>value_t</span><span class=o>::</span><span class=n>discarded</span></code> and <a href=../is_discarded/ ><code>is_discarded()</code></a> is <code class=highlight><span class=nb>true</span></code>.</p> <p>This is the only constructor a caller can use directly. Every other view is obtained from a <a href=../../basic_json_document/ ><code>basic_json_document</code></a>, via <a href=../../basic_json_document/root/ ><code>root()</code></a> or by navigating into a container with <a href=../operator%5B%5D/ ><code>operator[]</code></a>, <a href=../at/ ><code>at</code></a>, <a href=../front/ ><code>front</code></a>, <a href=../back/ ><code>back</code></a>, <a href=../find/ ><code>find</code></a>, or iteration.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this constructor never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p><code>basic_json_view</code> is trivially copyable (it holds two pointers), so a default-constructed view can be used as a placeholder for "no value yet" and later be assigned a real view.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below shows the default constructor and that a <code>basic_json_view</code> is a small, copyable handle.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// the default constructor is the only public one: it creates an invalid</span>
|
||||
<span class=w> </span><span class=c1>// (discarded) view, useful as a "no value yet" placeholder</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>v</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>v</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// views are trivially copyable handles (two pointers); the document owns</span>
|
||||
<span class=w> </span><span class=c1>// the actual data</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>copy</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>v</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>copy</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_discarded/ >is_discarded</a> - return whether the view is invalid</li> <li><a href=../../basic_json_document/root/ >root</a> - the view of a document's root value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svLine truncated
|
||||
@@ -0,0 +1,63 @@
|
||||
# nlohmann::basic_json_view::basic_json_view
|
||||
|
||||
```
|
||||
basic_json_view() noexcept = default;
|
||||
```
|
||||
|
||||
Creates an invalid (discarded) view: [`type()`](https://json.nlohmann.me/api/basic_json_view/type/index.md) is `value_t::discarded` and [`is_discarded()`](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) is `true`.
|
||||
|
||||
This is the only constructor a caller can use directly. Every other view is obtained from a [`basic_json_document`](https://json.nlohmann.me/api/basic_json_document/index.md), via [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md) or by navigating into a container with [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md), [`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md), [`front`](https://json.nlohmann.me/api/basic_json_view/front/index.md), [`back`](https://json.nlohmann.me/api/basic_json_view/back/index.md), [`find`](https://json.nlohmann.me/api/basic_json_view/find/index.md), or iteration.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this constructor never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`basic_json_view` is trivially copyable (it holds two pointers), so a default-constructed view can be used as a placeholder for "no value yet" and later be assigned a real view.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below shows the default constructor and that a `basic_json_view` is a small, copyable handle.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// the default constructor is the only public one: it creates an invalid
|
||||
// (discarded) view, useful as a "no value yet" placeholder
|
||||
nlohmann::json_view v;
|
||||
std::cout << v.is_discarded() << '\n';
|
||||
|
||||
// views are trivially copyable handles (two pointers); the document owns
|
||||
// the actual data
|
||||
nlohmann::json_view copy = v;
|
||||
std::cout << copy.is_discarded() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) - return whether the view is invalid
|
||||
- [root](https://json.nlohmann.me/api/basic_json_document/root/index.md) - the view of a document's root value
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,86 @@
|
||||
# <small>nlohmann::basic_json_view::</small>begin
|
||||
|
||||
```cpp
|
||||
iterator begin() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to the first element of an array, or the first member value of an object, in **document order**
|
||||
-- the order the values appear in the source text, not sorted by key. A primitive value iterates as a range of one
|
||||
element (itself), and a `#!json null` value or a [discarded](is_discarded.md) view iterates as an empty range.
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator to the first element.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
For an object, iteration visits **every** member, including all occurrences of a duplicate key -- unlike
|
||||
[`operator[]`](operator[].md), [`at`](at.md), [`find`](find.md), and [`contains`](contains.md),
|
||||
which all resolve to the *first* member with a given key. See the
|
||||
[Notes on duplicate keys](operator[].md#notes) of `operator[]`.
|
||||
|
||||
Because objects are iterated in document order rather than sorted by key, the order seen here can differ from what
|
||||
iterating the [`materialize()`](materialize.md)d `BasicJsonType` value would produce: a `#!cpp basic_json` object
|
||||
(`std::map`-backed by default) sorts its keys, while a view does not.
|
||||
|
||||
An `ordered_json` value also keeps document order, but it keeps only one entry per key: as `parse()` does, it keeps the
|
||||
*last* value, at the position of the key's *first* occurrence. The view, in contrast, visits every member, including
|
||||
the duplicates.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below iterates a log record's members with `begin()`/[`end()`](end.md) and prints them in the order
|
||||
they were written. Materializing the record into a `BasicJsonType` object and iterating that would instead print
|
||||
the members sorted by key.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a log record: the fields matter in the order they were written, e.g.
|
||||
// to reproduce the record as it was logged. A nlohmann::json object
|
||||
// sorts its keys, so materializing and iterating it would instead print
|
||||
// them alphabetically ("level", "message", "time")
|
||||
json_document record = json_document::parse(R"({"time": "10:00:01", "level": "info", "message": "started"})");
|
||||
|
||||
for (auto it = record.root().begin(); it != record.root().end(); ++it)
|
||||
{
|
||||
std::cout << it.key() << '=' << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
time="10:00:01"
|
||||
level="info"
|
||||
message="started"
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [end](end.md) - returns an iterator to one past the last element
|
||||
- [cbegin](cbegin.md) - returns a const iterator to the first element
|
||||
- [items](items.md) - access iterator member functions in range-based for
|
||||
- [`BasicJsonType::begin`](../basic_json/begin.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,23 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/begin/" rel="canonical"><link href=../back/ rel=prev><link href=../cbegin/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>begin - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewbegin class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> begin </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </labeLine truncated
|
||||
</code></pre></div> <p>Returns an iterator to the first element of an array, or the first member value of an object, in <strong>document order</strong> -- the order the values appear in the source text, not sorted by key. A primitive value iterates as a range of one element (itself), and a <code class=highlight><span class=kc>null</span></code> value or a <a href=../is_discarded/ >discarded</a> view iterates as an empty range.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>Iterator to the first element.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>For an object, iteration visits <strong>every</strong> member, including all occurrences of a duplicate key -- unlike <a href=../operator%5B%5D/ ><code>operator[]</code></a>, <a href=../at/ ><code>at</code></a>, <a href=../find/ ><code>find</code></a>, and <a href=../contains/ ><code>contains</code></a>, which all resolve to the <em>first</em> member with a given key. See the <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a> of <code>operator[]</code>.</p> <p>Because objects are iterated in document order rather than sorted by key, the order seen here can differ from what iterating the <a href=../materialize/ ><code>materialize()</code></a>d <code>BasicJsonType</code> value would produce: a <code class=highlight><span class=n>basic_json</span></code> object (<code>std::map</code>-backed by default) sorts its keys, while a view does not.</p> <p>An <code>ordered_json</code> value also keeps document order, but it keeps only one entry per key: as <code>parse()</code> does, it keeps the <em>last</em> value, at the position of the key's <em>first</em> occurrence. The view, in contrast, visits every member, including the duplicates.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below iterates a log record's members with <code>begin()</code>/<a href=../end/ ><code>end()</code></a> and prints them in the order they were written. Materializing the record into a <code>BasicJsonType</code> object and iterating that would instead print the members sorted by key.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a log record: the fields matter in the order they were written, e.g.</span>
|
||||
<span class=w> </span><span class=c1>// to reproduce the record as it was logged. A nlohmann::json object</span>
|
||||
<span class=w> </span><span class=c1>// sorts its keys, so materializing and iterating it would instead print</span>
|
||||
<span class=w> </span><span class=c1>// them alphabetically ("level", "message", "time")</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>record</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"time": "10:00:01", "level": "info", "message": "started"}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>auto</span><span class=w> </span><span class=n>it</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>record</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>begin</span><span class=p>();</span><span class=w> </span><span class=n>it</span><span class=w> </span><span class=o>!=</span><span class=w> </span><span class=n>record</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>end</span><span class=p>();</span><span class=w> </span><span class=o>++</span><span class=n>it</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>it</span><span class=p>.</span><span class=n>key</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'='</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>it</span><span class=o>-></span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>t</span><span class=err>ime=</span><span class=s2>"10:00:01"</span>
|
||||
<span class=err>level=</span><span class=s2>"info"</span>
|
||||
<span class=err>message=</span><span class=s2>"started"</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../end/ >end</a> - returns an iterator to one past the last element</li> <li><a href=../cbegin/ >cbegin</a> - returns a const iterator to the first element</li> <li><a href=../items/ >items</a> - access iterator member functions in range-based for</li> <li><a href=../../basic_json/begin/ ><code>BasicJsonType::begin</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.Line truncated
|
||||
@@ -0,0 +1,73 @@
|
||||
# nlohmann::basic_json_view::begin
|
||||
|
||||
```
|
||||
iterator begin() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to the first element of an array, or the first member value of an object, in **document order** -- the order the values appear in the source text, not sorted by key. A primitive value iterates as a range of one element (itself), and a `null` value or a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view iterates as an empty range.
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator to the first element.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
For an object, iteration visits **every** member, including all occurrences of a duplicate key -- unlike [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md), [`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md), [`find`](https://json.nlohmann.me/api/basic_json_view/find/index.md), and [`contains`](https://json.nlohmann.me/api/basic_json_view/contains/index.md), which all resolve to the *first* member with a given key. See the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes) of `operator[]`.
|
||||
|
||||
Because objects are iterated in document order rather than sorted by key, the order seen here can differ from what iterating the [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md)d `BasicJsonType` value would produce: a `basic_json` object (`std::map`-backed by default) sorts its keys, while a view does not.
|
||||
|
||||
An `ordered_json` value also keeps document order, but it keeps only one entry per key: as `parse()` does, it keeps the *last* value, at the position of the key's *first* occurrence. The view, in contrast, visits every member, including the duplicates.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below iterates a log record's members with `begin()`/[`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md) and prints them in the order they were written. Materializing the record into a `BasicJsonType` object and iterating that would instead print the members sorted by key.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a log record: the fields matter in the order they were written, e.g.
|
||||
// to reproduce the record as it was logged. A nlohmann::json object
|
||||
// sorts its keys, so materializing and iterating it would instead print
|
||||
// them alphabetically ("level", "message", "time")
|
||||
json_document record = json_document::parse(R"({"time": "10:00:01", "level": "info", "message": "started"})");
|
||||
|
||||
for (auto it = record.root().begin(); it != record.root().end(); ++it)
|
||||
{
|
||||
std::cout << it.key() << '=' << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
time="10:00:01"
|
||||
level="info"
|
||||
message="started"
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [end](https://json.nlohmann.me/api/basic_json_view/end/index.md) - returns an iterator to one past the last element
|
||||
- [cbegin](https://json.nlohmann.me/api/basic_json_view/cbegin/index.md) - returns a const iterator to the first element
|
||||
- [items](https://json.nlohmann.me/api/basic_json_view/items/index.md) - access iterator member functions in range-based for
|
||||
- [`BasicJsonType::begin`](https://json.nlohmann.me/api/basic_json/begin/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,74 @@
|
||||
# <small>nlohmann::basic_json_view::</small>cbegin
|
||||
|
||||
```cpp
|
||||
iterator cbegin() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to the first element, in [document order](begin.md). Equivalent to [`begin()`](begin.md): a view
|
||||
is always read-only, so `#!cpp const_iterator` and `#!cpp iterator` are the same type, and `cbegin()` exists only so
|
||||
that generic code that expects a `cbegin()`/`cend()` pair works with a view too.
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator to the first element; identical to what [`begin()`](begin.md) returns.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below sums many measurements with `std::accumulate`, using `cbegin()`/[`cend()`](cend.md) as the
|
||||
range -- the same way it would for any standard container -- without ever materializing the whole array into a
|
||||
`BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// sum many measurements with std::accumulate; cbegin()/cend() (identical
|
||||
// to begin()/end() here -- the view is always read-only) let the view be
|
||||
// used with standard algorithms without ever materializing the whole
|
||||
// array into a nlohmann::json value
|
||||
json_document measurements = json_document::parse("[3, 1, 4, 1, 5, 9, 2, 6]");
|
||||
const auto values = measurements.root();
|
||||
|
||||
const int sum = std::accumulate(values.cbegin(), values.cend(), 0,
|
||||
[](int total, const json_view & v)
|
||||
{
|
||||
return total + v.materialize().get<int>();
|
||||
});
|
||||
|
||||
std::cout << sum << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
31
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [begin](begin.md) - returns an iterator to the first element
|
||||
- [cend](cend.md) - returns a const iterator to one past the last element
|
||||
- [`BasicJsonType::cbegin`](../basic_json/cbegin.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,27 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/cbegin/" rel="canonical"><link href=../begin/ rel=prev><link href=../cend/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>cbegin - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewcbegin class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> cbegin </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </lLine truncated
|
||||
</code></pre></div> <p>Returns an iterator to the first element, in <a href=../begin/ >document order</a>. Equivalent to <a href=../begin/ ><code>begin()</code></a>: a view is always read-only, so <code class=highlight><span class=n>const_iterator</span></code> and <code class=highlight><span class=n>iterator</span></code> are the same type, and <code>cbegin()</code> exists only so that generic code that expects a <code>cbegin()</code>/<code>cend()</code> pair works with a view too.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>Iterator to the first element; identical to what <a href=../begin/ ><code>begin()</code></a> returns.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below sums many measurements with <code>std::accumulate</code>, using <code>cbegin()</code>/<a href=../cend/ ><code>cend()</code></a> as the range -- the same way it would for any standard container -- without ever materializing the whole array into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><numeric></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// sum many measurements with std::accumulate; cbegin()/cend() (identical</span>
|
||||
<span class=w> </span><span class=c1>// to begin()/end() here -- the view is always read-only) let the view be</span>
|
||||
<span class=w> </span><span class=c1>// used with standard algorithms without ever materializing the whole</span>
|
||||
<span class=w> </span><span class=c1>// array into a nlohmann::json value</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>measurements</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[3, 1, 4, 1, 5, 9, 2, 6]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>values</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>measurements</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>int</span><span class=w> </span><span class=n>sum</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>accumulate</span><span class=p>(</span><span class=n>values</span><span class=p>.</span><span class=n>cbegin</span><span class=p>(),</span><span class=w> </span><span class=n>values</span><span class=p>.</span><span class=n>cend</span><span class=p>(),</span><span class=w> </span><span class=mi>0</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=p>[](</span><span class=kt>int</span><span class=w> </span><span class=n>total</span><span class=p>,</span><span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>&</span><span class=w> </span><span class=n>v</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>return</span><span class=w> </span><span class=n>total</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=n>v</span><span class=p>.</span><span class=n>materialize</span><span class=p>().</span><span class=n>get</span><span class=o><</span><span class=kt>int</span><span class=o>></span><span class=p>();</span>
|
||||
<span class=w> </span><span class=p>});</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>sum</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=mi>31</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../begin/ >begin</a> - returns an iterator to the first element</li> <li><a href=../cend/ >cend</a> - returns a const iterator to one past the last element</li> <li><a href=../../basic_json/cbegin/ ><code>BasicJsonType::cbegin</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href=Line truncated
|
||||
@@ -0,0 +1,68 @@
|
||||
# nlohmann::basic_json_view::cbegin
|
||||
|
||||
```
|
||||
iterator cbegin() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to the first element, in [document order](https://json.nlohmann.me/api/basic_json_view/begin/index.md). Equivalent to [`begin()`](https://json.nlohmann.me/api/basic_json_view/begin/index.md): a view is always read-only, so `const_iterator` and `iterator` are the same type, and `cbegin()` exists only so that generic code that expects a `cbegin()`/`cend()` pair works with a view too.
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator to the first element; identical to what [`begin()`](https://json.nlohmann.me/api/basic_json_view/begin/index.md) returns.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below sums many measurements with `std::accumulate`, using `cbegin()`/[`cend()`](https://json.nlohmann.me/api/basic_json_view/cend/index.md) as the range -- the same way it would for any standard container -- without ever materializing the whole array into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// sum many measurements with std::accumulate; cbegin()/cend() (identical
|
||||
// to begin()/end() here -- the view is always read-only) let the view be
|
||||
// used with standard algorithms without ever materializing the whole
|
||||
// array into a nlohmann::json value
|
||||
json_document measurements = json_document::parse("[3, 1, 4, 1, 5, 9, 2, 6]");
|
||||
const auto values = measurements.root();
|
||||
|
||||
const int sum = std::accumulate(values.cbegin(), values.cend(), 0,
|
||||
[](int total, const json_view & v)
|
||||
{
|
||||
return total + v.materialize().get<int>();
|
||||
});
|
||||
|
||||
std::cout << sum << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
31
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [begin](https://json.nlohmann.me/api/basic_json_view/begin/index.md) - returns an iterator to the first element
|
||||
- [cend](https://json.nlohmann.me/api/basic_json_view/cend/index.md) - returns a const iterator to one past the last element
|
||||
- [`BasicJsonType::cbegin`](https://json.nlohmann.me/api/basic_json/cbegin/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,74 @@
|
||||
# <small>nlohmann::basic_json_view::</small>cend
|
||||
|
||||
```cpp
|
||||
iterator cend() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to one past the last element, in [document order](begin.md). Equivalent to [`end()`](end.md): a
|
||||
view is always read-only, so `#!cpp const_iterator` and `#!cpp iterator` are the same type, and `cend()` exists only
|
||||
so that generic code that expects a `cbegin()`/`cend()` pair works with a view too.
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator one past the last element; identical to what [`end()`](end.md) returns.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below checks that every record of a batch is an object with `std::all_of`, using
|
||||
[`cbegin()`](cbegin.md)/`cend()` as the range -- the same way it would for any standard container -- before
|
||||
materializing any record of the batch.
|
||||
|
||||
```cpp
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// check that every element of a (possibly large) batch is an object,
|
||||
// before materializing any of them -- cbegin()/cend() (identical to
|
||||
// begin()/end() here) work as the range for std::all_of like they would
|
||||
// for any standard container
|
||||
json_document batch = json_document::parse(R"([{"id": 1}, {"id": 2}, {"id": 3}])");
|
||||
const auto records = batch.root();
|
||||
|
||||
const bool all_objects = std::all_of(records.cbegin(), records.cend(),
|
||||
[](const json_view & v)
|
||||
{
|
||||
return v.is_object();
|
||||
});
|
||||
|
||||
std::cout << std::boolalpha << all_objects << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [end](end.md) - returns an iterator to one past the last element
|
||||
- [cbegin](cbegin.md) - returns a const iterator to the first element
|
||||
- [`BasicJsonType::cend`](../basic_json/cend.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,27 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/cend/" rel="canonical"><link href=../cbegin/ rel=prev><link href=../contains/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>cend - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewcend class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> cend </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </labeLine truncated
|
||||
</code></pre></div> <p>Returns an iterator to one past the last element, in <a href=../begin/ >document order</a>. Equivalent to <a href=../end/ ><code>end()</code></a>: a view is always read-only, so <code class=highlight><span class=n>const_iterator</span></code> and <code class=highlight><span class=n>iterator</span></code> are the same type, and <code>cend()</code> exists only so that generic code that expects a <code>cbegin()</code>/<code>cend()</code> pair works with a view too.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>Iterator one past the last element; identical to what <a href=../end/ ><code>end()</code></a> returns.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below checks that every record of a batch is an object with <code>std::all_of</code>, using <a href=../cbegin/ ><code>cbegin()</code></a>/<code>cend()</code> as the range -- the same way it would for any standard container -- before materializing any record of the batch.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><algorithm></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// check that every element of a (possibly large) batch is an object,</span>
|
||||
<span class=w> </span><span class=c1>// before materializing any of them -- cbegin()/cend() (identical to</span>
|
||||
<span class=w> </span><span class=c1>// begin()/end() here) work as the range for std::all_of like they would</span>
|
||||
<span class=w> </span><span class=c1>// for any standard container</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>[{"id": 1}, {"id": 2}, {"id": 3}]</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>records</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>all_objects</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>all_of</span><span class=p>(</span><span class=n>records</span><span class=p>.</span><span class=n>cbegin</span><span class=p>(),</span><span class=w> </span><span class=n>records</span><span class=p>.</span><span class=n>cend</span><span class=p>(),</span>
|
||||
<span class=w> </span><span class=p>[](</span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>&</span><span class=w> </span><span class=n>v</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>return</span><span class=w> </span><span class=n>v</span><span class=p>.</span><span class=n>is_object</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=p>});</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>all_objects</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../end/ >end</a> - returns an iterator to one past the last element</li> <li><a href=../cbegin/ >cbegin</a> - returns a const iterator to the first element</li> <li><a href=../../basic_json/cend/ ><code>BasicJsonType::cend</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="httLine truncated
|
||||
@@ -0,0 +1,68 @@
|
||||
# nlohmann::basic_json_view::cend
|
||||
|
||||
```
|
||||
iterator cend() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to one past the last element, in [document order](https://json.nlohmann.me/api/basic_json_view/begin/index.md). Equivalent to [`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md): a view is always read-only, so `const_iterator` and `iterator` are the same type, and `cend()` exists only so that generic code that expects a `cbegin()`/`cend()` pair works with a view too.
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator one past the last element; identical to what [`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md) returns.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below checks that every record of a batch is an object with `std::all_of`, using [`cbegin()`](https://json.nlohmann.me/api/basic_json_view/cbegin/index.md)/`cend()` as the range -- the same way it would for any standard container -- before materializing any record of the batch.
|
||||
|
||||
```
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// check that every element of a (possibly large) batch is an object,
|
||||
// before materializing any of them -- cbegin()/cend() (identical to
|
||||
// begin()/end() here) work as the range for std::all_of like they would
|
||||
// for any standard container
|
||||
json_document batch = json_document::parse(R"([{"id": 1}, {"id": 2}, {"id": 3}])");
|
||||
const auto records = batch.root();
|
||||
|
||||
const bool all_objects = std::all_of(records.cbegin(), records.cend(),
|
||||
[](const json_view & v)
|
||||
{
|
||||
return v.is_object();
|
||||
});
|
||||
|
||||
std::cout << std::boolalpha << all_objects << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [end](https://json.nlohmann.me/api/basic_json_view/end/index.md) - returns an iterator to one past the last element
|
||||
- [cbegin](https://json.nlohmann.me/api/basic_json_view/cbegin/index.md) - returns a const iterator to the first element
|
||||
- [`BasicJsonType::cend`](https://json.nlohmann.me/api/basic_json/cend/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,185 @@
|
||||
# <small>nlohmann::basic_json_view::</small>contains
|
||||
|
||||
```cpp
|
||||
// (1)
|
||||
bool contains(string_view_t key) const;
|
||||
bool contains(const char* key) const;
|
||||
bool contains(const string_t& key) const;
|
||||
|
||||
// (2)
|
||||
bool contains(const json_pointer& ptr) const;
|
||||
```
|
||||
|
||||
1. Checks whether the value is an object with a member with key `key`.
|
||||
2. Checks whether a JSON pointer `ptr` can be resolved, starting at this value.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in)
|
||||
: key value to check its existence
|
||||
|
||||
`ptr` (in)
|
||||
: JSON pointer to check its existence
|
||||
|
||||
## Return value
|
||||
|
||||
1. `#!cpp true` if the value is an object and has a member with key `key`, `#!cpp false` otherwise
|
||||
2. `#!cpp true` if `ptr` can be resolved to a value starting at this view, `#!cpp false` otherwise
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
1. Linear in the number of members: as for [`ordered_json`](../ordered_json.md), members are compared one after
|
||||
another, in document order, stopping at the first match. Each comparison first checks the
|
||||
key's length -- already known from the index, without reading the key bytes -- before comparing its content.
|
||||
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
||||
constant time on average.
|
||||
2. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at
|
||||
that level or the index into the array -- as for [`operator[]`](operator[].md#complexity) and
|
||||
[`at`](at.md#complexity) with a JSON pointer.
|
||||
|
||||
## Notes
|
||||
|
||||
!!! warning "Duplicate keys: the first member wins"
|
||||
|
||||
If the source text repeats a key, this resolves to the *first* member with it, not to the last one that
|
||||
[`materialize()`](materialize.md) and `parse()` keep. See the [Notes on duplicate keys](operator[].md#notes) of
|
||||
`operator[]` and [Duplicate keys](../../features/json_view.md#duplicate-keys) for the reasons and for how to get
|
||||
the last value.
|
||||
|
||||
Overload 1 always returns `#!cpp false` when the value is not an object -- including a [discarded](is_discarded.md)
|
||||
view.
|
||||
|
||||
!!! info "Postconditions"
|
||||
|
||||
If `#!cpp v.contains(key)` returns `#!cpp true`, then `#!cpp v[key]` is not [discarded](is_discarded.md) and
|
||||
`#!cpp v.at(key)` does not throw. If
|
||||
`#!cpp v.contains(ptr)` returns `#!cpp true`, then `#!cpp v[ptr]` is not discarded and `#!cpp v.at(ptr)` does not
|
||||
throw.
|
||||
|
||||
!!! info "Overload 2 never throws"
|
||||
|
||||
Unlike [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), which can throw for certain
|
||||
malformed pointers (for instance an empty array-index reference token), overload 2 never throws: a missing key,
|
||||
an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a primitive all
|
||||
simply make it return `#!cpp false`.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example: (1) check with key"
|
||||
|
||||
The example below counts how many of a batch of records carry an optional `retry_of` field, using `contains()`
|
||||
to check without ever materializing a single record of the batch.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// count how many of many incoming records carry an optional "retry_of"
|
||||
// field -- contains() only walks the flat index, so scanning a large
|
||||
// batch like this never builds a single nlohmann::json value
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1},
|
||||
{"id": 2, "retry_of": 1},
|
||||
{"id": 3},
|
||||
{"id": 4, "retry_of": 3}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto records = batch.root();
|
||||
std::size_t retries = 0;
|
||||
for (std::size_t i = 0; i < records.size(); ++i)
|
||||
{
|
||||
if (records[i].contains("retry_of"))
|
||||
{
|
||||
++retries;
|
||||
}
|
||||
}
|
||||
std::cout << retries << " of " << records.size() << " records are retries\n";
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
2 of 4 records are retries
|
||||
|
||||
```
|
||||
|
||||
??? example "Example: (2) check with JSON pointer"
|
||||
|
||||
The example below checks an optional, nested field with a JSON pointer, and shows two pointers that
|
||||
`#!cpp contains()` resolves to `#!cpp false` without throwing.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
// "retry_of" is only present on some records, nested a level down
|
||||
// inside "meta"
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1, "meta": {}},
|
||||
{"id": 2, "meta": {"retry_of": 1}}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto records = batch.root();
|
||||
const json_pointer retry_of("/meta/retry_of");
|
||||
for (std::size_t i = 0; i < records.size(); ++i)
|
||||
{
|
||||
const auto record = records[i];
|
||||
if (record.contains(retry_of))
|
||||
{
|
||||
std::cout << "record " << i << " is a retry of " << record[retry_of].get<int>() << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "record " << i << " is original\n";
|
||||
}
|
||||
}
|
||||
|
||||
// contains() with a JSON pointer never throws -- not even for a
|
||||
// pointer that indexes into a primitive ("/0/id/x") or uses a
|
||||
// malformed array index ("/01"), either of which would need a
|
||||
// try/catch with json::contains(ptr)
|
||||
std::cout << std::boolalpha << records.contains(json_pointer("/0/id/x")) << '\n';
|
||||
std::cout << std::boolalpha << records.contains(json_pointer("/01")) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
record 0 is original
|
||||
record 1 is a retry of 1
|
||||
false
|
||||
false
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [find](find.md) - find a value in an object
|
||||
- [count](count.md) - returns the number of occurrences of a key
|
||||
- [at](at.md), [operator[]](operator[].md) - resolve a JSON pointer and throw, or return a discarded view
|
||||
- [`BasicJsonType::contains`](../basic_json/contains.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,82 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/contains/" rel="canonical"><link href=../cend/ rel=prev><link href=../count/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>contains - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewcontains class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> contains </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></Line truncated
|
||||
<span class=kt>bool</span><span class=w> </span><span class=nf>contains</span><span class=p>(</span><span class=n>string_view_t</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=kt>bool</span><span class=w> </span><span class=nf>contains</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=kt>bool</span><span class=w> </span><span class=nf>contains</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>string_t</span><span class=o>&</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
|
||||
<span class=c1>// (2)</span>
|
||||
<span class=kt>bool</span><span class=w> </span><span class=nf>contains</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>json_pointer</span><span class=o>&</span><span class=w> </span><span class=n>ptr</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <ol> <li>Checks whether the value is an object with a member with key <code>key</code>.</li> <li>Checks whether a <abbr title="JavaScript Object Notation">JSON</abbr> pointer <code>ptr</code> can be resolved, starting at this value.</li> </ol> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>key</code> (in)</dt> <dd>key value to check its existence</dd> <dt><code>ptr</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> pointer to check its existence</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><code class=highlight><span class=nb>true</span></code> if the value is an object and has a member with key <code>key</code>, <code class=highlight><span class=nb>false</span></code> otherwise</li> <li><code class=highlight><span class=nb>true</span></code> if <code>ptr</code> can be resolved to a value starting at this view, <code class=highlight><span class=nb>false</span></code> otherwise</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <ol> <li>Linear in the number of members: as for <a href=../../ordered_json/ ><code>ordered_json</code></a>, members are compared one after another, in document order, stopping at the first match. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes constant time on average.</li> <li>Linear in the number of reference tokens of <code>ptr</code> and, for each token, in the number of members of the object at that level or the index into the array -- as for <a href=../operator%5B%5D/#complexity><code>operator[]</code></a> and <a href=../at/#complexity><code>at</code></a> with a <abbr title="JavaScript Object Notation">JSON</abbr> pointer.</li> </ol> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <div class="admonition warning"> <p class=admonition-title>Duplicate keys: the first member wins</p> <p>If the source text repeats a key, this resolves to the <em>first</em> member with it, not to the last one that <a href=../materialize/ ><code>materialize()</code></a> and <code>parse()</code> keep. See the <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a> of <code>operator[]</code> and <a href=../../../features/json_view/#duplicate-keys>Duplicate keys</a> for the reasons and for how to get the last value.</p> </div> <p>Overload 1 always returns <code class=highlight><span class=nb>false</span></code> when the value is not an object -- including a <a href=../is_discarded/ >discarded</a> view.</p> <div class="admonition info"> <p class=admonition-title>Postconditions</p> <p>If <code class=highlight><span class=n>v</span><span class=p>.</span><span class=n>contains</span><span class=p>(</span><span class=n>key</span><span class=p>)</span></code> returns <code class=highlight><span class=nb>true</span></code>, then <code class=highlight><span class=n>v</span><span class=p>[</span><span class=n>key</span><span class=p>]</span></code> is not <a href=../is_discarded/ >discarded</a> and <code class=highlight><span class=n>v</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=n>key</span><span class=p>)</span></code> does not throw. If <code class=highlight><span class=n>v</span><span class=p>.</span><span class=n>contains</span><span class=p>(</span><span class=n>ptr</span><span class=p>)</span></code> returns <code class=highlight><span class=nb>true</span></code>, then <code class=highlight><span class=n>v</span><span class=p>[</span><span class=n>ptr</span><span class=p>]</span></code> is not discarded and <code class=highlight><span class=n>v</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=n>ptr</span><span class=p>)</span></code> does not throw.</p> </div> <div class="admonition info"> <p class=admonition-title>Overload 2 never throws</p> <p>Unlike <a href=../../basic_json/contains/ ><code>BasicJsonType::contains(const json_pointer&)</code></a>, which can throw for certain malformed pointers (for instance an empty array-index reference token), overload 2 never throws: a missing key, an out-of-range or malformed array index, a <code class=highlight><span class=s>"-"</span></code> index, or a reference token used on a primitive all simply make it return <code class=highlight><span class=nb>false</span></code>.</p> </div> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=examLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// count how many of many incoming records carry an optional "retry_of"</span>
|
||||
<span class=w> </span><span class=c1>// field -- contains() only walks the flat index, so scanning a large</span>
|
||||
<span class=w> </span><span class=c1>// batch like this never builds a single nlohmann::json value</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> [</span>
|
||||
<span class=s> {"id": 1},</span>
|
||||
<span class=s> {"id": 2, "retry_of": 1},</span>
|
||||
<span class=s> {"id": 3},</span>
|
||||
<span class=s> {"id": 4, "retry_of": 3}</span>
|
||||
<span class=s> ]</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>records</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=n>retries</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><</span><span class=w> </span><span class=n>records</span><span class=p>.</span><span class=n>size</span><span class=p>();</span><span class=w> </span><span class=o>++</span><span class=n>i</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>records</span><span class=p>[</span><span class=n>i</span><span class=p>].</span><span class=n>contains</span><span class=p>(</span><span class=s>"retry_of"</span><span class=p>))</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=o>++</span><span class=n>retries</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>retries</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" of "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>records</span><span class=p>.</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" records are retries</span><span class=se>\n</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=mi>2</span><span class=w> </span><span class=err>o</span><span class=kc>f</span><span class=w> </span><span class=mi>4</span><span class=w> </span><span class=err>records</span><span class=w> </span><span class=err>are</span><span class=w> </span><span class=err>re</span><span class=kc>tr</span><span class=err>ies</span>
|
||||
</code></pre></div> </details> <details class=example> <summary>Example: (2) check with <abbr title="JavaScript Object Notation">JSON</abbr> pointer</summary> <p>The example below checks an optional, nested field with a <abbr title="JavaScript Object Notation">JSON</abbr> pointer, and shows two pointers that <code class=highlight><span class=n>contains</span><span class=p>()</span></code> resolves to <code class=highlight><span class=nb>false</span></code> without throwing.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_pointer</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>json_pointer</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// "retry_of" is only present on some records, nested a level down</span>
|
||||
<span class=w> </span><span class=c1>// inside "meta"</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> [</span>
|
||||
<span class=s> {"id": 1, "meta": {}},</span>
|
||||
<span class=s> {"id": 2, "meta": {"retry_of": 1}}</span>
|
||||
<span class=s> ]</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>records</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_pointer</span><span class=w> </span><span class=n>retry_of</span><span class=p>(</span><span class=s>"/meta/retry_of"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><</span><span class=w> </span><span class=n>records</span><span class=p>.</span><span class=n>size</span><span class=p>();</span><span class=w> </span><span class=o>++</span><span class=n>i</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>record</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>records</span><span class=p>[</span><span class=n>i</span><span class=p>];</span>
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>record</span><span class=p>.</span><span class=n>contains</span><span class=p>(</span><span class=n>retry_of</span><span class=p>))</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"record "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" is a retry of "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>record</span><span class=p>[</span><span class=n>retry_of</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=kt>int</span><span class=o>></span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>else</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"record "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" is original</span><span class=se>\n</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// contains() with a JSON pointer never throws -- not even for a</span>
|
||||
<span class=w> </span><span class=c1>// pointer that indexes into a primitive ("/0/id/x") or uses a</span>
|
||||
<span class=w> </span><span class=c1>// malformed array index ("/01"), either of which would need a</span>
|
||||
<span class=w> </span><span class=c1>// try/catch with json::contains(ptr)</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>records</span><span class=p>.</span><span class=n>contains</span><span class=p>(</span><span class=n>json_pointer</span><span class=p>(</span><span class=s>"/0/id/x"</span><span class=p>))</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>records</span><span class=p>.</span><span class=n>contains</span><span class=p>(</span><span class=n>json_pointer</span><span class=p>(</span><span class=s>"/01"</span><span class=p>))</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=err>record</span><span class=w> </span><span class=mi>0</span><span class=w> </span><span class=err>is</span><span class=w> </span><span class=err>origi</span><span class=kc>nal</span>
|
||||
<span class=err>record</span><span class=w> </span><span class=mi>1</span><span class=w> </span><span class=err>is</span><span class=w> </span><span class=err>a</span><span class=w> </span><span class=err>re</span><span class=kc>tr</span><span class=err>y</span><span class=w> </span><span class=err>o</span><span class=kc>f</span><span class=w> </span><span class=mi>1</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../find/ >find</a> - find a value in an object</li> <li><a href=../count/ >count</a> - returns the number of occurrences of a key</li> <li><a href=../at/ >at</a>, <a href=../operator%5B%5D/ >operator[]</a> - resolve a <abbr title="JavaScript Object Notation">JSON</abbr> pointer and throw, or return a discarded view</li> <li><a href=../../basic_json/contains/ ><code>BasicJsonType::contains</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:28 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.Line truncated
|
||||
@@ -0,0 +1,161 @@
|
||||
# nlohmann::basic_json_view::contains
|
||||
|
||||
```
|
||||
// (1)
|
||||
bool contains(string_view_t key) const;
|
||||
bool contains(const char* key) const;
|
||||
bool contains(const string_t& key) const;
|
||||
|
||||
// (2)
|
||||
bool contains(const json_pointer& ptr) const;
|
||||
```
|
||||
|
||||
1. Checks whether the value is an object with a member with key `key`.
|
||||
1. Checks whether a JSON pointer `ptr` can be resolved, starting at this value.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in) : key value to check its existence
|
||||
|
||||
`ptr` (in) : JSON pointer to check its existence
|
||||
|
||||
## Return value
|
||||
|
||||
1. `true` if the value is an object and has a member with key `key`, `false` otherwise
|
||||
1. `true` if `ptr` can be resolved to a value starting at this view, `false` otherwise
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
1. Linear in the number of members: as for [`ordered_json`](https://json.nlohmann.me/api/ordered_json/index.md), members are compared one after another, in document order, stopping at the first match. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes constant time on average.
|
||||
1. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at that level or the index into the array -- as for [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#complexity) and [`at`](https://json.nlohmann.me/api/basic_json_view/at/#complexity) with a JSON pointer.
|
||||
|
||||
## Notes
|
||||
|
||||
Duplicate keys: the first member wins
|
||||
|
||||
If the source text repeats a key, this resolves to the *first* member with it, not to the last one that [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) and `parse()` keep. See the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes) of `operator[]` and [Duplicate keys](https://json.nlohmann.me/features/json_view/#duplicate-keys) for the reasons and for how to get the last value.
|
||||
|
||||
Overload 1 always returns `false` when the value is not an object -- including a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view.
|
||||
|
||||
Postconditions
|
||||
|
||||
If `v.contains(key)` returns `true`, then `v[key]` is not [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) and `v.at(key)` does not throw. If `v.contains(ptr)` returns `true`, then `v[ptr]` is not discarded and `v.at(ptr)` does not throw.
|
||||
|
||||
Overload 2 never throws
|
||||
|
||||
Unlike [`BasicJsonType::contains(const json_pointer&)`](https://json.nlohmann.me/api/basic_json/contains/index.md), which can throw for certain malformed pointers (for instance an empty array-index reference token), overload 2 never throws: a missing key, an out-of-range or malformed array index, a `"-"` index, or a reference token used on a primitive all simply make it return `false`.
|
||||
|
||||
## Examples
|
||||
|
||||
Example: (1) check with key
|
||||
|
||||
The example below counts how many of a batch of records carry an optional `retry_of` field, using `contains()` to check without ever materializing a single record of the batch.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// count how many of many incoming records carry an optional "retry_of"
|
||||
// field -- contains() only walks the flat index, so scanning a large
|
||||
// batch like this never builds a single nlohmann::json value
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1},
|
||||
{"id": 2, "retry_of": 1},
|
||||
{"id": 3},
|
||||
{"id": 4, "retry_of": 3}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto records = batch.root();
|
||||
std::size_t retries = 0;
|
||||
for (std::size_t i = 0; i < records.size(); ++i)
|
||||
{
|
||||
if (records[i].contains("retry_of"))
|
||||
{
|
||||
++retries;
|
||||
}
|
||||
}
|
||||
std::cout << retries << " of " << records.size() << " records are retries\n";
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
2 of 4 records are retries
|
||||
```
|
||||
|
||||
Example: (2) check with JSON pointer
|
||||
|
||||
The example below checks an optional, nested field with a JSON pointer, and shows two pointers that `contains()` resolves to `false` without throwing.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
// "retry_of" is only present on some records, nested a level down
|
||||
// inside "meta"
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1, "meta": {}},
|
||||
{"id": 2, "meta": {"retry_of": 1}}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto records = batch.root();
|
||||
const json_pointer retry_of("/meta/retry_of");
|
||||
for (std::size_t i = 0; i < records.size(); ++i)
|
||||
{
|
||||
const auto record = records[i];
|
||||
if (record.contains(retry_of))
|
||||
{
|
||||
std::cout << "record " << i << " is a retry of " << record[retry_of].get<int>() << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "record " << i << " is original\n";
|
||||
}
|
||||
}
|
||||
|
||||
// contains() with a JSON pointer never throws -- not even for a
|
||||
// pointer that indexes into a primitive ("/0/id/x") or uses a
|
||||
// malformed array index ("/01"), either of which would need a
|
||||
// try/catch with json::contains(ptr)
|
||||
std::cout << std::boolalpha << records.contains(json_pointer("/0/id/x")) << '\n';
|
||||
std::cout << std::boolalpha << records.contains(json_pointer("/01")) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
record 0 is original
|
||||
record 1 is a retry of 1
|
||||
false
|
||||
false
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [find](https://json.nlohmann.me/api/basic_json_view/find/index.md) - find a value in an object
|
||||
- [count](https://json.nlohmann.me/api/basic_json_view/count/index.md) - returns the number of occurrences of a key
|
||||
- [at](https://json.nlohmann.me/api/basic_json_view/at/index.md), [operator[]](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) - resolve a JSON pointer and throw, or return a discarded view
|
||||
- [`BasicJsonType::contains`](https://json.nlohmann.me/api/basic_json/contains/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,105 @@
|
||||
# <small>nlohmann::basic_json_view::</small>count
|
||||
|
||||
```cpp
|
||||
size_type count(string_view_t key) const;
|
||||
size_type count(const char* key) const;
|
||||
size_type count(const string_t& key) const;
|
||||
```
|
||||
|
||||
Returns the number of members with key `key` if the value is an object, `#!cpp 0` otherwise. Members with a repeated
|
||||
key are all counted.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in)
|
||||
: key value of the element to count
|
||||
|
||||
## Return value
|
||||
|
||||
the number of members of the object with key `key` (duplicates included), or `#!cpp 0` if the value is not an object
|
||||
or has no such member.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the number of members: unlike the lookup functions, `count()` does not stop at the first match, because it
|
||||
has to count every member with the key. Members are compared one after another, in document order. Each comparison
|
||||
first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its
|
||||
content. The hash index of the keys of objects with 128 or more members does not help here, since every member
|
||||
is visited.
|
||||
|
||||
## Notes
|
||||
|
||||
This method always returns `#!cpp 0` when the value is not an object -- including a [discarded](is_discarded.md)
|
||||
view.
|
||||
|
||||
If the source text repeats a key, `count()` returns more than `#!cpp 1` for it, even though the
|
||||
[materialized](materialize.md) value (like the result of `parse()`) would have only one member with that key. Hence
|
||||
`#!cpp count(key) > 1` detects duplicate keys. This is unlike the lookup functions
|
||||
([`operator[]`](operator[].md), [`at`](at.md), [`find`](find.md), [`contains`](contains.md)), which resolve to the
|
||||
*first* member; see the [Notes on duplicate keys](operator[].md#notes) of `operator[]`.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below validates that every transaction of a batch carries exactly one `amount` field, using
|
||||
`count()` before deciding whether to materialize a transaction at all. It reports a transaction with no `amount`
|
||||
field and one with a duplicated `amount` field.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// validate that every transaction of a batch carries exactly one
|
||||
// "amount" field before materializing any of them into a nlohmann::json
|
||||
// value -- count() counts every member with the key
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1, "amount": 9.99},
|
||||
{"id": 2},
|
||||
{"id": 3, "amount": 5.00, "amount": 500.00}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto transactions = batch.root();
|
||||
for (std::size_t i = 0; i < transactions.size(); ++i)
|
||||
{
|
||||
const auto transaction = transactions[i];
|
||||
const auto amounts = transaction.count("amount");
|
||||
if (amounts != 1)
|
||||
{
|
||||
std::cout << "transaction " << i << " has " << amounts << " \"amount\" fields\n";
|
||||
continue;
|
||||
}
|
||||
std::cout << "transaction " << i << ": " << transaction["amount"].materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
transaction 0: 9.99
|
||||
transaction 1 has 0 "amount" fields
|
||||
transaction 2 has 2 "amount" fields
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [find](find.md) - find a value in an object
|
||||
- [contains](contains.md) - checks whether a key exists
|
||||
- [`BasicJsonType::count`](../basic_json/count.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,38 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/count/" rel="canonical"><link href=../contains/ rel=prev><link href=../dump/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>count - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewcount class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> count </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </laLine truncated
|
||||
<span class=n>size_type</span><span class=w> </span><span class=nf>count</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=n>size_type</span><span class=w> </span><span class=nf>count</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>string_t</span><span class=o>&</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <p>Returns the number of members with key <code>key</code> if the value is an object, <code class=highlight><span class=mi>0</span></code> otherwise. Members with a repeated key are all counted.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>key</code> (in)</dt> <dd>key value of the element to count</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>the number of members of the object with key <code>key</code> (duplicates included), or <code class=highlight><span class=mi>0</span></code> if the value is not an object or has no such member.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the number of members: unlike the lookup functions, <code>count()</code> does not stop at the first match, because it has to count every member with the key. Members are compared one after another, in document order. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. The hash index of the keys of objects with 128 or more members does not help here, since every member is visited.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>This method always returns <code class=highlight><span class=mi>0</span></code> when the value is not an object -- including a <a href=../is_discarded/ >discarded</a> view.</p> <p>If the source text repeats a key, <code>count()</code> returns more than <code class=highlight><span class=mi>1</span></code> for it, even though the <a href=../materialize/ >materialized</a> value (like the result of <code>parse()</code>) would have only one member with that key. Hence <code class=highlight><span class=n>count</span><span class=p>(</span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=o>></span><span class=w> </span><span class=mi>1</span></code> detects duplicate keys. This is unlike the lookup functions (<a href=../operator%5B%5D/ ><code>operator[]</code></a>, <a href=../at/ ><code>at</code></a>, <a href=../find/ ><code>find</code></a>, <a href=../contains/ ><code>contains</code></a>), which resolve to the <em>first</em> member; see the <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a> of <code>operator[]</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below validates that every transaction of a batch carries exactly one <code>amount</code> field, using <code>count()</code> before deciding whether to materialize a transaction at all. It reports a transaction with no <code>amount</code> field and one with a duplicated <code>amount</code> field.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// validate that every transaction of a batch carries exactly one</span>
|
||||
<span class=w> </span><span class=c1>// "amount" field before materializing any of them into a nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// value -- count() counts every member with the key</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> [</span>
|
||||
<span class=s> {"id": 1, "amount": 9.99},</span>
|
||||
<span class=s> {"id": 2},</span>
|
||||
<span class=s> {"id": 3, "amount": 5.00, "amount": 500.00}</span>
|
||||
<span class=s> ]</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>transactions</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><</span><span class=w> </span><span class=n>transactions</span><span class=p>.</span><span class=n>size</span><span class=p>();</span><span class=w> </span><span class=o>++</span><span class=n>i</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>transaction</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>transactions</span><span class=p>[</span><span class=n>i</span><span class=p>];</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>amounts</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>transaction</span><span class=p>.</span><span class=n>count</span><span class=p>(</span><span class=s>"amount"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>amounts</span><span class=w> </span><span class=o>!=</span><span class=w> </span><span class=mi>1</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"transaction "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" has "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>amounts</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" </span><span class=se>\"</span><span class=s>amount</span><span class=se>\"</span><span class=s> fields</span><span class=se>\n</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>continue</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"transaction "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>": "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>transaction</span><span class=p>[</span><span class=s>"amount"</span><span class=p>].</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>transa</span><span class=err>c</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=w> </span><span class=mi>0</span><span class=p>:</span><span class=w> </span><span class=mf>9.99</span>
|
||||
<span class=kc>transa</span><span class=err>c</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=w> </span><span class=mi>1</span><span class=w> </span><span class=err>has</span><span class=w> </span><span class=mi>0</span><span class=w> </span><span class=s2>"amount"</span><span class=w> </span><span class=kc>f</span><span class=err>ields</span>
|
||||
<span class=kc>transa</span><span class=err>c</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=w> </span><span class=mi>2</span><span class=w> </span><span class=err>has</span><span class=w> </span><span class=mi>2</span><span class=w> </span><span class=s2>"amount"</span><span class=w> </span><span class=kc>f</span><span class=err>ields</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../find/ >find</a> - find a value in an object</li> <li><a href=../contains/ >contains</a> - checks whether a key exists</li> <li><a href=../../basic_json/count/ ><code>BasicJsonType::count</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:28 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_LLine truncated
|
||||
@@ -0,0 +1,89 @@
|
||||
# nlohmann::basic_json_view::count
|
||||
|
||||
```
|
||||
size_type count(string_view_t key) const;
|
||||
size_type count(const char* key) const;
|
||||
size_type count(const string_t& key) const;
|
||||
```
|
||||
|
||||
Returns the number of members with key `key` if the value is an object, `0` otherwise. Members with a repeated key are all counted.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in) : key value of the element to count
|
||||
|
||||
## Return value
|
||||
|
||||
the number of members of the object with key `key` (duplicates included), or `0` if the value is not an object or has no such member.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the number of members: unlike the lookup functions, `count()` does not stop at the first match, because it has to count every member with the key. Members are compared one after another, in document order. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. The hash index of the keys of objects with 128 or more members does not help here, since every member is visited.
|
||||
|
||||
## Notes
|
||||
|
||||
This method always returns `0` when the value is not an object -- including a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view.
|
||||
|
||||
If the source text repeats a key, `count()` returns more than `1` for it, even though the [materialized](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) value (like the result of `parse()`) would have only one member with that key. Hence `count(key) > 1` detects duplicate keys. This is unlike the lookup functions ([`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md), [`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md), [`find`](https://json.nlohmann.me/api/basic_json_view/find/index.md), [`contains`](https://json.nlohmann.me/api/basic_json_view/contains/index.md)), which resolve to the *first* member; see the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes) of `operator[]`.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below validates that every transaction of a batch carries exactly one `amount` field, using `count()` before deciding whether to materialize a transaction at all. It reports a transaction with no `amount` field and one with a duplicated `amount` field.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// validate that every transaction of a batch carries exactly one
|
||||
// "amount" field before materializing any of them into a nlohmann::json
|
||||
// value -- count() counts every member with the key
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1, "amount": 9.99},
|
||||
{"id": 2},
|
||||
{"id": 3, "amount": 5.00, "amount": 500.00}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto transactions = batch.root();
|
||||
for (std::size_t i = 0; i < transactions.size(); ++i)
|
||||
{
|
||||
const auto transaction = transactions[i];
|
||||
const auto amounts = transaction.count("amount");
|
||||
if (amounts != 1)
|
||||
{
|
||||
std::cout << "transaction " << i << " has " << amounts << " \"amount\" fields\n";
|
||||
continue;
|
||||
}
|
||||
std::cout << "transaction " << i << ": " << transaction["amount"].materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
transaction 0: 9.99
|
||||
transaction 1 has 0 "amount" fields
|
||||
transaction 2 has 2 "amount" fields
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [find](https://json.nlohmann.me/api/basic_json_view/find/index.md) - find a value in an object
|
||||
- [contains](https://json.nlohmann.me/api/basic_json_view/contains/index.md) - checks whether a key exists
|
||||
- [`BasicJsonType::count`](https://json.nlohmann.me/api/basic_json/count/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,143 @@
|
||||
# <small>nlohmann::basic_json_view::</small>dump
|
||||
|
||||
```cpp
|
||||
string_t dump(const int indent = -1,
|
||||
const char indent_char = ' ',
|
||||
const bool ensure_ascii = false,
|
||||
const number_format numbers = number_format::shortest) const;
|
||||
```
|
||||
|
||||
Serializes this value (and its subtree) directly from the flat index, without ever building a `BasicJsonType` value
|
||||
first. With the default `#!cpp numbers == number_format::shortest`, the result is the same string
|
||||
[`BasicJsonType::dump`](../basic_json/dump.md) would produce for the value
|
||||
[`BasicJsonType::parse()`](../basic_json/parse.md) builds from the same source text, called with the same `indent`,
|
||||
`indent_char`, and `ensure_ascii` -- except that members of an object appear in document order rather than sorted by
|
||||
key, and *every* occurrence of a repeated key is written rather than only the last one (see
|
||||
[Notes on duplicate keys](operator[].md#notes)). For a `json_view` (whose `BasicJsonType` is not ordered), this means
|
||||
`dump()` can print an object's members in a different order than [`materialize()`](materialize.md)`.dump()` of the
|
||||
same subtree.
|
||||
|
||||
## Parameters
|
||||
|
||||
`indent` (in)
|
||||
: If `indent` is nonnegative, array elements and object members are pretty-printed with that indent level. An
|
||||
indent level of `0` only inserts newlines. `-1` (the default) selects the most compact representation.
|
||||
|
||||
`indent_char` (in)
|
||||
: The character used for indentation if `indent` is greater than `0`. The default is ` ` (space).
|
||||
|
||||
`ensure_ascii` (in)
|
||||
: If `ensure_ascii` is `#!cpp true`, all non-ASCII characters in the output are escaped with `\uXXXX` sequences, and
|
||||
the result consists of ASCII characters only.
|
||||
|
||||
`numbers` (in)
|
||||
: how to write numbers, see [`number_format`](number_format.md): `shortest` (the default) writes them the way
|
||||
[`BasicJsonType::dump`](../basic_json/dump.md) would; `source` copies every number exactly as it appears in the
|
||||
source text.
|
||||
|
||||
## Return value
|
||||
|
||||
string containing the serialization of this value, or `#!cpp "<discarded>"` if the view is
|
||||
[discarded](is_discarded.md).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong exception safety: if an exception is thrown, there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
May throw `#!cpp std::bad_alloc` if allocating the output string fails. Unlike
|
||||
[`BasicJsonType::dump`](../basic_json/dump.md), there is no `error_handler` parameter and no
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316): the view only ever holds text the parser
|
||||
already validated as UTF-8, so there is nothing to replace or ignore.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the size of the output text.
|
||||
|
||||
## Notes
|
||||
|
||||
The walk over the subtree is iterative, so the nesting depth it can write is limited by available memory only, not by
|
||||
the call stack -- as for [`materialize()`](materialize.md).
|
||||
|
||||
Strings are escaped by the same rules as [`BasicJsonType::dump`](../basic_json/dump.md).
|
||||
|
||||
With `#!cpp numbers == number_format::shortest`, floats are written with the library's shortest round-trip conversion,
|
||||
exactly as [`BasicJsonType::dump`](../basic_json/dump.md) would (e.g. `#!cpp 1.5`, `#!cpp 100.0`, `#!cpp 1e+100`), and
|
||||
integers are copied from the source text -- already canonical in JSON, so this matches their shortest form too --
|
||||
except that `#!cpp -0` is written as `#!cpp 0`, the way [`BasicJsonType::parse()`](../basic_json/parse.md) reads it.
|
||||
|
||||
`#!cpp number_format::source` copies every number exactly as written in the source text instead, with no exception
|
||||
for `#!cpp -0` -- `#!cpp 1.50`, `#!cpp 1E2`, `#!cpp -0.0`, `#!cpp -0`, or all digits of an integer literal with more
|
||||
digits than any number type holds (such a literal is itself classified as a float, see
|
||||
[What is different](../../features/json_view.md#what-is-different)) -- something `BasicJsonType` cannot do, since
|
||||
parsing already reduces every number to its parsed value.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below forwards a single record out of a larger batch, and re-serializes a configuration file, both
|
||||
without ever building a `BasicJsonType` value for the surrounding array or for the parts of it that were not
|
||||
needed. It also shows that [`materialize()`](materialize.md)`.dump()` of the configuration sorts its keys, where
|
||||
`dump()` on the view keeps the order they appear in the source text.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a large batch of sensor readings -- forward just the one that changed,
|
||||
// without ever building a basic_json value for the batch or for the
|
||||
// readings that are not needed
|
||||
const json_document batch = json_document::parse(R"(
|
||||
[{"id": 1, "temp": 21.5}, {"id": 2, "temp": 87.3}, {"id": 3, "temp": 21.7}]
|
||||
)");
|
||||
const json_view readings = batch.root();
|
||||
std::cout << readings[1].dump() << '\n';
|
||||
|
||||
// a configuration file -- dump() on the view keeps the member order of
|
||||
// the source text; a json value's object_t is std::map, so
|
||||
// materialize().dump() of the very same view sorts the keys instead
|
||||
const json_document config = json_document::parse(
|
||||
R"({"name": "cache", "host": "db1", "port": 6379, "timeout": 30})");
|
||||
std::cout << config.root().dump(2) << "\n\n";
|
||||
std::cout << config.root().materialize().dump(2) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
{"id":2,"temp":87.3}
|
||||
{
|
||||
"name": "cache",
|
||||
"host": "db1",
|
||||
"port": 6379,
|
||||
"timeout": 30
|
||||
}
|
||||
|
||||
{
|
||||
"host": "db1",
|
||||
"name": "cache",
|
||||
"port": 6379,
|
||||
"timeout": 30
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [`number_format`](number_format.md) - how `dump()` writes numbers
|
||||
- [operator<<](operator_ltlt.md) - serialize this value to a stream
|
||||
- [materialize](materialize.md) - build a `BasicJsonType` value, e.g. to use `BasicJsonType::dump`'s `error_handler`
|
||||
- [`BasicJsonType::dump`](../basic_json/dump.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,44 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/dump/" rel="canonical"><link href=../count/ rel=prev><link href=../empty/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>dump - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewdump class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> dump </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <Line truncated
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=w> </span><span class=n>indent_char</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=sc>' '</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>ensure_ascii</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>number_format</span><span class=w> </span><span class=n>numbers</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>number_format</span><span class=o>::</span><span class=n>shortest</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <p>Serializes this value (and its subtree) directly from the flat index, without ever building a <code>BasicJsonType</code> value first. With the default <code class=highlight><span class=n>numbers</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>number_format</span><span class=o>::</span><span class=n>shortest</span></code>, the result is the same string <a href=../../basic_json/dump/ ><code>BasicJsonType::dump</code></a> would produce for the value <a href=../../basic_json/parse/ ><code>BasicJsonType::parse()</code></a> builds from the same source text, called with the same <code>indent</code>, <code>indent_char</code>, and <code>ensure_ascii</code> -- except that members of an object appear in document order rather than sorted by key, and <em>every</em> occurrence of a repeated key is written rather than only the last one (see <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a>). For a <code>json_view</code> (whose <code>BasicJsonType</code> is not ordered), this means <code>dump()</code> can print an object's members in a different order than <a href=../materialize/ ><code>materialize()</code></a><code>.dump()</code> of the same subtree.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>indent</code> (in)</dt> <dd>If <code>indent</code> is nonnegative, array elements and object members are pretty-printed with that indent level. An indent level of <code>0</code> only inserts newlines. <code>-1</code> (the default) selects the most compact representation.</dd> <dt><code>indent_char</code> (in)</dt> <dd>The character used for indentation if <code>indent</code> is greater than <code>0</code>. The default is <code></code> (space).</dd> <dt><code>ensure_ascii</code> (in)</dt> <dd>If <code>ensure_ascii</code> is <code class=highlight><span class=nb>true</span></code>, all non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters in the output are escaped with <code>\uXXXX</code> sequences, and the result consists of <abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters only.</dd> <dt><code>numbers</code> (in)</dt> <dd>how to write numbers, see <a href=../number_format/ ><code>number_format</code></a>: <code>shortest</code> (the default) writes them the way <a href=../../basic_json/dump/ ><code>BasicJsonType::dump</code></a> would; <code>source</code> copies every number exactly as it appears in the source text.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>string containing the serialization of this value, or <code class=highlight><span class=s>"<discarded>"</span></code> if the view is <a href=../is_discarded/ >discarded</a>.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong exception safety: if an exception is thrown, there are no changes to the view or the document it refers to.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>May throw <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>bad_alloc</span></code> if allocating the output string fails. Unlike <a href=../../basic_json/dump/ ><code>BasicJsonType::dump</code></a>, there is no <code>error_handler</code> parameter and no <a href=../../../home/exceptions/#jsonexceptiontype_error316><code>type_error.316</code></a>: the view only ever holds text the parser already validated as <abbr title="Unicode Transformation Format">UTF</abbr>-8, so there is nothing to replace or ignore.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the output text.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>The walk over the subtree is iterative, so the nesting depth it can write is limited by available memory only, not by the call stack -- as for <a href=../materialize/ ><code>materialize()</code></a>.</p> <p>Strings are escaped by the same rules as <a href=../../basic_json/dump/ ><code>BasicJsonType::dump</code></a>.</p> <p>With <code class=highlight><span class=n>numbers</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>number_format</span><span class=o>::</span><span class=n>shortest</span></code>, floats are written with the library's shortest round-trip conversion, exactly as <a href=../../basic_json/dump/ ><code>BasicJsonType::dump</code></a> would (e.g. <code class=highlight><span class=mf>1.5</span></code>, <code class=highlight><span class=mf>100.0</span></code>, <code class=highlight><span class=mf>1e+100</span></code>), and integers are copied from the source text -- already canonical in <abbr title="JavaScript Object Notation">JLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a large batch of sensor readings -- forward just the one that changed,</span>
|
||||
<span class=w> </span><span class=c1>// without ever building a basic_json value for the batch or for the</span>
|
||||
<span class=w> </span><span class=c1>// readings that are not needed</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> [{"id": 1, "temp": 21.5}, {"id": 2, "temp": 87.3}, {"id": 3, "temp": 21.7}]</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>readings</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>readings</span><span class=p>[</span><span class=mi>1</span><span class=p>].</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a configuration file -- dump() on the view keeps the member order of</span>
|
||||
<span class=w> </span><span class=c1>// the source text; a json value's object_t is std::map, so</span>
|
||||
<span class=w> </span><span class=c1>// materialize().dump() of the very same view sorts the keys instead</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>config</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span>
|
||||
<span class=w> </span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"name": "cache", "host": "db1", "port": 6379, "timeout": 30}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>config</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>dump</span><span class=p>(</span><span class=mi>2</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"</span><span class=se>\n\n</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>config</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>(</span><span class=mi>2</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>{</span><span class=nt>"id"</span><span class=p>:</span><span class=mi>2</span><span class=p>,</span><span class=nt>"temp"</span><span class=p>:</span><span class=mf>87.3</span><span class=p>}</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=nt>"name"</span><span class=p>:</span><span class=w> </span><span class=s2>"cache"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"host"</span><span class=p>:</span><span class=w> </span><span class=s2>"db1"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"port"</span><span class=p>:</span><span class=w> </span><span class=mi>6379</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"timeout"</span><span class=p>:</span><span class=w> </span><span class=mi>30</span>
|
||||
<span class=p>}</span>
|
||||
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=nt>"host"</span><span class=p>:</span><span class=w> </span><span class=s2>"db1"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"name"</span><span class=p>:</span><span class=w> </span><span class=s2>"cache"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"port"</span><span class=p>:</span><span class=w> </span><span class=mi>6379</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"timeout"</span><span class=p>:</span><span class=w> </span><span class=mi>30</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../number_format/ ><code>number_format</code></a> - how <code>dump()</code> writes numbers</li> <li><a href=../operator_ltlt/ >operator<<</a> - serialize this value to a stream</li> <li><a href=../materialize/ >materialize</a> - build a <code>BasicJsonType</code> value, e.g. to use <code>BasicJsonType::dump</code>'s <code>error_handler</code></li> <li><a href=../../basic_json/dump/ ><code>BasicJsonType::dump</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:28 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5Line truncated
|
||||
@@ -0,0 +1,110 @@
|
||||
# nlohmann::basic_json_view::dump
|
||||
|
||||
```
|
||||
string_t dump(const int indent = -1,
|
||||
const char indent_char = ' ',
|
||||
const bool ensure_ascii = false,
|
||||
const number_format numbers = number_format::shortest) const;
|
||||
```
|
||||
|
||||
Serializes this value (and its subtree) directly from the flat index, without ever building a `BasicJsonType` value first. With the default `numbers == number_format::shortest`, the result is the same string [`BasicJsonType::dump`](https://json.nlohmann.me/api/basic_json/dump/index.md) would produce for the value [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) builds from the same source text, called with the same `indent`, `indent_char`, and `ensure_ascii` -- except that members of an object appear in document order rather than sorted by key, and *every* occurrence of a repeated key is written rather than only the last one (see [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes)). For a `json_view` (whose `BasicJsonType` is not ordered), this means `dump()` can print an object's members in a different order than [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md)`.dump()` of the same subtree.
|
||||
|
||||
## Parameters
|
||||
|
||||
`indent` (in) : If `indent` is nonnegative, array elements and object members are pretty-printed with that indent level. An indent level of `0` only inserts newlines. `-1` (the default) selects the most compact representation.
|
||||
|
||||
`indent_char` (in) : The character used for indentation if `indent` is greater than `0`. The default is (space).
|
||||
|
||||
`ensure_ascii` (in) : If `ensure_ascii` is `true`, all non-ASCII characters in the output are escaped with `\uXXXX` sequences, and the result consists of ASCII characters only.
|
||||
|
||||
`numbers` (in) : how to write numbers, see [`number_format`](https://json.nlohmann.me/api/basic_json_view/number_format/index.md): `shortest` (the default) writes them the way [`BasicJsonType::dump`](https://json.nlohmann.me/api/basic_json/dump/index.md) would; `source` copies every number exactly as it appears in the source text.
|
||||
|
||||
## Return value
|
||||
|
||||
string containing the serialization of this value, or `"<discarded>"` if the view is [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong exception safety: if an exception is thrown, there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
May throw `std::bad_alloc` if allocating the output string fails. Unlike [`BasicJsonType::dump`](https://json.nlohmann.me/api/basic_json/dump/index.md), there is no `error_handler` parameter and no [`type_error.316`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316): the view only ever holds text the parser already validated as UTF-8, so there is nothing to replace or ignore.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the size of the output text.
|
||||
|
||||
## Notes
|
||||
|
||||
The walk over the subtree is iterative, so the nesting depth it can write is limited by available memory only, not by the call stack -- as for [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md).
|
||||
|
||||
Strings are escaped by the same rules as [`BasicJsonType::dump`](https://json.nlohmann.me/api/basic_json/dump/index.md).
|
||||
|
||||
With `numbers == number_format::shortest`, floats are written with the library's shortest round-trip conversion, exactly as [`BasicJsonType::dump`](https://json.nlohmann.me/api/basic_json/dump/index.md) would (e.g. `1.5`, `100.0`, `1e+100`), and integers are copied from the source text -- already canonical in JSON, so this matches their shortest form too -- except that `-0` is written as `0`, the way [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) reads it.
|
||||
|
||||
`number_format::source` copies every number exactly as written in the source text instead, with no exception for `-0` -- `1.50`, `1E2`, `-0.0`, `-0`, or all digits of an integer literal with more digits than any number type holds (such a literal is itself classified as a float, see [What is different](https://json.nlohmann.me/features/json_view/#what-is-different)) -- something `BasicJsonType` cannot do, since parsing already reduces every number to its parsed value.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below forwards a single record out of a larger batch, and re-serializes a configuration file, both without ever building a `BasicJsonType` value for the surrounding array or for the parts of it that were not needed. It also shows that [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md)`.dump()` of the configuration sorts its keys, where `dump()` on the view keeps the order they appear in the source text.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a large batch of sensor readings -- forward just the one that changed,
|
||||
// without ever building a basic_json value for the batch or for the
|
||||
// readings that are not needed
|
||||
const json_document batch = json_document::parse(R"(
|
||||
[{"id": 1, "temp": 21.5}, {"id": 2, "temp": 87.3}, {"id": 3, "temp": 21.7}]
|
||||
)");
|
||||
const json_view readings = batch.root();
|
||||
std::cout << readings[1].dump() << '\n';
|
||||
|
||||
// a configuration file -- dump() on the view keeps the member order of
|
||||
// the source text; a json value's object_t is std::map, so
|
||||
// materialize().dump() of the very same view sorts the keys instead
|
||||
const json_document config = json_document::parse(
|
||||
R"({"name": "cache", "host": "db1", "port": 6379, "timeout": 30})");
|
||||
std::cout << config.root().dump(2) << "\n\n";
|
||||
std::cout << config.root().materialize().dump(2) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
{"id":2,"temp":87.3}
|
||||
{
|
||||
"name": "cache",
|
||||
"host": "db1",
|
||||
"port": 6379,
|
||||
"timeout": 30
|
||||
}
|
||||
|
||||
{
|
||||
"host": "db1",
|
||||
"name": "cache",
|
||||
"port": 6379,
|
||||
"timeout": 30
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [`number_format`](https://json.nlohmann.me/api/basic_json_view/number_format/index.md) - how `dump()` writes numbers
|
||||
- [operator\<<](https://json.nlohmann.me/api/basic_json_view/operator_ltlt/index.md) - serialize this value to a stream
|
||||
- [materialize](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) - build a `BasicJsonType` value, e.g. to use `BasicJsonType::dump`'s `error_handler`
|
||||
- [`BasicJsonType::dump`](https://json.nlohmann.me/api/basic_json/dump/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,86 @@
|
||||
# <small>nlohmann::basic_json_view::</small>empty
|
||||
|
||||
```cpp
|
||||
bool empty() const noexcept;
|
||||
```
|
||||
|
||||
Checks whether [`size()`](size.md) is `0`, as [`BasicJsonType::empty()`](../basic_json/empty.md) would for the same
|
||||
value.
|
||||
|
||||
## Return value
|
||||
|
||||
The return value depends on the type and is defined as follows:
|
||||
|
||||
| Value type | return value |
|
||||
|----------------------|-----------------|
|
||||
| null | `#!cpp true` |
|
||||
| discarded | `#!cpp true` |
|
||||
| boolean | `#!cpp false` |
|
||||
| string | `#!cpp false` |
|
||||
| number | `#!cpp false` |
|
||||
| object | `#!cpp object_t::empty()` |
|
||||
| array | `#!cpp array_t::empty()` |
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`BasicJsonType::empty()`](../basic_json/empty.md), this does not return whether a string value is empty -- it
|
||||
is `#!cpp false` for any string, regardless of its length.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below uses [`size()`](size.md) and `empty()` to decide whether a parsed message is worth acting on,
|
||||
without materializing it into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// decide whether a batch is worth processing before building any
|
||||
// nlohmann::json value for it
|
||||
json_document batch = json_document::parse(R"([1, 2, 3, 4, 5])");
|
||||
json_document empty_batch = json_document::parse("[]");
|
||||
|
||||
std::cout << batch.root().empty() << ' ' << batch.root().size() << '\n';
|
||||
std::cout << empty_batch.root().empty() << ' ' << empty_batch.root().size() << '\n';
|
||||
|
||||
// as for basic_json: null has size 0, every other scalar has size 1
|
||||
json_document n = json_document::parse("null");
|
||||
json_document s = json_document::parse(R"("hi")");
|
||||
std::cout << n.root().size() << ' ' << s.root().size() << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
false 5
|
||||
true 0
|
||||
0 1
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [size](size.md) - return the number of elements
|
||||
- [`BasicJsonType::empty`](../basic_json/empty.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,27 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/empty/" rel="canonical"><link href=../dump/ rel=prev><link href=../end/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>empty - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewempty class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> empty </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> Line truncated
|
||||
</code></pre></div> <p>Checks whether <a href=../size/ ><code>size()</code></a> is <code>0</code>, as <a href=../../basic_json/empty/ ><code>BasicJsonType::empty()</code></a> would for the same value.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>The return value depends on the type and is defined as follows:</p> <table> <thead> <tr> <th>Value type</th> <th>return value</th> </tr> </thead> <tbody> <tr> <td>null</td> <td><code class=highlight><span class=nb>true</span></code></td> </tr> <tr> <td>discarded</td> <td><code class=highlight><span class=nb>true</span></code></td> </tr> <tr> <td>boolean</td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td>string</td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td>number</td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td>object</td> <td><code class=highlight><span class=n>object_t</span><span class=o>::</span><span class=n>empty</span><span class=p>()</span></code></td> </tr> <tr> <td>array</td> <td><code class=highlight><span class=n>array_t</span><span class=o>::</span><span class=n>empty</span><span class=p>()</span></code></td> </tr> </tbody> </table> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>As for <a href=../../basic_json/empty/ ><code>BasicJsonType::empty()</code></a>, this does not return whether a string value is empty -- it is <code class=highlight><span class=nb>false</span></code> for any string, regardless of its length.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below uses <a href=../size/ ><code>size()</code></a> and <code>empty()</code> to decide whether a parsed message is worth acting on, without materializing it into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// decide whether a batch is worth processing before building any</span>
|
||||
<span class=w> </span><span class=c1>// nlohmann::json value for it</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>[1, 2, 3, 4, 5]</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>empty_batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[]"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>empty</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>empty_batch</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>empty</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>empty_batch</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// as for basic_json: null has size 0, every other scalar has size 1</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>n</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>s</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>n</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>s</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>false</span><span class=w> </span><span class=mi>5</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=mi>0</span>
|
||||
<span class=mi>0</span><span class=w> </span><span class=mi>1</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../size/ >size</a> - return the number of elements</li> <li><a href=../../basic_json/empty/ ><code>BasicJsonType::empty</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-Line truncated
|
||||
@@ -0,0 +1,81 @@
|
||||
# nlohmann::basic_json_view::empty
|
||||
|
||||
```
|
||||
bool empty() const noexcept;
|
||||
```
|
||||
|
||||
Checks whether [`size()`](https://json.nlohmann.me/api/basic_json_view/size/index.md) is `0`, as [`BasicJsonType::empty()`](https://json.nlohmann.me/api/basic_json/empty/index.md) would for the same value.
|
||||
|
||||
## Return value
|
||||
|
||||
The return value depends on the type and is defined as follows:
|
||||
|
||||
| Value type | return value |
|
||||
| ---------- | ------------------- |
|
||||
| null | `true` |
|
||||
| discarded | `true` |
|
||||
| boolean | `false` |
|
||||
| string | `false` |
|
||||
| number | `false` |
|
||||
| object | `object_t::empty()` |
|
||||
| array | `array_t::empty()` |
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`BasicJsonType::empty()`](https://json.nlohmann.me/api/basic_json/empty/index.md), this does not return whether a string value is empty -- it is `false` for any string, regardless of its length.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below uses [`size()`](https://json.nlohmann.me/api/basic_json_view/size/index.md) and `empty()` to decide whether a parsed message is worth acting on, without materializing it into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// decide whether a batch is worth processing before building any
|
||||
// nlohmann::json value for it
|
||||
json_document batch = json_document::parse(R"([1, 2, 3, 4, 5])");
|
||||
json_document empty_batch = json_document::parse("[]");
|
||||
|
||||
std::cout << batch.root().empty() << ' ' << batch.root().size() << '\n';
|
||||
std::cout << empty_batch.root().empty() << ' ' << empty_batch.root().size() << '\n';
|
||||
|
||||
// as for basic_json: null has size 0, every other scalar has size 1
|
||||
json_document n = json_document::parse("null");
|
||||
json_document s = json_document::parse(R"("hi")");
|
||||
std::cout << n.root().size() << ' ' << s.root().size() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
false 5
|
||||
true 0
|
||||
0 1
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [size](https://json.nlohmann.me/api/basic_json_view/size/index.md) - return the number of elements
|
||||
- [`BasicJsonType::empty`](https://json.nlohmann.me/api/basic_json/empty/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,81 @@
|
||||
# <small>nlohmann::basic_json_view::</small>end
|
||||
|
||||
```cpp
|
||||
iterator end() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to one past the last element of an array, one past the last member value of an object, in
|
||||
**document order** -- see [`begin()`](begin.md). A primitive value iterates as a range of one element (itself), and a
|
||||
`#!json null` value or a [discarded](is_discarded.md) view iterates as an empty range (`#!cpp begin() == end()`).
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator one past the last element.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`#!cpp iterator` is a forward iterator: unlike `BasicJsonType::iterator`, it cannot be decremented, so there is no
|
||||
way to reach the last element by stepping back from `end()`. Use [`back()`](back.md) instead.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below scans a (possibly large) array of readings for the first one over a threshold, stopping the
|
||||
loop at `end()` as soon as one is found. Only the matching reading, if any, is ever materialized.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// scan a (possibly large) array of readings for the first one over a
|
||||
// threshold; the loop stops at end() as soon as one is found, and only
|
||||
// the matching reading is ever materialized
|
||||
json_document readings = json_document::parse("[12, 18, 25, 31, 9]");
|
||||
const auto values = readings.root();
|
||||
|
||||
auto it = values.begin();
|
||||
for (; it != values.end(); ++it)
|
||||
{
|
||||
if (it->materialize().get<int>() > 20)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (it != values.end())
|
||||
{
|
||||
std::cout << "first reading over 20: " << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
first reading over 20: 25
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [begin](begin.md) - returns an iterator to the first element
|
||||
- [cend](cend.md) - returns a const iterator to one past the last element
|
||||
- [`BasicJsonType::end`](../basic_json/end.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,30 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/end/" rel="canonical"><link href=../empty/ rel=prev><link href=../find/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>end - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewend class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> end </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <div cLine truncated
|
||||
</code></pre></div> <p>Returns an iterator to one past the last element of an array, one past the last member value of an object, in <strong>document order</strong> -- see <a href=../begin/ ><code>begin()</code></a>. A primitive value iterates as a range of one element (itself), and a <code class=highlight><span class=kc>null</span></code> value or a <a href=../is_discarded/ >discarded</a> view iterates as an empty range (<code class=highlight><span class=n>begin</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>end</span><span class=p>()</span></code>).</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>Iterator one past the last element.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p><code class=highlight><span class=n>iterator</span></code> is a forward iterator: unlike <code>BasicJsonType::iterator</code>, it cannot be decremented, so there is no way to reach the last element by stepping back from <code>end()</code>. Use <a href=../back/ ><code>back()</code></a> instead.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below scans a (possibly large) array of readings for the first one over a threshold, stopping the loop at <code>end()</code> as soon as one is found. Only the matching reading, if any, is ever materialized.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// scan a (possibly large) array of readings for the first one over a</span>
|
||||
<span class=w> </span><span class=c1>// threshold; the loop stops at end() as soon as one is found, and only</span>
|
||||
<span class=w> </span><span class=c1>// the matching reading is ever materialized</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>readings</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[12, 18, 25, 31, 9]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>values</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>readings</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>it</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>values</span><span class=p>.</span><span class=n>begin</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(;</span><span class=w> </span><span class=n>it</span><span class=w> </span><span class=o>!=</span><span class=w> </span><span class=n>values</span><span class=p>.</span><span class=n>end</span><span class=p>();</span><span class=w> </span><span class=o>++</span><span class=n>it</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>it</span><span class=o>-></span><span class=n>materialize</span><span class=p>().</span><span class=n>get</span><span class=o><</span><span class=kt>int</span><span class=o>></span><span class=p>()</span><span class=w> </span><span class=o>></span><span class=w> </span><span class=mi>20</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>break</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>it</span><span class=w> </span><span class=o>!=</span><span class=w> </span><span class=n>values</span><span class=p>.</span><span class=n>end</span><span class=p>())</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"first reading over 20: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>it</span><span class=o>-></span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>f</span><span class=err>irs</span><span class=kc>t</span><span class=w> </span><span class=err>readi</span><span class=kc>n</span><span class=err>g</span><span class=w> </span><span class=err>over</span><span class=w> </span><span class=mi>20</span><span class=p>:</span><span class=w> </span><span class=mi>25</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../begin/ >begin</a> - returns an iterator to the first element</li> <li><a href=../cend/ >cend</a> - returns a const iterator to one past the last element</li> <li><a href=../../basic_json/end/ ><code>BasicJsonType::end</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="httpsLine truncated
|
||||
@@ -0,0 +1,75 @@
|
||||
# nlohmann::basic_json_view::end
|
||||
|
||||
```
|
||||
iterator end() const noexcept;
|
||||
```
|
||||
|
||||
Returns an iterator to one past the last element of an array, one past the last member value of an object, in **document order** -- see [`begin()`](https://json.nlohmann.me/api/basic_json_view/begin/index.md). A primitive value iterates as a range of one element (itself), and a `null` value or a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view iterates as an empty range (`begin() == end()`).
|
||||
|
||||
## Return value
|
||||
|
||||
Iterator one past the last element.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`iterator` is a forward iterator: unlike `BasicJsonType::iterator`, it cannot be decremented, so there is no way to reach the last element by stepping back from `end()`. Use [`back()`](https://json.nlohmann.me/api/basic_json_view/back/index.md) instead.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below scans a (possibly large) array of readings for the first one over a threshold, stopping the loop at `end()` as soon as one is found. Only the matching reading, if any, is ever materialized.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// scan a (possibly large) array of readings for the first one over a
|
||||
// threshold; the loop stops at end() as soon as one is found, and only
|
||||
// the matching reading is ever materialized
|
||||
json_document readings = json_document::parse("[12, 18, 25, 31, 9]");
|
||||
const auto values = readings.root();
|
||||
|
||||
auto it = values.begin();
|
||||
for (; it != values.end(); ++it)
|
||||
{
|
||||
if (it->materialize().get<int>() > 20)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (it != values.end())
|
||||
{
|
||||
std::cout << "first reading over 20: " << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
first reading over 20: 25
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [begin](https://json.nlohmann.me/api/basic_json_view/begin/index.md) - returns an iterator to the first element
|
||||
- [cend](https://json.nlohmann.me/api/basic_json_view/cend/index.md) - returns a const iterator to one past the last element
|
||||
- [`BasicJsonType::end`](https://json.nlohmann.me/api/basic_json/end/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,103 @@
|
||||
# <small>nlohmann::basic_json_view::</small>find
|
||||
|
||||
```cpp
|
||||
iterator find(string_view_t key) const;
|
||||
iterator find(const char* key) const;
|
||||
iterator find(const string_t& key) const;
|
||||
```
|
||||
|
||||
Finds a member with key `key` -- the first one, should the key occur more than once (see
|
||||
[Notes on duplicate keys](operator[].md#notes)). If the value is not an object, or no member has this key,
|
||||
[`end()`](end.md) is returned.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in)
|
||||
: key value of the element to search for
|
||||
|
||||
## Return value
|
||||
|
||||
An iterator to the member with key `key`, or [`end()`](end.md) if there is none or the value is not an object.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the number of members: as for [`ordered_json`](../ordered_json.md), members are compared one after another,
|
||||
in document order, stopping at the first match. Each comparison first checks the key's length
|
||||
-- already known from the index, without reading the key bytes -- before comparing its content.
|
||||
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
||||
constant time on average.
|
||||
|
||||
## Notes
|
||||
|
||||
!!! warning "Duplicate keys: the first member wins"
|
||||
|
||||
If the source text repeats a key, this resolves to the *first* member with it, not to the last one that
|
||||
[`materialize()`](materialize.md) and `parse()` keep. See the [Notes on duplicate keys](operator[].md#notes) of
|
||||
`operator[]` and [Duplicate keys](../../features/json_view.md#duplicate-keys) for the reasons and for how to get
|
||||
the last value.
|
||||
|
||||
Unlike [`BasicJsonType::find`](../basic_json/find.md), which always returns `#!cpp end()` for a non-object type, this
|
||||
also does so for a [discarded](is_discarded.md) view -- there is no separate "invalid" iterator to return.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below scans a batch of events for those that carry an optional `user_id` field, using `find()`
|
||||
instead of [`operator[]`](operator[].md) (which would throw for the events that are not objects at all) or
|
||||
[`contains()`](contains.md) followed by a second lookup. Events without a match are never materialized.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a batch of incoming events; only some carry a "user_id" -- find()
|
||||
// locates it without throwing for the events that turn out to not be
|
||||
// objects, and without materializing an event that does not match
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"type": "click", "user_id": 42},
|
||||
{"type": "ping"},
|
||||
{"type": "click", "user_id": 7}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto events = batch.root();
|
||||
for (std::size_t i = 0; i < events.size(); ++i)
|
||||
{
|
||||
const auto event = events[i];
|
||||
const auto it = event.find("user_id");
|
||||
if (it != event.end())
|
||||
{
|
||||
std::cout << "user " << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
user 42
|
||||
user 7
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [count](count.md) - returns the number of occurrences of a key
|
||||
- [contains](contains.md) - checks whether a key exists
|
||||
- [`BasicJsonType::find`](../basic_json/find.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,35 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/find/" rel="canonical"><link href=../end/ rel=prev><link href=../front/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>find - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewfind class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> find </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <diLine truncated
|
||||
<span class=n>iterator</span><span class=w> </span><span class=nf>find</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
<span class=n>iterator</span><span class=w> </span><span class=nf>find</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>string_t</span><span class=o>&</span><span class=w> </span><span class=n>key</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <p>Finds a member with key <code>key</code> -- the first one, should the key occur more than once (see <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a>). If the value is not an object, or no member has this key, <a href=../end/ ><code>end()</code></a> is returned.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>key</code> (in)</dt> <dd>key value of the element to search for</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>An iterator to the member with key <code>key</code>, or <a href=../end/ ><code>end()</code></a> if there is none or the value is not an object.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the number of members: as for <a href=../../ordered_json/ ><code>ordered_json</code></a>, members are compared one after another, in document order, stopping at the first match. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes constant time on average.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <div class="admonition warning"> <p class=admonition-title>Duplicate keys: the first member wins</p> <p>If the source text repeats a key, this resolves to the <em>first</em> member with it, not to the last one that <a href=../materialize/ ><code>materialize()</code></a> and <code>parse()</code> keep. See the <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a> of <code>operator[]</code> and <a href=../../../features/json_view/#duplicate-keys>Duplicate keys</a> for the reasons and for how to get the last value.</p> </div> <p>Unlike <a href=../../basic_json/find/ ><code>BasicJsonType::find</code></a>, which always returns <code class=highlight><span class=n>end</span><span class=p>()</span></code> for a non-object type, this also does so for a <a href=../is_discarded/ >discarded</a> view -- there is no separate "invalid" iterator to return.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below scans a batch of events for those that carry an optional <code>user_id</code> field, using <code>find()</code> instead of <a href=../operator%5B%5D/ ><code>operator[]</code></a> (which would throw for the events that are not objects at all) or <a href=../contains/ ><code>contains()</code></a> followed by a second lookup. Events without a match are never materialized.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a batch of incoming events; only some carry a "user_id" -- find()</span>
|
||||
<span class=w> </span><span class=c1>// locates it without throwing for the events that turn out to not be</span>
|
||||
<span class=w> </span><span class=c1>// objects, and without materializing an event that does not match</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> [</span>
|
||||
<span class=s> {"type": "click", "user_id": 42},</span>
|
||||
<span class=s> {"type": "ping"},</span>
|
||||
<span class=s> {"type": "click", "user_id": 7}</span>
|
||||
<span class=s> ]</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>events</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>batch</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o><</span><span class=w> </span><span class=n>events</span><span class=p>.</span><span class=n>size</span><span class=p>();</span><span class=w> </span><span class=o>++</span><span class=n>i</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>event</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>events</span><span class=p>[</span><span class=n>i</span><span class=p>];</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>it</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>event</span><span class=p>.</span><span class=n>find</span><span class=p>(</span><span class=s>"user_id"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>it</span><span class=w> </span><span class=o>!=</span><span class=w> </span><span class=n>event</span><span class=p>.</span><span class=n>end</span><span class=p>())</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"user "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>it</span><span class=o>-></span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=err>user</span><span class=w> </span><span class=mi>42</span>
|
||||
<span class=err>user</span><span class=w> </span><span class=mi>7</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../count/ >count</a> - returns the number of occurrences of a key</li> <li><a href=../contains/ >contains</a> - checks whether a key exists</li> <li><a href=../../basic_json/find/ ><code>BasicJsonType::find</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:28 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.coLine truncated
|
||||
@@ -0,0 +1,88 @@
|
||||
# nlohmann::basic_json_view::find
|
||||
|
||||
```
|
||||
iterator find(string_view_t key) const;
|
||||
iterator find(const char* key) const;
|
||||
iterator find(const string_t& key) const;
|
||||
```
|
||||
|
||||
Finds a member with key `key` -- the first one, should the key occur more than once (see [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes)). If the value is not an object, or no member has this key, [`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md) is returned.
|
||||
|
||||
## Parameters
|
||||
|
||||
`key` (in) : key value of the element to search for
|
||||
|
||||
## Return value
|
||||
|
||||
An iterator to the member with key `key`, or [`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md) if there is none or the value is not an object.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the number of members: as for [`ordered_json`](https://json.nlohmann.me/api/ordered_json/index.md), members are compared one after another, in document order, stopping at the first match. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content. Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes constant time on average.
|
||||
|
||||
## Notes
|
||||
|
||||
Duplicate keys: the first member wins
|
||||
|
||||
If the source text repeats a key, this resolves to the *first* member with it, not to the last one that [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) and `parse()` keep. See the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes) of `operator[]` and [Duplicate keys](https://json.nlohmann.me/features/json_view/#duplicate-keys) for the reasons and for how to get the last value.
|
||||
|
||||
Unlike [`BasicJsonType::find`](https://json.nlohmann.me/api/basic_json/find/index.md), which always returns `end()` for a non-object type, this also does so for a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view -- there is no separate "invalid" iterator to return.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below scans a batch of events for those that carry an optional `user_id` field, using `find()` instead of [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) (which would throw for the events that are not objects at all) or [`contains()`](https://json.nlohmann.me/api/basic_json_view/contains/index.md) followed by a second lookup. Events without a match are never materialized.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a batch of incoming events; only some carry a "user_id" -- find()
|
||||
// locates it without throwing for the events that turn out to not be
|
||||
// objects, and without materializing an event that does not match
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"type": "click", "user_id": 42},
|
||||
{"type": "ping"},
|
||||
{"type": "click", "user_id": 7}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto events = batch.root();
|
||||
for (std::size_t i = 0; i < events.size(); ++i)
|
||||
{
|
||||
const auto event = events[i];
|
||||
const auto it = event.find("user_id");
|
||||
if (it != event.end())
|
||||
{
|
||||
std::cout << "user " << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
user 42
|
||||
user 7
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [count](https://json.nlohmann.me/api/basic_json_view/count/index.md) - returns the number of occurrences of a key
|
||||
- [contains](https://json.nlohmann.me/api/basic_json_view/contains/index.md) - checks whether a key exists
|
||||
- [`BasicJsonType::find`](https://json.nlohmann.me/api/basic_json/find/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,87 @@
|
||||
# <small>nlohmann::basic_json_view::</small>front
|
||||
|
||||
```cpp
|
||||
basic_json_view front() const;
|
||||
```
|
||||
|
||||
Returns the first element of an array, the first member value of an object, or the value itself if it is primitive
|
||||
(as for [`BasicJsonType::front()`](../basic_json/front.md), a primitive value is a range of one element).
|
||||
|
||||
## Return value
|
||||
|
||||
The first element or member value. For a primitive value (number, string, boolean), the value itself.
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`invalid_iterator.214`](../../home/exceptions.md#jsonexceptioninvalid_iterator214) if the view is
|
||||
[null](is_null.md) or [discarded](is_discarded.md), or if it is an empty array or object.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no
|
||||
`BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with
|
||||
`JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
Unlike [`BasicJsonType::front()`](../basic_json/front.md), which has undefined behavior for an empty array or
|
||||
object, `front()` throws `invalid_iterator.214` for an empty array, an empty object, a null value, or a discarded
|
||||
view.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below reads only the earliest entry of a build log with `front()`, without materializing the rest
|
||||
of the (possibly long) log.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// the build log of a running job; front() reads just the earliest event
|
||||
// without materializing the (possibly long) rest of the log
|
||||
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
|
||||
std::cout << log.root().front().materialize().dump() << '\n';
|
||||
|
||||
// an empty log -- front() throws instead of the undefined behavior
|
||||
// basic_json::front() has for an empty array
|
||||
json_document empty_log = json_document::parse("[]");
|
||||
try
|
||||
{
|
||||
static_cast<void>(empty_log.root().front());
|
||||
}
|
||||
catch (const nlohmann::json::invalid_iterator& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
"queued"
|
||||
[json.exception.invalid_iterator.214] cannot get value
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [back](back.md) - access the last element
|
||||
- [`BasicJsonType::front`](../basic_json/front.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,28 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/front/" rel="canonical"><link href=../find/ rel=prev><link href=../get/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>front - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewfront class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> front </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> Line truncated
|
||||
</code></pre></div> <p>Returns the first element of an array, the first member value of an object, or the value itself if it is primitive (as for <a href=../../basic_json/front/ ><code>BasicJsonType::front()</code></a>, a primitive value is a range of one element).</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>The first element or member value. For a primitive value (number, string, boolean), the value itself.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: there are no changes to the view or the document it refers to.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>Throws <a href=../../../home/exceptions/#jsonexceptioninvalid_iterator214><code>invalid_iterator.214</code></a> if the view is <a href=../is_null/ >null</a> or <a href=../is_discarded/ >discarded</a>, or if it is an empty array or object.</p> <p>This exception does not carry a <a href=../../macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> path: the view has no <code>BasicJsonType</code> value to point at, so the exception is created without one, even if <code>BasicJsonType</code> was built with <code>JSON_DIAGNOSTICS</code> enabled.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>Unlike <a href=../../basic_json/front/ ><code>BasicJsonType::front()</code></a>, which has undefined behavior for an empty array or object, <code>front()</code> throws <code>invalid_iterator.214</code> for an empty array, an empty object, a null value, or a discarded view.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below reads only the earliest entry of a build log with <code>front()</code>, without materializing the rest of the (possibly long) log.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// the build log of a running job; front() reads just the earliest event</span>
|
||||
<span class=w> </span><span class=c1>// without materializing the (possibly long) rest of the log</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>log</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>["queued", "started", "compiling", "linking", "done"]</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>log</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>front</span><span class=p>().</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// an empty log -- front() throws instead of the undefined behavior</span>
|
||||
<span class=w> </span><span class=c1>// basic_json::front() has for an empty array</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>empty_log</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>try</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=kt>void</span><span class=o>></span><span class=p>(</span><span class=n>empty_log</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>front</span><span class=p>());</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=k>catch</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>invalid_iterator</span><span class=o>&</span><span class=w> </span><span class=n>e</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=s2>"queued"</span>
|
||||
<span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.i</span><span class=kc>n</span><span class=err>valid_i</span><span class=kc>terat</span><span class=err>or.</span><span class=mi>214</span><span class=p>]</span><span class=w> </span><span class=err>ca</span><span class=kc>nn</span><span class=err>o</span><span class=kc>t</span><span class=w> </span><span class=err>ge</span><span class=kc>t</span><span class=w> </span><span class=err>value</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../back/ >back</a> - access the last element</li> <li><a href=../../basic_json/front/ ><code>BasicJsonType::front</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-socialLine truncated
|
||||
@@ -0,0 +1,78 @@
|
||||
# nlohmann::basic_json_view::front
|
||||
|
||||
```
|
||||
basic_json_view front() const;
|
||||
```
|
||||
|
||||
Returns the first element of an array, the first member value of an object, or the value itself if it is primitive (as for [`BasicJsonType::front()`](https://json.nlohmann.me/api/basic_json/front/index.md), a primitive value is a range of one element).
|
||||
|
||||
## Return value
|
||||
|
||||
The first element or member value. For a primitive value (number, string, boolean), the value itself.
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`invalid_iterator.214`](https://json.nlohmann.me/home/exceptions/#jsonexceptioninvalid_iterator214) if the view is [null](https://json.nlohmann.me/api/basic_json_view/is_null/index.md) or [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md), or if it is an empty array or object.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](https://json.nlohmann.me/api/macros/json_diagnostics/index.md) path: the view has no `BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with `JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
Unlike [`BasicJsonType::front()`](https://json.nlohmann.me/api/basic_json/front/index.md), which has undefined behavior for an empty array or object, `front()` throws `invalid_iterator.214` for an empty array, an empty object, a null value, or a discarded view.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below reads only the earliest entry of a build log with `front()`, without materializing the rest of the (possibly long) log.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// the build log of a running job; front() reads just the earliest event
|
||||
// without materializing the (possibly long) rest of the log
|
||||
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
|
||||
std::cout << log.root().front().materialize().dump() << '\n';
|
||||
|
||||
// an empty log -- front() throws instead of the undefined behavior
|
||||
// basic_json::front() has for an empty array
|
||||
json_document empty_log = json_document::parse("[]");
|
||||
try
|
||||
{
|
||||
static_cast<void>(empty_log.root().front());
|
||||
}
|
||||
catch (const nlohmann::json::invalid_iterator& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
"queued"
|
||||
[json.exception.invalid_iterator.214] cannot get value
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [back](https://json.nlohmann.me/api/basic_json_view/back/index.md) - access the last element
|
||||
- [`BasicJsonType::front`](https://json.nlohmann.me/api/basic_json/front/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,191 @@
|
||||
# <small>nlohmann::basic_json_view::</small>get
|
||||
|
||||
```cpp
|
||||
template<typename T>
|
||||
T get() const;
|
||||
```
|
||||
|
||||
Converts the value to `T`.
|
||||
|
||||
For the types below, the conversion works directly on the flat index -- no `BasicJsonType` value is built for it:
|
||||
|
||||
- `#!cpp bool`
|
||||
- arithmetic types other than `#!cpp bool` (for a boolean, `#!cpp 0`/`#!cpp 1`, exactly as for
|
||||
[`BasicJsonType::get<T>()`](../basic_json/get.md))
|
||||
- `#!cpp std::nullptr_t`
|
||||
- `#!cpp std::basic_string<char, Traits, Alloc>` (including `string_t`) -- a copy of the string
|
||||
- [`string_view_t`](index.md#member-types) -- **no copy**: the returned view points into the document's
|
||||
[`source()`](../basic_json_document/source.md) text, or, for a string that contains escape sequences, into the
|
||||
document's own buffer of decoded strings (see [`get_string()`](get_string.md))
|
||||
- `BasicJsonType` -- equivalent to [`materialize()`](materialize.md)
|
||||
- `basic_json_view` -- returns `#!cpp *this`
|
||||
- `#!cpp std::vector<U, A>` -- from an array, element by element, each converted with `#!cpp get<U>()`; `#!cpp
|
||||
std::vector<basic_json_view>` keeps a view of every element instead of a value
|
||||
- `#!cpp std::map<K, V, C, A>` and `#!cpp std::unordered_map<K, V, H, E, A>`, if `K` is constructible from a `#!cpp
|
||||
(const char*, std::size_t)` pair -- from an object, member by member, each value converted with `#!cpp get<V>()`; with a repeated
|
||||
key, the *last* value is kept, as [`BasicJsonType::parse()`](../basic_json/parse.md) (and
|
||||
[`materialize()`](materialize.md)) does; `#!cpp std::map<std::string, basic_json_view>` keeps views of the members
|
||||
instead of values
|
||||
|
||||
Every other `T` -- `#!cpp std::list`, `#!cpp std::pair`, `#!cpp std::array`, enumerations, user types with a
|
||||
`from_json()`, ... -- is converted by `#!cpp materialize().get<T>()`: the subtree is built into a real `BasicJsonType`
|
||||
value first (as [`BasicJsonType::parse()`](../basic_json/parse.md) would), and converted from there exactly as
|
||||
[`BasicJsonType::get<T>()`](../basic_json/get.md) would convert it.
|
||||
|
||||
## Template parameters
|
||||
|
||||
`T`
|
||||
: the type to convert the value to
|
||||
|
||||
## Return value
|
||||
|
||||
the value, converted to `T`
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
- For the directly-converted types listed above (other than `BasicJsonType` and `basic_json_view`, which never
|
||||
throw): throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if the value's type does not
|
||||
match `T` -- the same exception, with the same message, that [`BasicJsonType::get<T>()`](../basic_json/get.md)
|
||||
throws for the same JSON type and `T`.
|
||||
- For `#!cpp std::vector<U, A>`: throws `type_error.302` if the value is not an array; otherwise, whatever converting
|
||||
an element to `U` throws.
|
||||
- For `#!cpp std::map`/`#!cpp std::unordered_map`: throws `type_error.302` if the value is not an object; otherwise,
|
||||
whatever converting a member to the mapped type throws.
|
||||
- For every other `T`: whatever [`materialize().get<T>()`](../basic_json/get.md) throws -- typically `type_error.302`,
|
||||
or whatever a user-provided `from_json()` throws.
|
||||
|
||||
None of the exceptions thrown directly by this function (the first three bullets above) carry a
|
||||
[`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no `BasicJsonType` value to point at. An
|
||||
exception thrown while converting through `materialize()` (the last bullet) is different: it is thrown by a real
|
||||
`BasicJsonType` value, so it **does** carry a `JSON_DIAGNOSTICS` path if `BasicJsonType` was built with it enabled.
|
||||
That path is relative to the root of the view's subtree, because `materialize()` builds only the subtree (with parent
|
||||
pointers): it is empty if the failing value is the subtree's root itself, and it never is the path within the whole
|
||||
document.
|
||||
|
||||
## Complexity
|
||||
|
||||
- `#!cpp bool`, arithmetic types, `#!cpp std::nullptr_t`, [`string_view_t`](index.md#member-types), `basic_json_view`:
|
||||
constant.
|
||||
- `#!cpp std::basic_string<char, Traits, Alloc>`: constant, plus one allocation and a copy of the string's bytes.
|
||||
- `BasicJsonType`: linear in the size of the subtree, see [`materialize()`](materialize.md).
|
||||
- `#!cpp std::vector<U, A>`: linear in the number of elements, times the complexity of converting one element to `U`.
|
||||
- `#!cpp std::map`/`#!cpp std::unordered_map`: linear in the number of members for walking them, plus the container's
|
||||
own insertion cost per member (logarithmic for `#!cpp std::map`, amortized constant for `#!cpp
|
||||
std::unordered_map`), times the complexity of converting one member to the mapped type.
|
||||
- every other `T`: the cost of [`materialize()`](materialize.md) and of destroying the `BasicJsonType` value
|
||||
afterwards, plus that of [`BasicJsonType::get<T>()`](../basic_json/get.md).
|
||||
|
||||
## Notes
|
||||
|
||||
!!! info "Floating-point values"
|
||||
|
||||
A floating-point `T` is converted from the same digits the lexer would see during `#!cpp BasicJsonType::parse()`,
|
||||
using the same conversion, so the result is bit-for-bit identical to `#!cpp BasicJsonType::parse(text).get<T>()`
|
||||
for the same source text.
|
||||
|
||||
!!! info "Duplicate keys"
|
||||
|
||||
`#!cpp std::map`/`#!cpp std::unordered_map` conversions keep the *last* value of a repeated key, like
|
||||
[`materialize()`](materialize.md) and [`BasicJsonType::parse()`](../basic_json/parse.md) do. This is the opposite
|
||||
of [`operator[]`](operator[].md)/[`at`](at.md)/[`find`](find.md)/[`contains`](contains.md), which resolve to the
|
||||
*first* occurrence (see the [Notes on duplicate keys](operator[].md#notes)).
|
||||
|
||||
!!! info "No pointers, references, or implicit conversion"
|
||||
|
||||
Unlike `BasicJsonType`, `basic_json_view` has no stored value anywhere to hand out a pointer or a reference to, so
|
||||
it provides neither `#!cpp get_ptr()`, `#!cpp get_ref()`, nor `#!cpp operator ValueType()`.
|
||||
[`get_string()`](get_string.md) (equivalently, `#!cpp get<string_view_t>()`) is the zero-copy alternative for
|
||||
strings.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below reads typed fields straight into C++ variables, collects a view of every array element with
|
||||
`#!cpp get<std::vector<basic_json_view>>()` instead of a value, and converts a nested object into a user type
|
||||
through its `from_json()` -- which runs on a `BasicJsonType` value `materialize()` builds for just that one
|
||||
member.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
// address has no direct conversion in get<T>(), so get<address>() falls back
|
||||
// to materialize().get<address>() -- a real nlohmann::json value is built
|
||||
// for just this one member, and its own from_json() runs on that
|
||||
struct address
|
||||
{
|
||||
std::string city;
|
||||
int zip = 0;
|
||||
};
|
||||
|
||||
void from_json(const nlohmann::json& j, address& a)
|
||||
{
|
||||
j.at("city").get_to(a.city);
|
||||
j.at("zip").get_to(a.zip);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"(
|
||||
{
|
||||
"name": "Alice",
|
||||
"active": true,
|
||||
"orders": [1, 2, 3],
|
||||
"address": {"city": "Berlin", "zip": 10115}
|
||||
}
|
||||
)");
|
||||
const json_view customer = doc.root();
|
||||
|
||||
// read typed fields straight into C++ variables -- none of these build
|
||||
// a nlohmann::json value
|
||||
const std::string name = customer["name"].get<std::string>();
|
||||
const bool active = customer["active"].get<bool>();
|
||||
std::cout << name << (active ? " (active)" : " (inactive)") << '\n';
|
||||
|
||||
// std::vector<json_view> keeps views of the array elements instead of
|
||||
// copies of their values
|
||||
bool first = true;
|
||||
for (const json_view order : customer["orders"].get<std::vector<json_view>>())
|
||||
{
|
||||
std::cout << (first ? "" : " ") << order.get<int>();
|
||||
first = false;
|
||||
}
|
||||
std::cout << '\n';
|
||||
|
||||
// everything else goes through materialize()
|
||||
const address a = customer["address"].get<address>();
|
||||
std::cout << a.city << ' ' << a.zip << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
Alice (active)
|
||||
1 2 3
|
||||
Berlin 10115
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get_to](get_to.md) - convert and write into a passed value
|
||||
- [get_string](get_string.md) - the string, without a copy
|
||||
- [number_token](number_token.md) - a number's token text, without a copy
|
||||
- [materialize](materialize.md) - build the `BasicJsonType` value of this subtree
|
||||
- [`BasicJsonType::get`](../basic_json/get.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,61 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/get/" rel="canonical"><link href=../front/ rel=prev><link href=../get_string/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>get - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewget class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> get </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> Line truncated
|
||||
<span class=n>T</span><span class=w> </span><span class=n>get</span><span class=p>()</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <p>Converts the value to <code>T</code>.</p> <p>For the types below, the conversion works directly on the flat index -- no <code>BasicJsonType</code> value is built for it:</p> <ul> <li><code class=highlight><span class=kt>bool</span></code></li> <li>arithmetic types other than <code class=highlight><span class=kt>bool</span></code> (for a boolean, <code class=highlight><span class=mi>0</span></code>/<code class=highlight><span class=mi>1</span></code>, exactly as for <a href=../../basic_json/get/ ><code>BasicJsonType::get<T>()</code></a>)</li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>nullptr_t</span></code></li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>basic_string</span><span class=o><</span><span class=kt>char</span><span class=p>,</span><span class=w> </span><span class=n>Traits</span><span class=p>,</span><span class=w> </span><span class=n>Alloc</span><span class=o>></span></code> (including <code>string_t</code>) -- a copy of the string</li> <li><a href=../#member-types><code>string_view_t</code></a> -- <strong>no copy</strong>: the returned view points into the document's <a href=../../basic_json_document/source/ ><code>source()</code></a> text, or, for a string that contains escape sequences, into the document's own buffer of decoded strings (see <a href=../get_string/ ><code>get_string()</code></a>)</li> <li><code>BasicJsonType</code> -- equivalent to <a href=../materialize/ ><code>materialize()</code></a></li> <li><code>basic_json_view</code> -- returns <code class=highlight><span class=o>*</span><span class=k>this</span></code></li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o><</span><span class=n>U</span><span class=p>,</span><span class=w> </span><span class=n>A</span><span class=o>></span></code> -- from an array, element by element, each converted with <code class=highlight><span class=n>get</span><span class=o><</span><span class=n>U</span><span class=o>></span><span class=p>()</span></code>; <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o><</span><span class=n>basic_json_view</span><span class=o>></span></code> keeps a view of every element instead of a value</li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span><span class=o><</span><span class=n>K</span><span class=p>,</span><span class=w> </span><span class=n>V</span><span class=p>,</span><span class=w> </span><span class=n>C</span><span class=p>,</span><span class=w> </span><span class=n>A</span><span class=o>></span></code> and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>unordered_map</span><span class=o><</span><span class=n>K</span><span class=p>,</span><span class=w> </span><span class=n>V</span><span class=p>,</span><span class=w> </span><span class=n>H</span><span class=p>,</span><span class=w> </span><span class=n>E</span><span class=p>,</span><span class=w> </span><span class=n>A</span><span class=o>></span></code>, if <code>K</code> is constructible from a <code class=highlight><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=p>,</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=p>)</span></code> pair -- from an object, member by member, each value converted with <code class=highlight><span class=n>get</span><span class=o><</span><span class=n>V</span><span class=o>></span><span class=p>()</span></code>; with a repeated key, the <em>last</em> value is kept, as <a href=../../basic_json/parse/ ><code>BasicJsonType::parse()</code></a> (and <a href=../materialize/ ><code>materialize()</code></a>) does; <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span><span class=o><</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=p>,</span><span class=w> </span><span class=n>basic_json_view</span><span class=o>></span></code> keeps views of the members instead of values</li> </ul> <p>Every other <code>T</code> -- <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>list</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>pair</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>array</span></code>, enumerations, user types with a <code>from_json()</code>, ... -- is converted by <code class=highlight><span class=n>materialize</span><span class=p>().</span><span class=n>get</span><span class=o><</span><span class=n>T</span><span class=o>></span><span class=p>()</span></code>: the subtree is built into a real <code>BaLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><string></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><vector></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=c1>// address has no direct conversion in get<T>(), so get<address>() falls back</span>
|
||||
<span class=c1>// to materialize().get<address>() -- a real nlohmann::json value is built</span>
|
||||
<span class=c1>// for just this one member, and its own from_json() runs on that</span>
|
||||
<span class=k>struct</span><span class=w> </span><span class=nc>address</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>city</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=kt>int</span><span class=w> </span><span class=n>zip</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span>
|
||||
<span class=p>};</span>
|
||||
|
||||
<span class=kt>void</span><span class=w> </span><span class=nf>from_json</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>&</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=n>address</span><span class=o>&</span><span class=w> </span><span class=n>a</span><span class=p>)</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"city"</span><span class=p>).</span><span class=n>get_to</span><span class=p>(</span><span class=n>a</span><span class=p>.</span><span class=n>city</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"zip"</span><span class=p>).</span><span class=n>get_to</span><span class=p>(</span><span class=n>a</span><span class=p>.</span><span class=n>zip</span><span class=p>);</span>
|
||||
<span class=p>}</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> {</span>
|
||||
<span class=s> "name": "Alice",</span>
|
||||
<span class=s> "active": true,</span>
|
||||
<span class=s> "orders": [1, 2, 3],</span>
|
||||
<span class=s> "address": {"city": "Berlin", "zip": 10115}</span>
|
||||
<span class=s> }</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>customer</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// read typed fields straight into C++ variables -- none of these build</span>
|
||||
<span class=w> </span><span class=c1>// a nlohmann::json value</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>name</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>customer</span><span class=p>[</span><span class=s>"name"</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=o>></span><span class=p>();</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>active</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>customer</span><span class=p>[</span><span class=s>"active"</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=kt>bool</span><span class=o>></span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>name</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>active</span><span class=w> </span><span class=o>?</span><span class=w> </span><span class=s>" (active)"</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=s>" (inactive)"</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// std::vector<json_view> keeps views of the array elements instead of</span>
|
||||
<span class=w> </span><span class=c1>// copies of their values</span>
|
||||
<span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>first</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>order</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>customer</span><span class=p>[</span><span class=s>"orders"</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o><</span><span class=n>json_view</span><span class=o>>></span><span class=p>())</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>first</span><span class=w> </span><span class=o>?</span><span class=w> </span><span class=s>""</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=s>" "</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>.</span><span class=n>get</span><span class=o><</span><span class=kt>int</span><span class=o>></span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>first</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// everything else goes through materialize()</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>address</span><span class=w> </span><span class=n>a</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>customer</span><span class=p>[</span><span class=s>"address"</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=n>address</span><span class=o>></span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>a</span><span class=p>.</span><span class=n>city</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>a</span><span class=p>.</span><span class=n>zip</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=err>Alice</span><span class=w> </span><span class=err>(ac</span><span class=kc>t</span><span class=err>ive)</span>
|
||||
<span class=mi>1</span><span class=w> </span><span class=mi>2</span><span class=w> </span><span class=mi>3</span>
|
||||
<span class=err>Berli</span><span class=kc>n</span><span class=w> </span><span class=mi>10115</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../get_to/ >get_to</a> - convert and write into a passed value</li> <li><a href=../get_string/ >get_string</a> - the string, without a copy</li> <li><a href=../number_token/ >number_token</a> - a number's token text, without a copy</li> <li><a href=../materialize/ >materialize</a> - build the <code>BasicJsonType</code> value of this subtree</li> <li><a href=../../basic_json/get/ ><code>BasicJsonType::get</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-7Line truncated
|
||||
@@ -0,0 +1,150 @@
|
||||
# nlohmann::basic_json_view::get
|
||||
|
||||
```
|
||||
template<typename T>
|
||||
T get() const;
|
||||
```
|
||||
|
||||
Converts the value to `T`.
|
||||
|
||||
For the types below, the conversion works directly on the flat index -- no `BasicJsonType` value is built for it:
|
||||
|
||||
- `bool`
|
||||
- arithmetic types other than `bool` (for a boolean, `0`/`1`, exactly as for [`BasicJsonType::get<T>()`](https://json.nlohmann.me/api/basic_json/get/index.md))
|
||||
- `std::nullptr_t`
|
||||
- `std::basic_string<char, Traits, Alloc>` (including `string_t`) -- a copy of the string
|
||||
- [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types) -- **no copy**: the returned view points into the document's [`source()`](https://json.nlohmann.me/api/basic_json_document/source/index.md) text, or, for a string that contains escape sequences, into the document's own buffer of decoded strings (see [`get_string()`](https://json.nlohmann.me/api/basic_json_view/get_string/index.md))
|
||||
- `BasicJsonType` -- equivalent to [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md)
|
||||
- `basic_json_view` -- returns `*this`
|
||||
- `std::vector<U, A>` -- from an array, element by element, each converted with `get<U>()`; `std::vector<basic_json_view>` keeps a view of every element instead of a value
|
||||
- `std::map<K, V, C, A>` and `std::unordered_map<K, V, H, E, A>`, if `K` is constructible from a `(const char*, std::size_t)` pair -- from an object, member by member, each value converted with `get<V>()`; with a repeated key, the *last* value is kept, as [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) (and [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md)) does; `std::map<std::string, basic_json_view>` keeps views of the members instead of values
|
||||
|
||||
Every other `T` -- `std::list`, `std::pair`, `std::array`, enumerations, user types with a `from_json()`, ... -- is converted by `materialize().get<T>()`: the subtree is built into a real `BasicJsonType` value first (as [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) would), and converted from there exactly as [`BasicJsonType::get<T>()`](https://json.nlohmann.me/api/basic_json/get/index.md) would convert it.
|
||||
|
||||
## Template parameters
|
||||
|
||||
`T` : the type to convert the value to
|
||||
|
||||
## Return value
|
||||
|
||||
the value, converted to `T`
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
- For the directly-converted types listed above (other than `BasicJsonType` and `basic_json_view`, which never throw): throws [`type_error.302`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error302) if the value's type does not match `T` -- the same exception, with the same message, that [`BasicJsonType::get<T>()`](https://json.nlohmann.me/api/basic_json/get/index.md) throws for the same JSON type and `T`.
|
||||
- For `std::vector<U, A>`: throws `type_error.302` if the value is not an array; otherwise, whatever converting an element to `U` throws.
|
||||
- For `std::map`/`std::unordered_map`: throws `type_error.302` if the value is not an object; otherwise, whatever converting a member to the mapped type throws.
|
||||
- For every other `T`: whatever [`materialize().get<T>()`](https://json.nlohmann.me/api/basic_json/get/index.md) throws -- typically `type_error.302`, or whatever a user-provided `from_json()` throws.
|
||||
|
||||
None of the exceptions thrown directly by this function (the first three bullets above) carry a [`JSON_DIAGNOSTICS`](https://json.nlohmann.me/api/macros/json_diagnostics/index.md) path: the view has no `BasicJsonType` value to point at. An exception thrown while converting through `materialize()` (the last bullet) is different: it is thrown by a real `BasicJsonType` value, so it **does** carry a `JSON_DIAGNOSTICS` path if `BasicJsonType` was built with it enabled. That path is relative to the root of the view's subtree, because `materialize()` builds only the subtree (with parent pointers): it is empty if the failing value is the subtree's root itself, and it never is the path within the whole document.
|
||||
|
||||
## Complexity
|
||||
|
||||
- `bool`, arithmetic types, `std::nullptr_t`, [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types), `basic_json_view`: constant.
|
||||
- `std::basic_string<char, Traits, Alloc>`: constant, plus one allocation and a copy of the string's bytes.
|
||||
- `BasicJsonType`: linear in the size of the subtree, see [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md).
|
||||
- `std::vector<U, A>`: linear in the number of elements, times the complexity of converting one element to `U`.
|
||||
- `std::map`/`std::unordered_map`: linear in the number of members for walking them, plus the container's own insertion cost per member (logarithmic for `std::map`, amortized constant for `std::unordered_map`), times the complexity of converting one member to the mapped type.
|
||||
- every other `T`: the cost of [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) and of destroying the `BasicJsonType` value afterwards, plus that of [`BasicJsonType::get<T>()`](https://json.nlohmann.me/api/basic_json/get/index.md).
|
||||
|
||||
## Notes
|
||||
|
||||
Floating-point values
|
||||
|
||||
A floating-point `T` is converted from the same digits the lexer would see during `BasicJsonType::parse()`, using the same conversion, so the result is bit-for-bit identical to `BasicJsonType::parse(text).get<T>()` for the same source text.
|
||||
|
||||
Duplicate keys
|
||||
|
||||
`std::map`/`std::unordered_map` conversions keep the *last* value of a repeated key, like [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) and [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) do. This is the opposite of [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md)/[`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md)/[`find`](https://json.nlohmann.me/api/basic_json_view/find/index.md)/[`contains`](https://json.nlohmann.me/api/basic_json_view/contains/index.md), which resolve to the *first* occurrence (see the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes)).
|
||||
|
||||
No pointers, references, or implicit conversion
|
||||
|
||||
Unlike `BasicJsonType`, `basic_json_view` has no stored value anywhere to hand out a pointer or a reference to, so it provides neither `get_ptr()`, `get_ref()`, nor `operator ValueType()`. [`get_string()`](https://json.nlohmann.me/api/basic_json_view/get_string/index.md) (equivalently, `get<string_view_t>()`) is the zero-copy alternative for strings.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below reads typed fields straight into C++ variables, collects a view of every array element with `get<std::vector<basic_json_view>>()` instead of a value, and converts a nested object into a user type through its `from_json()` -- which runs on a `BasicJsonType` value `materialize()` builds for just that one member.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
// address has no direct conversion in get<T>(), so get<address>() falls back
|
||||
// to materialize().get<address>() -- a real nlohmann::json value is built
|
||||
// for just this one member, and its own from_json() runs on that
|
||||
struct address
|
||||
{
|
||||
std::string city;
|
||||
int zip = 0;
|
||||
};
|
||||
|
||||
void from_json(const nlohmann::json& j, address& a)
|
||||
{
|
||||
j.at("city").get_to(a.city);
|
||||
j.at("zip").get_to(a.zip);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"(
|
||||
{
|
||||
"name": "Alice",
|
||||
"active": true,
|
||||
"orders": [1, 2, 3],
|
||||
"address": {"city": "Berlin", "zip": 10115}
|
||||
}
|
||||
)");
|
||||
const json_view customer = doc.root();
|
||||
|
||||
// read typed fields straight into C++ variables -- none of these build
|
||||
// a nlohmann::json value
|
||||
const std::string name = customer["name"].get<std::string>();
|
||||
const bool active = customer["active"].get<bool>();
|
||||
std::cout << name << (active ? " (active)" : " (inactive)") << '\n';
|
||||
|
||||
// std::vector<json_view> keeps views of the array elements instead of
|
||||
// copies of their values
|
||||
bool first = true;
|
||||
for (const json_view order : customer["orders"].get<std::vector<json_view>>())
|
||||
{
|
||||
std::cout << (first ? "" : " ") << order.get<int>();
|
||||
first = false;
|
||||
}
|
||||
std::cout << '\n';
|
||||
|
||||
// everything else goes through materialize()
|
||||
const address a = customer["address"].get<address>();
|
||||
std::cout << a.city << ' ' << a.zip << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
Alice (active)
|
||||
1 2 3
|
||||
Berlin 10115
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get_to](https://json.nlohmann.me/api/basic_json_view/get_to/index.md) - convert and write into a passed value
|
||||
- [get_string](https://json.nlohmann.me/api/basic_json_view/get_string/index.md) - the string, without a copy
|
||||
- [number_token](https://json.nlohmann.me/api/basic_json_view/number_token/index.md) - a number's token text, without a copy
|
||||
- [materialize](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) - build the `BasicJsonType` value of this subtree
|
||||
- [`BasicJsonType::get`](https://json.nlohmann.me/api/basic_json/get/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,110 @@
|
||||
# <small>nlohmann::basic_json_view::</small>get_string
|
||||
|
||||
```cpp
|
||||
string_view_t get_string() const;
|
||||
```
|
||||
|
||||
Returns the string value as a [`string_view_t`](index.md#member-types), without copying it.
|
||||
|
||||
## Return value
|
||||
|
||||
The string, as a [`string_view_t`](index.md#member-types) that points either into the document's
|
||||
[`source()`](../basic_json_document/source.md) text (a string with no escape sequences), or into the document's own
|
||||
buffer of decoded strings (a string that contains escape sequences, such as `#!json "\n"` or `#!json "\u00e9"`, which
|
||||
had to be decoded once when the document was parsed).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if the value is not a string; example:
|
||||
`"type must be string, but is array"`.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no
|
||||
`BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with
|
||||
`JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`basic_json_view` has no `BasicJsonType` value stored anywhere, so unlike `BasicJsonType`, it has no `get_ref()` to
|
||||
hand out a reference to a stored `string_t`. `get_string()` (equivalently, [`get<string_view_t>()`](get.md)) is the
|
||||
zero-copy alternative: [`BasicJsonType::get_ref<const string_t&>()`](../basic_json/get_ref.md) is its closest
|
||||
counterpart, except that it returns a view instead of a reference to a value that must already exist.
|
||||
|
||||
`get_string()` borrows the string: nothing is copied, whereas [`get<std::string>()`](get.md) copies it. For a borrowed
|
||||
document, the returned [`string_view_t`](index.md#member-types) is valid as long as the caller's buffer; for an owned
|
||||
one, as long as the document.
|
||||
|
||||
The returned [`string_view_t`](index.md#member-types) is valid exactly as long as the view that produced it -- see the
|
||||
[validity rules](index.md) of `basic_json_view` -- and, for a string with no escapes, for as long as the document's
|
||||
source text, which for a borrowed document must remain valid **and unmodified** for as long as the document exists,
|
||||
since floating-point values and unescaped strings are read from the source lazily.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below pulls one field out of a JSON text that stands in for a large API response, and shows that no
|
||||
`#!cpp std::string` was allocated for it: the returned view still points inside the original buffer. A field that
|
||||
contains an escape sequence cannot point into the original text -- it was decoded once into the document's own
|
||||
buffer instead -- but still avoids a per-field allocation.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a "large response" stand-in: only the "id" field is ever read out of it
|
||||
const std::string text =
|
||||
R"({"id": "8f14e45f-ceea-467e-bb92-963f5e3c7a08", "note": "created via API\n", "payload": "..."})";
|
||||
const json_document doc = json_document::parse(text);
|
||||
const json_view response = doc.root();
|
||||
|
||||
const json_view::string_view_t id = response["id"].get_string();
|
||||
std::cout << id << '\n';
|
||||
|
||||
// no std::string was allocated for "id": its bytes still live inside
|
||||
// the original buffer, so id's data lies inside [text.data(),
|
||||
// text.data() + text.size())
|
||||
const bool id_in_source = id.data() >= text.data() && id.data() + id.size() <= text.data() + text.size();
|
||||
std::cout << std::boolalpha << id_in_source << '\n';
|
||||
|
||||
// "note" contains an escape sequence ('\n'), so it was decoded once
|
||||
// into the document's own buffer -- get_string() still avoids a copy
|
||||
// into a new std::string, but the bytes no longer live inside "text"
|
||||
const json_view::string_view_t note = response["note"].get_string();
|
||||
const bool note_in_source = note.data() >= text.data() && note.data() + note.size() <= text.data() + text.size();
|
||||
std::cout << std::boolalpha << note_in_source << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
8f14e45f-ceea-467e-bb92-963f5e3c7a08
|
||||
true
|
||||
false
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get](get.md) - convert the value to a given type (`#!cpp get<string_view_t>()` is equivalent to this function)
|
||||
- [number_token](number_token.md) - a number's token text, without a copy
|
||||
- [`BasicJsonType::get_ref`](../basic_json/get_ref.md) - the closest counterpart of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,36 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/get_string/" rel="canonical"><link href=../get/ rel=prev><link href=../get_to/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>get_string - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewget_string class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> get_string </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.Line truncated
|
||||
</code></pre></div> <p>Returns the string value as a <a href=../#member-types><code>string_view_t</code></a>, without copying it.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>The string, as a <a href=../#member-types><code>string_view_t</code></a> that points either into the document's <a href=../../basic_json_document/source/ ><code>source()</code></a> text (a string with no escape sequences), or into the document's own buffer of decoded strings (a string that contains escape sequences, such as <code class=highlight><span class=s2>"\n"</span></code> or <code class=highlight><span class=s2>"\u00e9"</span></code>, which had to be decoded once when the document was parsed).</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: there are no changes to the view or the document it refers to.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error302><code>type_error.302</code></a> if the value is not a string; example: <code>"type must be string, but is array"</code>.</p> <p>This exception does not carry a <a href=../../macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> path: the view has no <code>BasicJsonType</code> value to point at, so the exception is created without one, even if <code>BasicJsonType</code> was built with <code>JSON_DIAGNOSTICS</code> enabled.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p><code>basic_json_view</code> has no <code>BasicJsonType</code> value stored anywhere, so unlike <code>BasicJsonType</code>, it has no <code>get_ref()</code> to hand out a reference to a stored <code>string_t</code>. <code>get_string()</code> (equivalently, <a href=../get/ ><code>get<string_view_t>()</code></a>) is the zero-copy alternative: <a href=../../basic_json/get_ref/ ><code>BasicJsonType::get_ref<const string_t&>()</code></a> is its closest counterpart, except that it returns a view instead of a reference to a value that must already exist.</p> <p><code>get_string()</code> borrows the string: nothing is copied, whereas <a href=../get/ ><code>get<std::string>()</code></a> copies it. For a borrowed document, the returned <a href=../#member-types><code>string_view_t</code></a> is valid as long as the caller's buffer; for an owned one, as long as the document.</p> <p>The returned <a href=../#member-types><code>string_view_t</code></a> is valid exactly as long as the view that produced it -- see the <a href=../ >validity rules</a> of <code>basic_json_view</code> -- and, for a string with no escapes, for as long as the document's source text, which for a borrowed document must remain valid <strong>and unmodified</strong> for as long as the document exists, since floating-point values and unescaped strings are read from the source lazily.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below pulls one field out of a <abbr title="JavaScript Object Notation">JSON</abbr> text that stands in for a large <abbr title="Application Programming Interface">API</abbr> response, and shows that no <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code> was allocated for it: the returned view still points inside the original buffer. A field that contains an escape sequence cannot point into the original text -- it was decoded once into the document's own buffer instead -- but still avoids a per-field allocation.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><string></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a "large response" stand-in: only the "id" field is ever read out of it</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>text</span><span class=w> </span><span class=o>=</span>
|
||||
<span class=w> </span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"id": "8f14e45f-ceea-467e-bb92-963f5e3c7a08", "note": "created via API\n", "payload": "..."}</span><span class=dl>)</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>text</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>response</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=o>::</span><span class=n>string_view_t</span><span class=w> </span><span class=n>id</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>response</span><span class=p>[</span><span class=s>"id"</span><span class=p>].</span><span class=n>get_string</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// no std::string was allocated for "id": its bytes still live inside</span>
|
||||
<span class=w> </span><span class=c1>// the original buffer, so id's data lies inside [text.data(),</span>
|
||||
<span class=w> </span><span class=c1>// text.data() + text.size())</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>id_in_source</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>id</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>>=</span><span class=w> </span><span class=n>text</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>&&</span><span class=w> </span><span class=n>id</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=n>id</span><span class=p>.</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><=</span><span class=w> </span><span class=n>text</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=n>text</span><span class=p>.</span><span class=n>size</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>id_in_source</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// "note" contains an escape sequence ('\n'), so it was decoded once</span>
|
||||
<span class=w> </span><span class=c1>// into the document's own buffer -- get_string() still avoids a copy</span>
|
||||
<span class=w> </span><span class=c1>// into a new std::string, but the bytes no longer live inside "text"</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=o>::</span><span class=n>string_view_t</span><span class=w> </span><span class=n>note</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>response</span><span class=p>[</span><span class=s>"note"</span><span class=p>].</span><span class=n>get_string</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>note_in_source</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>note</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>>=</span><span class=w> </span><span class=n>text</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>&&</span><span class=w> </span><span class=n>note</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=n>note</span><span class=p>.</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><=</span><span class=w> </span><span class=n>text</span><span class=p>.</span><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=n>text</span><span class=p>.</span><span class=n>size</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>note_in_source</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=mi>8</span><span class=kc>f</span><span class=mf>14e45</span><span class=kc>f</span><span class=mi>-</span><span class=err>ceea</span><span class=mf>-467e-</span><span class=err>bb</span><span class=mi>92-963</span><span class=kc>f</span><span class=mf>5e3</span><span class=err>c</span><span class=mi>7</span><span class=err>a</span><span class=mi>08</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>false</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../get/ >get</a> - convert the value to a given type (<code class=highlight><span class=n>get</span><span class=o><</span><span class=n>string_view_t</span><span class=o>></span><span class=p>()</span></code> is equivalent to this function)</li> <li><a href=../number_token/ >number_token</a> - a number's token text, without a copy</li> <li><a href=../../basic_json/get_ref/ ><code>BasicJsonType::get_ref</code></a> - the closest counterpart of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-Line truncated
|
||||
@@ -0,0 +1,91 @@
|
||||
# nlohmann::basic_json_view::get_string
|
||||
|
||||
```
|
||||
string_view_t get_string() const;
|
||||
```
|
||||
|
||||
Returns the string value as a [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types), without copying it.
|
||||
|
||||
## Return value
|
||||
|
||||
The string, as a [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types) that points either into the document's [`source()`](https://json.nlohmann.me/api/basic_json_document/source/index.md) text (a string with no escape sequences), or into the document's own buffer of decoded strings (a string that contains escape sequences, such as `"\n"` or `"\u00e9"`, which had to be decoded once when the document was parsed).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`type_error.302`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error302) if the value is not a string; example: `"type must be string, but is array"`.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](https://json.nlohmann.me/api/macros/json_diagnostics/index.md) path: the view has no `BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with `JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`basic_json_view` has no `BasicJsonType` value stored anywhere, so unlike `BasicJsonType`, it has no `get_ref()` to hand out a reference to a stored `string_t`. `get_string()` (equivalently, [`get<string_view_t>()`](https://json.nlohmann.me/api/basic_json_view/get/index.md)) is the zero-copy alternative: [`BasicJsonType::get_ref<const string_t&>()`](https://json.nlohmann.me/api/basic_json/get_ref/index.md) is its closest counterpart, except that it returns a view instead of a reference to a value that must already exist.
|
||||
|
||||
`get_string()` borrows the string: nothing is copied, whereas [`get<std::string>()`](https://json.nlohmann.me/api/basic_json_view/get/index.md) copies it. For a borrowed document, the returned [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types) is valid as long as the caller's buffer; for an owned one, as long as the document.
|
||||
|
||||
The returned [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types) is valid exactly as long as the view that produced it -- see the [validity rules](https://json.nlohmann.me/api/basic_json_view/index.md) of `basic_json_view` -- and, for a string with no escapes, for as long as the document's source text, which for a borrowed document must remain valid **and unmodified** for as long as the document exists, since floating-point values and unescaped strings are read from the source lazily.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below pulls one field out of a JSON text that stands in for a large API response, and shows that no `std::string` was allocated for it: the returned view still points inside the original buffer. A field that contains an escape sequence cannot point into the original text -- it was decoded once into the document's own buffer instead -- but still avoids a per-field allocation.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a "large response" stand-in: only the "id" field is ever read out of it
|
||||
const std::string text =
|
||||
R"({"id": "8f14e45f-ceea-467e-bb92-963f5e3c7a08", "note": "created via API\n", "payload": "..."})";
|
||||
const json_document doc = json_document::parse(text);
|
||||
const json_view response = doc.root();
|
||||
|
||||
const json_view::string_view_t id = response["id"].get_string();
|
||||
std::cout << id << '\n';
|
||||
|
||||
// no std::string was allocated for "id": its bytes still live inside
|
||||
// the original buffer, so id's data lies inside [text.data(),
|
||||
// text.data() + text.size())
|
||||
const bool id_in_source = id.data() >= text.data() && id.data() + id.size() <= text.data() + text.size();
|
||||
std::cout << std::boolalpha << id_in_source << '\n';
|
||||
|
||||
// "note" contains an escape sequence ('\n'), so it was decoded once
|
||||
// into the document's own buffer -- get_string() still avoids a copy
|
||||
// into a new std::string, but the bytes no longer live inside "text"
|
||||
const json_view::string_view_t note = response["note"].get_string();
|
||||
const bool note_in_source = note.data() >= text.data() && note.data() + note.size() <= text.data() + text.size();
|
||||
std::cout << std::boolalpha << note_in_source << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
8f14e45f-ceea-467e-bb92-963f5e3c7a08
|
||||
true
|
||||
false
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get](https://json.nlohmann.me/api/basic_json_view/get/index.md) - convert the value to a given type (`get<string_view_t>()` is equivalent to this function)
|
||||
- [number_token](https://json.nlohmann.me/api/basic_json_view/number_token/index.md) - a number's token text, without a copy
|
||||
- [`BasicJsonType::get_ref`](https://json.nlohmann.me/api/basic_json/get_ref/index.md) - the closest counterpart of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,110 @@
|
||||
# <small>nlohmann::basic_json_view::</small>get_to
|
||||
|
||||
```cpp
|
||||
template<typename T>
|
||||
T& get_to(T& v) const;
|
||||
```
|
||||
|
||||
Converts the value to `T` and stores it in `v`, in one of two ways:
|
||||
|
||||
- For the types that [`get<T>()`](get.md) converts directly -- arithmetic types, `#!cpp bool`,
|
||||
`#!cpp std::nullptr_t`, [`string_view_t`](index.md#member-types), `#!cpp std::basic_string`, `BasicJsonType`,
|
||||
`basic_json_view`, `#!cpp std::vector`, and `#!cpp std::map`/`#!cpp std::unordered_map` with string keys -- `v` is
|
||||
assigned: `#!cpp v = get<T>()`.
|
||||
- For every other type, the subtree is built into a `BasicJsonType` value and `#!cpp materialize().get_to(v)` is
|
||||
called, i.e. `from_json()` is invoked with the existing `v`, exactly as
|
||||
[`BasicJsonType::get_to()`](../basic_json/get_to.md) does.
|
||||
|
||||
## Template parameters
|
||||
|
||||
`T`
|
||||
: the type to convert the value to
|
||||
|
||||
## Parameters
|
||||
|
||||
`v` (out)
|
||||
: the variable to store the converted value in
|
||||
|
||||
## Return value
|
||||
|
||||
`v`, allowing calls to chain
|
||||
|
||||
## Exception safety
|
||||
|
||||
- For the directly converted types: strong exception safety. If an exception is thrown, `v` is not modified.
|
||||
- For every other type: as for [`BasicJsonType::get_to()`](../basic_json/get_to.md). `materialize()` throws before `v`
|
||||
is touched, but a `from_json()` that throws may leave `v` partly modified.
|
||||
|
||||
## Exceptions
|
||||
|
||||
- For the directly converted types: whatever [`get<T>()`](get.md) throws for the same value and `T`.
|
||||
- For every other type: whatever [`materialize()`](materialize.md) and
|
||||
[`BasicJsonType::get_to()`](../basic_json/get_to.md) throw.
|
||||
|
||||
## Complexity
|
||||
|
||||
- For the directly converted types: whatever [`get<T>()`](get.md) has for the same `T`.
|
||||
- For every other type: that of [`materialize()`](materialize.md), plus that of
|
||||
[`BasicJsonType::get_to()`](../basic_json/get_to.md).
|
||||
|
||||
## Notes
|
||||
|
||||
The difference between the two ways matters for a `from_json()` that sets only the fields present in the JSON: since
|
||||
it runs on the existing `v`, the other fields of `v` keep their values, whereas assigning `get<T>()` would replace
|
||||
them with those of a default-constructed `T`.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below reads several fields of a service configuration directly into existing variables, then uses
|
||||
the returned reference to fold the `#!cpp host`/`#!cpp port` pair into a single string in the same expression.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"host": "db.example.com", "port": 5432, "ssl": true})");
|
||||
const json_view config = doc.root();
|
||||
|
||||
// get_to() writes directly into existing variables -- handy for filling
|
||||
// in the members of a struct one field at a time, without an
|
||||
// intermediate value from get<T>() for each one
|
||||
std::string host;
|
||||
int port = 0;
|
||||
bool ssl = false;
|
||||
config["host"].get_to(host);
|
||||
config["port"].get_to(port);
|
||||
config["ssl"].get_to(ssl);
|
||||
std::cout << host << ':' << port << (ssl ? " (tls)" : "") << '\n';
|
||||
|
||||
// the return value is a reference to the argument, so a call can be
|
||||
// used directly in a larger expression
|
||||
std::string other_host;
|
||||
std::cout << config["host"].get_to(other_host).size() << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
db.example.com:5432 (tls)
|
||||
14
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get](get.md) - convert the value to a given type
|
||||
- [`BasicJsonType::get_to`](../basic_json/get_to.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,33 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/get_to/" rel="canonical"><link href=../get_string/ rel=prev><link href=../is_array/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>get_to - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewget_to class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> get_to </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/><Line truncated
|
||||
<span class=n>T</span><span class=o>&</span><span class=w> </span><span class=n>get_to</span><span class=p>(</span><span class=n>T</span><span class=o>&</span><span class=w> </span><span class=n>v</span><span class=p>)</span><span class=w> </span><span class=k>const</span><span class=p>;</span>
|
||||
</code></pre></div> <p>Converts the value to <code>T</code> and stores it in <code>v</code>, in one of two ways:</p> <ul> <li>For the types that <a href=../get/ ><code>get<T>()</code></a> converts directly -- arithmetic types, <code class=highlight><span class=kt>bool</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>nullptr_t</span></code>, <a href=../#member-types><code>string_view_t</code></a>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>basic_string</span></code>, <code>BasicJsonType</code>, <code>basic_json_view</code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span></code>, and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span></code>/<code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>unordered_map</span></code> with string keys -- <code>v</code> is assigned: <code class=highlight><span class=n>v</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>get</span><span class=o><</span><span class=n>T</span><span class=o>></span><span class=p>()</span></code>.</li> <li>For every other type, the subtree is built into a <code>BasicJsonType</code> value and <code class=highlight><span class=n>materialize</span><span class=p>().</span><span class=n>get_to</span><span class=p>(</span><span class=n>v</span><span class=p>)</span></code> is called, i.e. <code>from_json()</code> is invoked with the existing <code>v</code>, exactly as <a href=../../basic_json/get_to/ ><code>BasicJsonType::get_to()</code></a> does.</li> </ul> <h2 id=template-parameters>Template parameters<a class=headerlink href=#template-parameters title="Permanent link">¶</a></h2> <dl> <dt><code>T</code></dt> <dd>the type to convert the value to</dd> </dl> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>v</code> (out)</dt> <dd>the variable to store the converted value in</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code>v</code>, allowing calls to chain</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <ul> <li>For the directly converted types: strong exception safety. If an exception is thrown, <code>v</code> is not modified.</li> <li>For every other type: as for <a href=../../basic_json/get_to/ ><code>BasicJsonType::get_to()</code></a>. <code>materialize()</code> throws before <code>v</code> is touched, but a <code>from_json()</code> that throws may leave <code>v</code> partly modified.</li> </ul> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>For the directly converted types: whatever <a href=../get/ ><code>get<T>()</code></a> throws for the same value and <code>T</code>.</li> <li>For every other type: whatever <a href=../materialize/ ><code>materialize()</code></a> and <a href=../../basic_json/get_to/ ><code>BasicJsonType::get_to()</code></a> throw.</li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <ul> <li>For the directly converted types: whatever <a href=../get/ ><code>get<T>()</code></a> has for the same <code>T</code>.</li> <li>For every other type: that of <a href=../materialize/ ><code>materialize()</code></a>, plus that of <a href=../../basic_json/get_to/ ><code>BasicJsonType::get_to()</code></a>.</li> </ul> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>The difference between the two ways matters for a <code>from_json()</code> that sets only the fields present in the <abbr title="JavaScript Object Notation">JSON</abbr>: since it runs on the existing <code>v</code>, the other fields of <code>v</code> keep their values, whereas assigning <code>get<T>()</code> would replace them with those of a default-constructed <code>T</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below reads several fields of a service configuration directly into existing variables, then uses the returned reference to fold the <code class=highlight><span class=n>host</span></code>/<code class=highlight><span class=n>port</span></code> pair into a single string in the same expression.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><string></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"host": "db.example.com", "port": 5432, "ssl": true}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>config</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// get_to() writes directly into existing variables -- handy for filling</span>
|
||||
<span class=w> </span><span class=c1>// in the members of a struct one field at a time, without an</span>
|
||||
<span class=w> </span><span class=c1>// intermediate value from get<T>() for each one</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>host</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=kt>int</span><span class=w> </span><span class=n>port</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>ssl</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>config</span><span class=p>[</span><span class=s>"host"</span><span class=p>].</span><span class=n>get_to</span><span class=p>(</span><span class=n>host</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>config</span><span class=p>[</span><span class=s>"port"</span><span class=p>].</span><span class=n>get_to</span><span class=p>(</span><span class=n>port</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>config</span><span class=p>[</span><span class=s>"ssl"</span><span class=p>].</span><span class=n>get_to</span><span class=p>(</span><span class=n>ssl</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>host</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>':'</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>port</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>ssl</span><span class=w> </span><span class=o>?</span><span class=w> </span><span class=s>" (tls)"</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=s>""</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// the return value is a reference to the argument, so a call can be</span>
|
||||
<span class=w> </span><span class=c1>// used directly in a larger expression</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>other_host</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>config</span><span class=p>[</span><span class=s>"host"</span><span class=p>].</span><span class=n>get_to</span><span class=p>(</span><span class=n>other_host</span><span class=p>).</span><span class=n>size</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=err>db.example.com</span><span class=p>:</span><span class=mi>5432</span><span class=w> </span><span class=err>(</span><span class=kc>tls</span><span class=err>)</span>
|
||||
<span class=mi>14</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../get/ >get</a> - convert the value to a given type</li> <li><a href=../../basic_json/get_to/ ><code>BasicJsonType::get_to</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class=Line truncated
|
||||
@@ -0,0 +1,95 @@
|
||||
# nlohmann::basic_json_view::get_to
|
||||
|
||||
```
|
||||
template<typename T>
|
||||
T& get_to(T& v) const;
|
||||
```
|
||||
|
||||
Converts the value to `T` and stores it in `v`, in one of two ways:
|
||||
|
||||
- For the types that [`get<T>()`](https://json.nlohmann.me/api/basic_json_view/get/index.md) converts directly -- arithmetic types, `bool`, `std::nullptr_t`, [`string_view_t`](https://json.nlohmann.me/api/basic_json_view/#member-types), `std::basic_string`, `BasicJsonType`, `basic_json_view`, `std::vector`, and `std::map`/`std::unordered_map` with string keys -- `v` is assigned: `v = get<T>()`.
|
||||
- For every other type, the subtree is built into a `BasicJsonType` value and `materialize().get_to(v)` is called, i.e. `from_json()` is invoked with the existing `v`, exactly as [`BasicJsonType::get_to()`](https://json.nlohmann.me/api/basic_json/get_to/index.md) does.
|
||||
|
||||
## Template parameters
|
||||
|
||||
`T` : the type to convert the value to
|
||||
|
||||
## Parameters
|
||||
|
||||
`v` (out) : the variable to store the converted value in
|
||||
|
||||
## Return value
|
||||
|
||||
`v`, allowing calls to chain
|
||||
|
||||
## Exception safety
|
||||
|
||||
- For the directly converted types: strong exception safety. If an exception is thrown, `v` is not modified.
|
||||
- For every other type: as for [`BasicJsonType::get_to()`](https://json.nlohmann.me/api/basic_json/get_to/index.md). `materialize()` throws before `v` is touched, but a `from_json()` that throws may leave `v` partly modified.
|
||||
|
||||
## Exceptions
|
||||
|
||||
- For the directly converted types: whatever [`get<T>()`](https://json.nlohmann.me/api/basic_json_view/get/index.md) throws for the same value and `T`.
|
||||
- For every other type: whatever [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) and [`BasicJsonType::get_to()`](https://json.nlohmann.me/api/basic_json/get_to/index.md) throw.
|
||||
|
||||
## Complexity
|
||||
|
||||
- For the directly converted types: whatever [`get<T>()`](https://json.nlohmann.me/api/basic_json_view/get/index.md) has for the same `T`.
|
||||
- For every other type: that of [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md), plus that of [`BasicJsonType::get_to()`](https://json.nlohmann.me/api/basic_json/get_to/index.md).
|
||||
|
||||
## Notes
|
||||
|
||||
The difference between the two ways matters for a `from_json()` that sets only the fields present in the JSON: since it runs on the existing `v`, the other fields of `v` keep their values, whereas assigning `get<T>()` would replace them with those of a default-constructed `T`.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below reads several fields of a service configuration directly into existing variables, then uses the returned reference to fold the `host`/`port` pair into a single string in the same expression.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"host": "db.example.com", "port": 5432, "ssl": true})");
|
||||
const json_view config = doc.root();
|
||||
|
||||
// get_to() writes directly into existing variables -- handy for filling
|
||||
// in the members of a struct one field at a time, without an
|
||||
// intermediate value from get<T>() for each one
|
||||
std::string host;
|
||||
int port = 0;
|
||||
bool ssl = false;
|
||||
config["host"].get_to(host);
|
||||
config["port"].get_to(port);
|
||||
config["ssl"].get_to(ssl);
|
||||
std::cout << host << ':' << port << (ssl ? " (tls)" : "") << '\n';
|
||||
|
||||
// the return value is a reference to the argument, so a call can be
|
||||
// used directly in a larger expression
|
||||
std::string other_host;
|
||||
std::cout << config["host"].get_to(other_host).size() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
db.example.com:5432 (tls)
|
||||
14
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get](https://json.nlohmann.me/api/basic_json_view/get/index.md) - convert the value to a given type
|
||||
- [`BasicJsonType::get_to`](https://json.nlohmann.me/api/basic_json/get_to/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,3 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/" rel="canonical"><link href=../basic_json_document/source/ rel=prev><link href=basic_json_view/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Overview - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_view class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Overview </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </laLine truncated
|
||||
<span class=k>class</span><span class=w> </span><span class=nc>basic_json_view</span><span class=p>;</span>
|
||||
</code></pre></div> <p>A read-only handle to one value of a <a href=../basic_json_document/ ><code>basic_json_document</code></a>: two pointers (a pointer to the document and a pointer into its index), trivially copyable. A view is valid as long as</p> <ul> <li>the document is alive,</li> <li>the document has not been re-parsed with <a href=../basic_json_document/read/ ><code>read()</code></a> (or <a href=../basic_json_document/parse/ ><code>parse()</code></a> into it) or shrunk with <a href=../basic_json_document/shrink_to_fit/ ><code>shrink_to_fit()</code></a> since the view was taken, and</li> <li>if the document borrows its source text, that text is still alive and unmodified (floating-point values and unescaped strings are read from it lazily).</li> </ul> <p>Moving the document itself does not invalidate its views: the index is heap-allocated independently of the <code>basic_json_document</code> object.</p> <p><code>basic_json_view</code> provides the read-only part of the <code>BasicJsonType</code> interface:</p> <ul> <li>the type-inspection functions</li> <li>element access</li> <li>lookup</li> <li>iteration</li> <li>conversion: <a href=get/ ><code>get<T>()</code></a>, <a href=get_string/ ><code>get_string()</code></a>, <a href=number_token/ ><code>number_token()</code></a>, and <a href=materialize/ ><code>materialize()</code></a> to build the <code>BasicJsonType</code> value of a subtree on demand</li> <li>comparison: <a href=operator_eq/ ><code>operator==</code></a> and <a href=operator_ne/ ><code>operator!=</code></a> compare two views, or a view and a <code>BasicJsonType</code> value, without ever building a <code>BasicJsonType</code> value for a view; no ordering comparison (<code class=highlight><span class=k>operator</span><span class=o><</span></code>) is provided</li> </ul> <p><a href=operator%5B%5D/ ><code>operator[]</code></a>, <a href=at/ ><code>at</code></a>, <a href=contains/ ><code>contains</code></a>, and <a href=value/ ><code>value</code></a> also accept a <a href=../json_pointer/ ><code>json_pointer</code></a>. Unlike <code>basic_json</code>, there are no implicit conversion operators (no <code class=highlight><span class=k>operator</span><span class=w> </span><span class=n>ValueType</span><span class=p>()</span></code>), and no <code class=highlight><span class=n>get_ptr</span><span class=p>()</span></code> or <code class=highlight><span class=n>get_ref</span><span class=p>()</span></code>; see <a href=get/ ><code>get</code></a>.</p> <p><code>basic_json_view</code> itself is always read-only -- it never has a <code>set</code> or <code>push_back</code> of its own. A view of an <strong>editable</strong> document (<code class=highlight><span class=n>Editable</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=nb>true</span></code>) sees every edit made through <a href=../basic_json_document/set/ ><code>basic_json_document::set</code></a> and <a href=../basic_json_document/push_back/ ><code>basic_json_document::push_back</code></a>: once a value is changed, every view that still refers to it -- including ones taken before the change -- reads the new value. See <a href=../basic_json_document/#edits>Edits</a>.</p> <h2 id=template-parameters>Template parameters<a class=headerlink href=#template-parameters title="Permanent link">¶</a></h2> <dl> <dt><code>BasicJsonType</code></dt> <dd>a specialization of <a href=../basic_json/ ><code>basic_json</code></a>, matching the <a href=../basic_json_document/ ><code>basic_json_document</code></a> the view was taken from.</dd> <dt><code>Editable</code></dt> <dd>whether the view is of an editable document, matching the <a href=../basic_json_document/ ><code>basic_json_document</code></a> it was taken from (optional, <code class=highlight><span class=nb>false</span></code> by default). See <a href=../basic_json_document/#edits>Edits</a>.</dd> </dl> <h2 id=specializations>Specializations<a class=headerlink href=#specializations title="Permanent link">¶</a></h2> <ul> <li><a href=../json_view/ ><strong>json_view</strong></a> - views of a <a href=../json_document/ ><code>json_document</code></a></li> <li><a href=../ordered_json_view/ ><strong>ordered_json_view</strong></a> - views of an <a href=../ordered_json_document/ ><code>ordered_json_document</code></a></li> <li><a href=../json_editable_view/ ><strong>json_editable_view</strong></a> - views of a <a href=../json_editable_document/ ><code>json_editable_document</code></a></li> <li><a href=../ordered_json_editable_view/ ><strong>ordered_json_editable_view</strong></a> - views of an <a href=../ordered_json_editable_document/ ><code>ordered_json_editable_document</code></a></li> </ul> <h2 id=member-types>Member types<a class=headerlink href=#member-types title="Permanent link">¶</a></h2> <ul> <li><strong>value_t</strong> - the <abbr title="JavaScript Object Notation">JSON</abbr> type enumeration, see <a href=../basic_json/value_t/ ><code>basic_jsonLine truncated
|
||||
@@ -0,0 +1,126 @@
|
||||
# nlohmann::basic_json_view
|
||||
|
||||
Defined in header `<nlohmann/json_view.hpp>`
|
||||
|
||||
```
|
||||
template<typename BasicJsonType, bool Editable = false>
|
||||
class basic_json_view;
|
||||
```
|
||||
|
||||
A read-only handle to one value of a [`basic_json_document`](https://json.nlohmann.me/api/basic_json_document/index.md): two pointers (a pointer to the document and a pointer into its index), trivially copyable. A view is valid as long as
|
||||
|
||||
- the document is alive,
|
||||
- the document has not been re-parsed with [`read()`](https://json.nlohmann.me/api/basic_json_document/read/index.md) (or [`parse()`](https://json.nlohmann.me/api/basic_json_document/parse/index.md) into it) or shrunk with [`shrink_to_fit()`](https://json.nlohmann.me/api/basic_json_document/shrink_to_fit/index.md) since the view was taken, and
|
||||
- if the document borrows its source text, that text is still alive and unmodified (floating-point values and unescaped strings are read from it lazily).
|
||||
|
||||
Moving the document itself does not invalidate its views: the index is heap-allocated independently of the `basic_json_document` object.
|
||||
|
||||
`basic_json_view` provides the read-only part of the `BasicJsonType` interface:
|
||||
|
||||
- the type-inspection functions
|
||||
- element access
|
||||
- lookup
|
||||
- iteration
|
||||
- conversion: [`get<T>()`](https://json.nlohmann.me/api/basic_json_view/get/index.md), [`get_string()`](https://json.nlohmann.me/api/basic_json_view/get_string/index.md), [`number_token()`](https://json.nlohmann.me/api/basic_json_view/number_token/index.md), and [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) to build the `BasicJsonType` value of a subtree on demand
|
||||
- comparison: [`operator==`](https://json.nlohmann.me/api/basic_json_view/operator_eq/index.md) and [`operator!=`](https://json.nlohmann.me/api/basic_json_view/operator_ne/index.md) compare two views, or a view and a `BasicJsonType` value, without ever building a `BasicJsonType` value for a view; no ordering comparison (`operator<`) is provided
|
||||
|
||||
[`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md), [`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md), [`contains`](https://json.nlohmann.me/api/basic_json_view/contains/index.md), and [`value`](https://json.nlohmann.me/api/basic_json_view/value/index.md) also accept a [`json_pointer`](https://json.nlohmann.me/api/json_pointer/index.md). Unlike `basic_json`, there are no implicit conversion operators (no `operator ValueType()`), and no `get_ptr()` or `get_ref()`; see [`get`](https://json.nlohmann.me/api/basic_json_view/get/index.md).
|
||||
|
||||
`basic_json_view` itself is always read-only -- it never has a `set` or `push_back` of its own. A view of an **editable** document (`Editable == true`) sees every edit made through [`basic_json_document::set`](https://json.nlohmann.me/api/basic_json_document/set/index.md) and [`basic_json_document::push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md): once a value is changed, every view that still refers to it -- including ones taken before the change -- reads the new value. See [Edits](https://json.nlohmann.me/api/basic_json_document/#edits).
|
||||
|
||||
## Template parameters
|
||||
|
||||
`BasicJsonType` : a specialization of [`basic_json`](https://json.nlohmann.me/api/basic_json/index.md), matching the [`basic_json_document`](https://json.nlohmann.me/api/basic_json_document/index.md) the view was taken from.
|
||||
|
||||
`Editable` : whether the view is of an editable document, matching the [`basic_json_document`](https://json.nlohmann.me/api/basic_json_document/index.md) it was taken from (optional, `false` by default). See [Edits](https://json.nlohmann.me/api/basic_json_document/#edits).
|
||||
|
||||
## Specializations
|
||||
|
||||
- [**json_view**](https://json.nlohmann.me/api/json_view/index.md) - views of a [`json_document`](https://json.nlohmann.me/api/json_document/index.md)
|
||||
- [**ordered_json_view**](https://json.nlohmann.me/api/ordered_json_view/index.md) - views of an [`ordered_json_document`](https://json.nlohmann.me/api/ordered_json_document/index.md)
|
||||
- [**json_editable_view**](https://json.nlohmann.me/api/json_editable_view/index.md) - views of a [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)
|
||||
- [**ordered_json_editable_view**](https://json.nlohmann.me/api/ordered_json_editable_view/index.md) - views of an [`ordered_json_editable_document`](https://json.nlohmann.me/api/ordered_json_editable_document/index.md)
|
||||
|
||||
## Member types
|
||||
|
||||
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](https://json.nlohmann.me/api/basic_json/value_t/index.md)
|
||||
- **string_t**, **number_integer_t**, **number_unsigned_t**, **number_float_t**, **json_pointer** - the corresponding member types of `BasicJsonType`
|
||||
- **size_type** - `std::size_t`
|
||||
- **string_view_t** - `std::string_view` on C++17 and newer, a minimal internal substitute otherwise
|
||||
- **iterator**, **const_iterator** - a forward iterator over the elements of an array or the member values of an object, in document order; both names refer to the same type, since a view is always read-only
|
||||
- [**number_format**](https://json.nlohmann.me/api/basic_json_view/number_format/index.md) - how [`dump()`](https://json.nlohmann.me/api/basic_json_view/dump/index.md) writes numbers
|
||||
|
||||
## Member functions
|
||||
|
||||
- [(constructor)](https://json.nlohmann.me/api/basic_json_view/basic_json_view/index.md)
|
||||
|
||||
### Object inspection
|
||||
|
||||
- [**type**](https://json.nlohmann.me/api/basic_json_view/type/index.md) - return the type of the value
|
||||
- [**type_name**](https://json.nlohmann.me/api/basic_json_view/type_name/index.md) - return the type as string
|
||||
- [**is_null**](https://json.nlohmann.me/api/basic_json_view/is_null/index.md) - return whether the value is null
|
||||
- [**is_boolean**](https://json.nlohmann.me/api/basic_json_view/is_boolean/index.md) - return whether the value is a boolean
|
||||
- [**is_number**](https://json.nlohmann.me/api/basic_json_view/is_number/index.md) - return whether the value is a number
|
||||
- [**is_number_integer**](https://json.nlohmann.me/api/basic_json_view/is_number_integer/index.md) - return whether the value is an integer number
|
||||
- [**is_number_unsigned**](https://json.nlohmann.me/api/basic_json_view/is_number_unsigned/index.md) - return whether the value is an unsigned integer number
|
||||
- [**is_number_float**](https://json.nlohmann.me/api/basic_json_view/is_number_float/index.md) - return whether the value is a floating-point number
|
||||
- [**is_string**](https://json.nlohmann.me/api/basic_json_view/is_string/index.md) - return whether the value is a string
|
||||
- [**is_array**](https://json.nlohmann.me/api/basic_json_view/is_array/index.md) - return whether the value is an array
|
||||
- [**is_object**](https://json.nlohmann.me/api/basic_json_view/is_object/index.md) - return whether the value is an object
|
||||
- [**is_binary**](https://json.nlohmann.me/api/basic_json_view/is_binary/index.md) - return whether the value is a binary array (always `false`)
|
||||
- [**is_primitive**](https://json.nlohmann.me/api/basic_json_view/is_primitive/index.md) - return whether the type is primitive
|
||||
- [**is_structured**](https://json.nlohmann.me/api/basic_json_view/is_structured/index.md) - return whether the type is structured
|
||||
- [**is_discarded**](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) - return whether the view is invalid
|
||||
|
||||
### Element access
|
||||
|
||||
- [**at**](https://json.nlohmann.me/api/basic_json_view/at/index.md) - access specified element with bounds checking
|
||||
- [**operator[]**](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) - access specified element
|
||||
- [**value**](https://json.nlohmann.me/api/basic_json_view/value/index.md) - access specified element with default value
|
||||
- [**front**](https://json.nlohmann.me/api/basic_json_view/front/index.md) - access the first element
|
||||
- [**back**](https://json.nlohmann.me/api/basic_json_view/back/index.md) - access the last element
|
||||
|
||||
### Lookup
|
||||
|
||||
- [**find**](https://json.nlohmann.me/api/basic_json_view/find/index.md) - find an element in an object
|
||||
- [**count**](https://json.nlohmann.me/api/basic_json_view/count/index.md) - returns the number of occurrences of a key in an object
|
||||
- [**contains**](https://json.nlohmann.me/api/basic_json_view/contains/index.md) - check the existence of an element in an object
|
||||
|
||||
### Iterators
|
||||
|
||||
- [**begin**](https://json.nlohmann.me/api/basic_json_view/begin/index.md) - returns an iterator to the first element
|
||||
- [**cbegin**](https://json.nlohmann.me/api/basic_json_view/cbegin/index.md) - returns a const iterator to the first element
|
||||
- [**end**](https://json.nlohmann.me/api/basic_json_view/end/index.md) - returns an iterator to one past the last element
|
||||
- [**cend**](https://json.nlohmann.me/api/basic_json_view/cend/index.md) - returns a const iterator to one past the last element
|
||||
- [**items**](https://json.nlohmann.me/api/basic_json_view/items/index.md) - wrapper to access iterator member functions in range-based for
|
||||
|
||||
### Capacity
|
||||
|
||||
- [**size**](https://json.nlohmann.me/api/basic_json_view/size/index.md) - return the number of elements
|
||||
- [**empty**](https://json.nlohmann.me/api/basic_json_view/empty/index.md) - return whether the value has no elements
|
||||
|
||||
### Conversion
|
||||
|
||||
- [**get**](https://json.nlohmann.me/api/basic_json_view/get/index.md) - get a value
|
||||
- [**get_to**](https://json.nlohmann.me/api/basic_json_view/get_to/index.md) - get a value and write it to a destination
|
||||
- [**get_string**](https://json.nlohmann.me/api/basic_json_view/get_string/index.md) - get a string value without a copy
|
||||
- [**number_token**](https://json.nlohmann.me/api/basic_json_view/number_token/index.md) - get a number's token text without a copy
|
||||
- [**materialize**](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) - build the `BasicJsonType` value of this subtree
|
||||
|
||||
### Comparison
|
||||
|
||||
- [**operator==**](https://json.nlohmann.me/api/basic_json_view/operator_eq/index.md) - comparison: equal
|
||||
- [**operator!=**](https://json.nlohmann.me/api/basic_json_view/operator_ne/index.md) - comparison: not equal
|
||||
|
||||
### Serialization
|
||||
|
||||
- [**dump**](https://json.nlohmann.me/api/basic_json_view/dump/index.md) - serialize to a JSON-formatted string
|
||||
- [**operator\<<**](https://json.nlohmann.me/api/basic_json_view/operator_ltlt/index.md) - serialize to stream
|
||||
|
||||
### Source access
|
||||
|
||||
- [**source_offset**](https://json.nlohmann.me/api/basic_json_view/source_offset/index.md) - byte offset of this value in the document's source text
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,102 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_array
|
||||
|
||||
```cpp
|
||||
bool is_array() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is an array.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is an array, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_structured](is_structured.md) - return whether the type is structured
|
||||
- [size](size.md) - the number of elements
|
||||
- [empty](empty.md) - whether there are no elements
|
||||
- [`BasicJsonType::is_array`](../basic_json/is_array.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_array/" rel="canonical"><link href=../get_to/ rel=prev><link href=../is_binary/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_array - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_array class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_array </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is an array.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is an array, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_structured/ >is_structured</a> - return whether the type is structured</li> <li><a href=../size/ >size</a> - the number of elements</li> <li><a href=../empty/ >empty</a> - whether there are no elements</li> <li><a href=../../basic_json/is_array/ ><code>BasicJsonType::is_array</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-Line truncated
|
||||
@@ -0,0 +1,99 @@
|
||||
# nlohmann::basic_json_view::is_array
|
||||
|
||||
```
|
||||
bool is_array() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is an array.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is an array, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_structured](https://json.nlohmann.me/api/basic_json_view/is_structured/index.md) - return whether the type is structured
|
||||
- [size](https://json.nlohmann.me/api/basic_json_view/size/index.md) - the number of elements
|
||||
- [empty](https://json.nlohmann.me/api/basic_json_view/empty/index.md) - whether there are no elements
|
||||
- [`BasicJsonType::is_array`](https://json.nlohmann.me/api/basic_json/is_array/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,102 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_binary
|
||||
|
||||
```cpp
|
||||
bool is_binary() const noexcept;
|
||||
```
|
||||
|
||||
This function always returns `#!cpp false`: a document is only ever parsed from JSON text, never from CBOR or the other
|
||||
binary formats, and a JSON text has no binary values, so a view can never refer to one. The function exists for
|
||||
interface parity with [`BasicJsonType::is_binary`](../basic_json/is_binary.md).
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp false`, always.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [type](type.md) - return the type of the value
|
||||
- [`BasicJsonType::is_binary`](../basic_json/is_binary.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_binary/" rel="canonical"><link href=../is_array/ rel=prev><link href=../is_boolean/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_binary - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_binary class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_binary </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12Line truncated
|
||||
</code></pre></div> <p>This function always returns <code class=highlight><span class=nb>false</span></code>: a document is only ever parsed from <abbr title="JavaScript Object Notation">JSON</abbr> text, never from <abbr title="Concise Binary Object Representation">CBOR</abbr> or the other binary formats, and a <abbr title="JavaScript Object Notation">JSON</abbr> text has no binary values, so a view can never refer to one. The function exists for interface parity with <a href=../../basic_json/is_binary/ ><code>BasicJsonType::is_binary</code></a>.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>false</span></code>, always.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../type/ >type</a> - return the type of the value</li> <li><a href=../../basic_json/is_binary/ ><code>BasicJsonType::is_binary</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" claLine truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::is_binary
|
||||
|
||||
```
|
||||
bool is_binary() const noexcept;
|
||||
```
|
||||
|
||||
This function always returns `false`: a document is only ever parsed from JSON text, never from CBOR or the other binary formats, and a JSON text has no binary values, so a view can never refer to one. The function exists for interface parity with [`BasicJsonType::is_binary`](https://json.nlohmann.me/api/basic_json/is_binary/index.md).
|
||||
|
||||
## Return value
|
||||
|
||||
`false`, always.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [type](https://json.nlohmann.me/api/basic_json_view/type/index.md) - return the type of the value
|
||||
- [`BasicJsonType::is_binary`](https://json.nlohmann.me/api/basic_json/is_binary/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,100 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_boolean
|
||||
|
||||
```cpp
|
||||
bool is_boolean() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is a boolean.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is a boolean, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [type](type.md) - return the type of the value
|
||||
- [`BasicJsonType::is_boolean`](../basic_json/is_boolean.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_boolean/" rel="canonical"><link href=../is_binary/ rel=prev><link href=../is_discarded/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_boolean - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_boolean class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_boolean </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is a boolean.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is a boolean, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../type/ >type</a> - return the type of the value</li> <li><a href=../../basic_json/is_boolean/ ><code>BasicJsonType::is_boolean</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" cLine truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::is_boolean
|
||||
|
||||
```
|
||||
bool is_boolean() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is a boolean.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is a boolean, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [type](https://json.nlohmann.me/api/basic_json_view/type/index.md) - return the type of the value
|
||||
- [`BasicJsonType::is_boolean`](https://json.nlohmann.me/api/basic_json/is_boolean/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,115 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_discarded
|
||||
|
||||
```cpp
|
||||
bool is_discarded() const noexcept;
|
||||
```
|
||||
|
||||
Returns whether this view is invalid, i.e. does not refer to a value. This is the case for a default-constructed
|
||||
view (see [(constructor)](basic_json_view.md)), and for [`root()`](../basic_json_document/root.md) of a document
|
||||
that is itself [discarded](../basic_json_document/is_discarded.md) -- in particular, the root of a failed
|
||||
[`parse()`](../basic_json_document/parse.md) with `allow_exceptions` set to `#!cpp false`.
|
||||
|
||||
A discarded view is also what [`operator[]`](operator[].md) returns for a missing key, an index out of range, or a
|
||||
JSON pointer that cannot be resolved, and for any access on a view that is itself discarded (so a chain such as
|
||||
`#!cpp v["a"]["b"]` is safe). [`at`](at.md) throws instead.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the view or its document is discarded, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
A `basic_json_view` is not convertible to `#!cpp bool`: such a conversion would mean "refers to a value", whereas
|
||||
`basic_json` converts to the `#!cpp bool` it holds, so the same code would silently behave differently. Test
|
||||
`#!cpp !v.is_discarded()` explicitly.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [operator[]](operator[].md) - access specified element; yields a discarded view where an element is missing
|
||||
- [(constructor)](basic_json_view.md) - the default constructor creates a discarded view
|
||||
- [is_discarded (basic_json_document)](../basic_json_document/is_discarded.md) - return whether the last parse failed
|
||||
- [`BasicJsonType::is_discarded`](../basic_json/is_discarded.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_discarded/" rel="canonical"><link href=../is_boolean/ rel=prev><link href=../is_null/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_discarded - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_discarded class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_discarded </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 1Line truncated
|
||||
</code></pre></div> <p>Returns whether this view is invalid, i.e. does not refer to a value. This is the case for a default-constructed view (see <a href=../basic_json_view/ >(constructor)</a>), and for <a href=../../basic_json_document/root/ ><code>root()</code></a> of a document that is itself <a href=../../basic_json_document/is_discarded/ >discarded</a> -- in particular, the root of a failed <a href=../../basic_json_document/parse/ ><code>parse()</code></a> with <code>allow_exceptions</code> set to <code class=highlight><span class=nb>false</span></code>.</p> <p>A discarded view is also what <a href=../operator%5B%5D/ ><code>operator[]</code></a> returns for a missing key, an index out of range, or a <abbr title="JavaScript Object Notation">JSON</abbr> pointer that cannot be resolved, and for any access on a view that is itself discarded (so a chain such as <code class=highlight><span class=n>v</span><span class=p>[</span><span class=s>"a"</span><span class=p>][</span><span class=s>"b"</span><span class=p>]</span></code> is safe). <a href=../at/ ><code>at</code></a> throws instead.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the view or its document is discarded, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>A <code>basic_json_view</code> is not convertible to <code class=highlight><span class=kt>bool</span></code>: such a conversion would mean "refers to a value", whereas <code>basic_json</code> converts to the <code class=highlight><span class=kt>bool</span></code> it holds, so the same code would silently behave differently. Test <code class=highlight><span class=o>!</span><span class=n>v</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span></code> explicitly.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../operator%5B%5D/ >operator[]</a> - access specified element; yields a discarded view where an element is missing</li> <li><a href=../basic_json_view/ >(constructor)</a> - the default constructor creates a discarded view</li> <li><a href=../../basic_json_document/is_discarded/ >is_discarded (basic_json_document)</a> - return whether the last parse failed</li> <li><a href=../../basic_json/is_discarded/ ><code>BasicJsonType::is_discarded</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38Line truncated
|
||||
@@ -0,0 +1,105 @@
|
||||
# nlohmann::basic_json_view::is_discarded
|
||||
|
||||
```
|
||||
bool is_discarded() const noexcept;
|
||||
```
|
||||
|
||||
Returns whether this view is invalid, i.e. does not refer to a value. This is the case for a default-constructed view (see [(constructor)](https://json.nlohmann.me/api/basic_json_view/basic_json_view/index.md)), and for [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md) of a document that is itself [discarded](https://json.nlohmann.me/api/basic_json_document/is_discarded/index.md) -- in particular, the root of a failed [`parse()`](https://json.nlohmann.me/api/basic_json_document/parse/index.md) with `allow_exceptions` set to `false`.
|
||||
|
||||
A discarded view is also what [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) returns for a missing key, an index out of range, or a JSON pointer that cannot be resolved, and for any access on a view that is itself discarded (so a chain such as `v["a"]["b"]` is safe). [`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md) throws instead.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the view or its document is discarded, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
A `basic_json_view` is not convertible to `bool`: such a conversion would mean "refers to a value", whereas `basic_json` converts to the `bool` it holds, so the same code would silently behave differently. Test `!v.is_discarded()` explicitly.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [operator[]](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) - access specified element; yields a discarded view where an element is missing
|
||||
- [(constructor)](https://json.nlohmann.me/api/basic_json_view/basic_json_view/index.md) - the default constructor creates a discarded view
|
||||
- [is_discarded (basic_json_document)](https://json.nlohmann.me/api/basic_json_document/is_discarded/index.md) - return whether the last parse failed
|
||||
- [`BasicJsonType::is_discarded`](https://json.nlohmann.me/api/basic_json/is_discarded/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,100 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_null
|
||||
|
||||
```cpp
|
||||
bool is_null() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is `#!json null`.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is `#!json null`, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [type](type.md) - return the type of the value
|
||||
- [`BasicJsonType::is_null`](../basic_json/is_null.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_null/" rel="canonical"><link href=../is_discarded/ rel=prev><link href=../is_number/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_null - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_null class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_null </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is <code class=highlight><span class=kc>null</span></code>.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is <code class=highlight><span class=kc>null</span></code>, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../type/ >type</a> - return the type of the value</li> <li><a href=../../basic_json/is_null/ ><code>BasicJsonType::is_null</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="Line truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::is_null
|
||||
|
||||
```
|
||||
bool is_null() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is `null`.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is `null`, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [type](https://json.nlohmann.me/api/basic_json_view/type/index.md) - return the type of the value
|
||||
- [`BasicJsonType::is_null`](https://json.nlohmann.me/api/basic_json/is_null/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,102 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_number
|
||||
|
||||
```cpp
|
||||
bool is_number() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is a number, i.e. an integer, unsigned integer, or floating-point value. It is defined as `#!cpp is_number_integer() || is_number_float()`.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is a number, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number_integer](is_number_integer.md) - return whether the value is an integer or unsigned integer number
|
||||
- [is_number_unsigned](is_number_unsigned.md) - return whether the value is an unsigned integer number
|
||||
- [is_number_float](is_number_float.md) - return whether the value is a floating-point number
|
||||
- [`BasicJsonType::is_number`](../basic_json/is_number.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_number/" rel="canonical"><link href=../is_null/ rel=prev><link href=../is_number_float/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_number - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_number class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_number </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is a number, i.e. an integer, unsigned integer, or floating-point value. It is defined as <code class=highlight><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o>||</span><span class=w> </span><span class=n>is_number_float</span><span class=p>()</span></code>.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is a number, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_number_integer/ >is_number_integer</a> - return whether the value is an integer or unsigned integer number</li> <li><a href=../is_number_unsigned/ >is_number_unsigned</a> - return whether the value is an unsigned integer number</li> <li><a href=../is_number_float/ >is_number_float</a> - return whether the value is a floating-point number</li> <li><a href=../../basic_json/is_number/ ><code>BasicJsonType::is_number</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5Line truncated
|
||||
@@ -0,0 +1,99 @@
|
||||
# nlohmann::basic_json_view::is_number
|
||||
|
||||
```
|
||||
bool is_number() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is a number, i.e. an integer, unsigned integer, or floating-point value. It is defined as `is_number_integer() || is_number_float()`.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is a number, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number_integer](https://json.nlohmann.me/api/basic_json_view/is_number_integer/index.md) - return whether the value is an integer or unsigned integer number
|
||||
- [is_number_unsigned](https://json.nlohmann.me/api/basic_json_view/is_number_unsigned/index.md) - return whether the value is an unsigned integer number
|
||||
- [is_number_float](https://json.nlohmann.me/api/basic_json_view/is_number_float/index.md) - return whether the value is a floating-point number
|
||||
- [`BasicJsonType::is_number`](https://json.nlohmann.me/api/basic_json/is_number/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,106 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_number_float
|
||||
|
||||
```cpp
|
||||
bool is_number_float() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is a floating-point number.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is `#!cpp value_t::number_float`, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`BasicJsonType::parse`](../basic_json/parse.md), an integer literal that does not fit into the 64-bit
|
||||
integer type is classified as a floating-point number, so `is_number_float()` can be `#!cpp true` even for an
|
||||
integer-looking token in the source text.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number](is_number.md) - return whether the value is a number
|
||||
- [`BasicJsonType::is_number_float`](../basic_json/is_number_float.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_number_float/" rel="canonical"><link href=../is_number/ rel=prev><link href=../is_number_integer/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_number_float - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_number_float class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_number_float </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is a floating-point number.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is <code class=highlight><span class=n>value_t</span><span class=o>::</span><span class=n>number_float</span></code>, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>As for <a href=../../basic_json/parse/ ><code>BasicJsonType::parse</code></a>, an integer literal that does not fit into the 64-bit integer type is classified as a floating-point number, so <code>is_number_float()</code> can be <code class=highlight><span class=nb>true</span></code> even for an integer-looking token in the source text.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_number/ >is_number</a> - return whether the value is a number</li> <li><a href=../../basic_json/is_number_float/ ><code>BasicJsonType::is_number_float</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopLine truncated
|
||||
@@ -0,0 +1,101 @@
|
||||
# nlohmann::basic_json_view::is_number_float
|
||||
|
||||
```
|
||||
bool is_number_float() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is a floating-point number.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is `value_t::number_float`, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`BasicJsonType::parse`](https://json.nlohmann.me/api/basic_json/parse/index.md), an integer literal that does not fit into the 64-bit integer type is classified as a floating-point number, so `is_number_float()` can be `true` even for an integer-looking token in the source text.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number](https://json.nlohmann.me/api/basic_json_view/is_number/index.md) - return whether the value is a number
|
||||
- [`BasicJsonType::is_number_float`](https://json.nlohmann.me/api/basic_json/is_number_float/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,106 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_number_integer
|
||||
|
||||
```cpp
|
||||
bool is_number_integer() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is an integer or unsigned integer number.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is `#!cpp value_t::number_integer` or `#!cpp value_t::number_unsigned`, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`BasicJsonType::is_number_integer`](../basic_json/is_number_integer.md), this includes unsigned integer
|
||||
values; use [`is_number_unsigned`](is_number_unsigned.md) to test for those specifically.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number](is_number.md) - return whether the value is a number
|
||||
- [is_number_unsigned](is_number_unsigned.md) - return whether the value is an unsigned integer number
|
||||
- [`BasicJsonType::is_number_integer`](../basic_json/is_number_integer.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_number_integer/" rel="canonical"><link href=../is_number_float/ rel=prev><link href=../is_number_unsigned/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_number_integer - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_number_integer class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_number_integer </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is an integer or unsigned integer number.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is <code class=highlight><span class=n>value_t</span><span class=o>::</span><span class=n>number_integer</span></code> or <code class=highlight><span class=n>value_t</span><span class=o>::</span><span class=n>number_unsigned</span></code>, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>As for <a href=../../basic_json/is_number_integer/ ><code>BasicJsonType::is_number_integer</code></a>, this includes unsigned integer values; use <a href=../is_number_unsigned/ ><code>is_number_unsigned</code></a> to test for those specifically.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_number/ >is_number</a> - return whether the value is a number</li> <li><a href=../is_number_unsigned/ >is_number_unsigned</a> - return whether the value is an unsigned integer number</li> <li><a href=../../basic_json/is_number_integer/ ><code>BasicJsonType::is_number_integer</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-3Line truncated
|
||||
@@ -0,0 +1,102 @@
|
||||
# nlohmann::basic_json_view::is_number_integer
|
||||
|
||||
```
|
||||
bool is_number_integer() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is an integer or unsigned integer number.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is `value_t::number_integer` or `value_t::number_unsigned`, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`BasicJsonType::is_number_integer`](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md), this includes unsigned integer values; use [`is_number_unsigned`](https://json.nlohmann.me/api/basic_json_view/is_number_unsigned/index.md) to test for those specifically.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number](https://json.nlohmann.me/api/basic_json_view/is_number/index.md) - return whether the value is a number
|
||||
- [is_number_unsigned](https://json.nlohmann.me/api/basic_json_view/is_number_unsigned/index.md) - return whether the value is an unsigned integer number
|
||||
- [`BasicJsonType::is_number_integer`](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,100 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_number_unsigned
|
||||
|
||||
```cpp
|
||||
bool is_number_unsigned() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is an unsigned integer number.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is `#!cpp value_t::number_unsigned`, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number_integer](is_number_integer.md) - return whether the value is an integer or unsigned integer number
|
||||
- [`BasicJsonType::is_number_unsigned`](../basic_json/is_number_unsigned.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_number_unsigned/" rel="canonical"><link href=../is_number_integer/ rel=prev><link href=../is_object/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_number_unsigned - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_number_unsigned class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_number_unsigned </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is an unsigned integer number.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is <code class=highlight><span class=n>value_t</span><span class=o>::</span><span class=n>number_unsigned</span></code>, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_number_integer/ >is_number_integer</a> - return whether the value is an integer or unsigned integer number</li> <li><a href=../../basic_json/is_number_unsigned/ ><code>BasicJsonType::is_number_unsigned</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.cLine truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::is_number_unsigned
|
||||
|
||||
```
|
||||
bool is_number_unsigned() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is an unsigned integer number.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is `value_t::number_unsigned`, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_number_integer](https://json.nlohmann.me/api/basic_json_view/is_number_integer/index.md) - return whether the value is an integer or unsigned integer number
|
||||
- [`BasicJsonType::is_number_unsigned`](https://json.nlohmann.me/api/basic_json/is_number_unsigned/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,102 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_object
|
||||
|
||||
```cpp
|
||||
bool is_object() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is an object.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is an object, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_structured](is_structured.md) - return whether the type is structured
|
||||
- [size](size.md) - the number of elements
|
||||
- [empty](empty.md) - whether there are no elements
|
||||
- [`BasicJsonType::is_object`](../basic_json/is_object.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_object/" rel="canonical"><link href=../is_number_unsigned/ rel=prev><link href=../is_primitive/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_object - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_object class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_object </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is an object.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is an object, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_structured/ >is_structured</a> - return whether the type is structured</li> <li><a href=../size/ >size</a> - the number of elements</li> <li><a href=../empty/ >empty</a> - whether there are no elements</li> <li><a href=../../basic_json/is_object/ ><code>BasicJsonType::is_object</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.Line truncated
|
||||
@@ -0,0 +1,99 @@
|
||||
# nlohmann::basic_json_view::is_object
|
||||
|
||||
```
|
||||
bool is_object() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is an object.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is an object, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_structured](https://json.nlohmann.me/api/basic_json_view/is_structured/index.md) - return whether the type is structured
|
||||
- [size](https://json.nlohmann.me/api/basic_json_view/size/index.md) - the number of elements
|
||||
- [empty](https://json.nlohmann.me/api/basic_json_view/empty/index.md) - whether there are no elements
|
||||
- [`BasicJsonType::is_object`](https://json.nlohmann.me/api/basic_json/is_object/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,101 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_primitive
|
||||
|
||||
```cpp
|
||||
bool is_primitive() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is primitive, i.e. `#!json null`, a boolean, a number, or a string. It is defined as `#!cpp is_null() || is_string() || is_boolean() || is_number()`.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is primitive, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_structured](is_structured.md) - return whether the type is structured (the complement of this function, for a
|
||||
non-discarded view)
|
||||
- [`BasicJsonType::is_primitive`](../basic_json/is_primitive.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_primitive/" rel="canonical"><link href=../is_object/ rel=prev><link href=../is_string/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_primitive - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_primitive class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_primitive </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is primitive, i.e. <code class=highlight><span class=kc>null</span></code>, a boolean, a number, or a string. It is defined as <code class=highlight><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o>||</span><span class=w> </span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o>||</span><span class=w> </span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o>||</span><span class=w> </span><span class=n>is_number</span><span class=p>()</span></code>.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is primitive, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_structured/ >is_structured</a> - return whether the type is structured (the complement of this function, for a non-discarded view)</li> <li><a href=../../basic_json/is_primitive/ ><code>BasicJsonType::is_primitive</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https:Line truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::is_primitive
|
||||
|
||||
```
|
||||
bool is_primitive() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is primitive, i.e. `null`, a boolean, a number, or a string. It is defined as `is_null() || is_string() || is_boolean() || is_number()`.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is primitive, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_structured](https://json.nlohmann.me/api/basic_json_view/is_structured/index.md) - return whether the type is structured (the complement of this function, for a non-discarded view)
|
||||
- [`BasicJsonType::is_primitive`](https://json.nlohmann.me/api/basic_json/is_primitive/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,101 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_string
|
||||
|
||||
```cpp
|
||||
bool is_string() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is a string.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is a string, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [source_offset](source_offset.md) - byte offset of this value in the source text, which differs for strings: an
|
||||
escaped string has none
|
||||
- [`BasicJsonType::is_string`](../basic_json/is_string.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_string/" rel="canonical"><link href=../is_primitive/ rel=prev><link href=../is_structured/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_string - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_string class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_string </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is a string.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is a string, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../source_offset/ >source_offset</a> - byte offset of this value in the source text, which differs for strings: an escaped string has none</li> <li><a href=../../basic_json/is_string/ ><code>BasicJsonType::is_string</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://wwLine truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::is_string
|
||||
|
||||
```
|
||||
bool is_string() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is a string.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is a string, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [source_offset](https://json.nlohmann.me/api/basic_json_view/source_offset/index.md) - byte offset of this value in the source text, which differs for strings: an escaped string has none
|
||||
- [`BasicJsonType::is_string`](https://json.nlohmann.me/api/basic_json/is_string/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,101 @@
|
||||
# <small>nlohmann::basic_json_view::</small>is_structured
|
||||
|
||||
```cpp
|
||||
bool is_structured() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `#!cpp true` if and only if the value is structured, i.e. an array or an object. It is defined as `#!cpp is_array() || is_object()`.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if the type is structured, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any
|
||||
of them into a `BasicJsonType` value.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_primitive](is_primitive.md) - return whether the type is primitive
|
||||
- [size](size.md), [empty](empty.md) - the number of elements, and whether there are none
|
||||
- [`BasicJsonType::is_structured`](../basic_json/is_structured.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,57 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/is_structured/" rel="canonical"><link href=../is_string/ rel=prev><link href=../items/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>is_structured - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewis_structured class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> is_structured </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 Line truncated
|
||||
</code></pre></div> <p>This function returns <code class=highlight><span class=nb>true</span></code> if and only if the value is structured, i.e. an array or an object. It is defined as <code class=highlight><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o>||</span><span class=w> </span><span class=n>is_object</span><span class=p>()</span></code>.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p><code class=highlight><span class=nb>true</span></code> if the type is structured, <code class=highlight><span class=nb>false</span></code> otherwise.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below classifies several parsed documents by the type of their root value, without materializing any of them into a <code>BasicJsonType</code> value.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>boolalpha</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// several incoming messages, one json_document per message. type() and</span>
|
||||
<span class=w> </span><span class=c1>// is_*() only look at the flat index built by parse(); no nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// tree exists yet, and none is built unless materialize() is called</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_null</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_bool</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"true"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_int</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"-42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_unsigned</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"42"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_float</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"4.2"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_string</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>"hi"</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_array</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2, 3]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>d_object</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"a": 1}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_null</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_null</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_bool</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_boolean</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_int</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_integer</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_unsigned</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_unsigned</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_float</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_number_float</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_string</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_string</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_structured</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_object</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>' '</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_primitive</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// JSON text can never produce a binary value: is_binary() is always false</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>d_array</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_binary</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// a default-constructed view, and the root of a document that failed to</span>
|
||||
<span class=w> </span><span class=c1>// parse without exceptions, are both discarded</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=w> </span><span class=n>invalid</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>failed</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"not json"</span><span class=p>,</span><span class=w> </span><span class=cm>/* allow_exceptions */</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>invalid</span><span class=p>.</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>failed</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>is_discarded</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// type() returns the same value_t enumeration as basic_json::type()</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=p>(</span><span class=n>d_object</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>type</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>value_t</span><span class=o>::</span><span class=n>object</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>true</span>
|
||||
<span class=kc>true</span><span class=w> </span><span class=kc>false</span>
|
||||
<span class=kc>false</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
<span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../is_primitive/ >is_primitive</a> - return whether the type is primitive</li> <li><a href=../size/ >size</a>, <a href=../empty/ >empty</a> - the number of elements, and whether there are none</li> <li><a href=../../basic_json/is_structured/ ><code>BasicJsonType::is_structured</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 Line truncated
|
||||
@@ -0,0 +1,98 @@
|
||||
# nlohmann::basic_json_view::is_structured
|
||||
|
||||
```
|
||||
bool is_structured() const noexcept;
|
||||
```
|
||||
|
||||
This function returns `true` if and only if the value is structured, i.e. an array or an object. It is defined as `is_array() || is_object()`.
|
||||
|
||||
## Return value
|
||||
|
||||
`true` if the type is structured, `false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below classifies several parsed documents by the type of their root value, without materializing any of them into a `BasicJsonType` value.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha;
|
||||
|
||||
// several incoming messages, one json_document per message. type() and
|
||||
// is_*() only look at the flat index built by parse(); no nlohmann::json
|
||||
// tree exists yet, and none is built unless materialize() is called
|
||||
json_document d_null = json_document::parse("null");
|
||||
json_document d_bool = json_document::parse("true");
|
||||
json_document d_int = json_document::parse("-42");
|
||||
json_document d_unsigned = json_document::parse("42");
|
||||
json_document d_float = json_document::parse("4.2");
|
||||
json_document d_string = json_document::parse(R"("hi")");
|
||||
json_document d_array = json_document::parse("[1, 2, 3]");
|
||||
json_document d_object = json_document::parse(R"({"a": 1})");
|
||||
|
||||
std::cout << d_null.root().is_null() << '\n';
|
||||
std::cout << d_bool.root().is_boolean() << '\n';
|
||||
std::cout << d_int.root().is_number() << ' ' << d_int.root().is_number_integer() << '\n';
|
||||
std::cout << d_unsigned.root().is_number_unsigned() << '\n';
|
||||
std::cout << d_float.root().is_number_float() << '\n';
|
||||
std::cout << d_string.root().is_string() << '\n';
|
||||
std::cout << d_array.root().is_array() << ' ' << d_array.root().is_structured() << '\n';
|
||||
std::cout << d_object.root().is_object() << ' ' << d_object.root().is_primitive() << '\n';
|
||||
|
||||
// JSON text can never produce a binary value: is_binary() is always false
|
||||
std::cout << d_array.root().is_binary() << '\n';
|
||||
|
||||
// a default-constructed view, and the root of a document that failed to
|
||||
// parse without exceptions, are both discarded
|
||||
nlohmann::json_view invalid;
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
std::cout << invalid.is_discarded() << '\n';
|
||||
std::cout << failed.root().is_discarded() << '\n';
|
||||
|
||||
// type() returns the same value_t enumeration as basic_json::type()
|
||||
std::cout << (d_object.root().type() == nlohmann::json::value_t::object) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
true
|
||||
true
|
||||
true true
|
||||
true
|
||||
true
|
||||
true
|
||||
true true
|
||||
true false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [is_primitive](https://json.nlohmann.me/api/basic_json_view/is_primitive/index.md) - return whether the type is primitive
|
||||
- [size](https://json.nlohmann.me/api/basic_json_view/size/index.md), [empty](https://json.nlohmann.me/api/basic_json_view/empty/index.md) - the number of elements, and whether there are none
|
||||
- [`BasicJsonType::is_structured`](https://json.nlohmann.me/api/basic_json/is_structured/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,113 @@
|
||||
# <small>nlohmann::basic_json_view::</small>items
|
||||
|
||||
```cpp
|
||||
/* unspecified */ items() const noexcept;
|
||||
```
|
||||
|
||||
Returns a range of items -- (key, value) pairs -- for use in range-based for loops. An item's `#!cpp key()` is a
|
||||
`#!cpp string_view_t` (for an array element, its index converted to a string, as for
|
||||
[`BasicJsonType::items()`](../basic_json/items.md)), and its `#!cpp value()` is a `basic_json_view` of the element.
|
||||
|
||||
The returned type is not part of the public API and may change between versions; use a range-based for loop (see the
|
||||
example), or `#!cpp decltype(v.items())` if you need to name it.
|
||||
|
||||
```cpp
|
||||
for (const auto& item : v.items())
|
||||
{
|
||||
std::cout << "key: " << item.key() << ", value: " << item.value() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
On C++17, `item` also supports [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding):
|
||||
|
||||
```cpp
|
||||
for (const auto [key, value] : v.items())
|
||||
{
|
||||
std::cout << "key: " << key << ", value: " << value << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Note the `#!cpp const auto` (by value), not `#!cpp const auto&`: unlike `BasicJsonType::items()`, whose elements are
|
||||
references into an existing object, a view's `item` is produced on the fly for each step of the iteration, so there
|
||||
is nothing for a reference to bind to.
|
||||
|
||||
## Return value
|
||||
|
||||
A range whose iterators dereference to an item (with `#!cpp key()` and `#!cpp value()`, see above) and whose
|
||||
`#!cpp begin()`/`#!cpp end()` are equivalent to [`basic_json_view::begin()`](begin.md)/[`end()`](end.md), in document order.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`begin()`](begin.md)/[`end()`](end.md), `items()` visits **every** member of an object, including all
|
||||
occurrences of a duplicate key -- unlike [`operator[]`](operator[].md), [`at`](at.md), [`find`](find.md),
|
||||
and [`contains`](contains.md), which resolve to the *first* member with a given key. See the
|
||||
[Notes on duplicate keys](operator[].md#notes) of `operator[]`.
|
||||
|
||||
!!! danger "Lifetime issues"
|
||||
|
||||
Unlike `BasicJsonType::items()` (see [#2040](https://github.com/nlohmann/json/issues/2040)), calling `items()` on
|
||||
a temporary view is safe, for instance `#!cpp for (auto item : v["a"].items())`: the returned range holds a copy
|
||||
of the view. What must outlive the loop is the document and, for a borrowed document, the source buffer.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below shows a settings object whose source text records every update to a key as a duplicate
|
||||
member, in the order they happened. `items()` walks all of them, so the update history is visible, while
|
||||
[`operator[]`](operator[].md) only ever sees the *first* one and [`materialize()`](materialize.md) -- like
|
||||
[`BasicJsonType::parse()`](../basic_json/parse.md) -- keeps only the *last*.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a settings object whose source text records every update to a key as
|
||||
// a duplicate member. items() visits all of them, in document order, so
|
||||
// the update history is visible; operator[] only ever sees the first
|
||||
// one, and materialize() -- like basic_json::parse() -- keeps the last
|
||||
json_document updates = json_document::parse(R"({"retries": 1, "timeout": 30, "retries": 5})");
|
||||
const auto settings = updates.root();
|
||||
|
||||
for (const auto& item : settings.items())
|
||||
{
|
||||
std::cout << item.key() << '=' << item.value().materialize().dump() << '\n';
|
||||
}
|
||||
|
||||
std::cout << "first \"retries\" seen by operator[]: " << settings["retries"].materialize().dump() << '\n';
|
||||
std::cout << "last \"retries\" kept by materialize(): " << settings.materialize()["retries"].dump() << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
retries=1
|
||||
timeout=30
|
||||
retries=5
|
||||
first "retries" seen by operator[]: 1
|
||||
last "retries" kept by materialize(): 5
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [begin](begin.md), [end](end.md) - the iterators `items()` is built on
|
||||
- [`BasicJsonType::items`](../basic_json/items.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,37 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/items/" rel="canonical"><link href=../is_structured/ rel=prev><link href=../materialize/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>items - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewitems class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> items </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/Line truncated
|
||||
</code></pre></div> <p>Returns a range of items -- (key, value) pairs -- for use in range-based for loops. An item's <code class=highlight><span class=n>key</span><span class=p>()</span></code> is a <code class=highlight><span class=n>string_view_t</span></code> (for an array element, its index converted to a string, as for <a href=../../basic_json/items/ ><code>BasicJsonType::items()</code></a>), and its <code class=highlight><span class=n>value</span><span class=p>()</span></code> is a <code>basic_json_view</code> of the element.</p> <p>The returned type is not part of the public <abbr title="Application Programming Interface">API</abbr> and may change between versions; use a range-based for loop (see the example), or <code class=highlight><span class=k>decltype</span><span class=p>(</span><span class=n>v</span><span class=p>.</span><span class=n>items</span><span class=p>())</span></code> if you need to name it.</p> <div class=highlight><pre><span></span><code><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=o>&</span><span class=w> </span><span class=n>item</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>v</span><span class=p>.</span><span class=n>items</span><span class=p>())</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"key: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>item</span><span class=p>.</span><span class=n>key</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>", value: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>item</span><span class=p>.</span><span class=n>value</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>On C++17, <code>item</code> also supports <a href="https://en.cppreference.com/w/cpp/language/structured_binding">structured bindings</a>:</p> <div class=highlight><pre><span></span><code><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=p>[</span><span class=n>key</span><span class=p>,</span><span class=w> </span><span class=n>value</span><span class=p>]</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>v</span><span class=p>.</span><span class=n>items</span><span class=p>())</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"key: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>key</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>", value: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>value</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Note the <code class=highlight><span class=k>const</span><span class=w> </span><span class=k>auto</span></code> (by value), not <code class=highlight><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=o>&</span></code>: unlike <code>BasicJsonType::items()</code>, whose elements are references into an existing object, a view's <code>item</code> is produced on the fly for each step of the iteration, so there is nothing for a reference to bind to.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>A range whose iterators dereference to an item (with <code class=highlight><span class=n>key</span><span class=p>()</span></code> and <code class=highlight><span class=n>value</span><span class=p>()</span></code>, see above) and whose <code class=highlight><span class=n>begin</span><span class=p>()</span></code>/<code class=highlight><span class=n>end</span><span class=p>()</span></code> are equivalent to <a href=../begin/ ><code>basic_json_view::begin()</code></a>/<a href=../end/ ><code>end()</code></a>, in document order.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>No-throw guarantee: this function never throws exceptions.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p>As for <a href=../begin/ ><code>begin()</code></a>/<a href=../end/ ><code>end()</code></a>, <code>items()</code> visits <strong>every</strong> member of an object, including all occurrences of a duplicate key -- unlike <a href=../operator%5B%5D/ ><code>operator[]</code></a>, <a href=../at/ ><code>at</code></a>, <a href=../find/ ><code>find</code></a>, and <a href=../contains/ ><code>contains</code></a>, which resolve to the <em>first</em> member with a given key. See the <a href=../operator%5B%5D/#notes>Notes on duplicate keys</a> of <code>operator[]</code>.</p> <div class="admonition danger"> <p class=admonition-title>Lifetime issues</p> <p>Unlike <code>BasicJsonType::items()</code> (see <a href="https://github.com/nlohmann/json/issues/2040">#2040</a>), calling <code>items()</code> on a temporary view is safe, for instance <code class=highlight><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>auto</span><span class=w> </span><span class=n>item</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>v</span><span class=p>[</span><span class=s>"a"</span><span class=p>].</span><span class=n>items</span><span class=p>())</span></code>: the returned range holds a copy of the view. What must outlive the loop is the document and, for a borrowed document, the source buffer.</p> </div> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below shows a settings object whose source text records every update to a key as a duplicate member, in the order they happened. <code>items()</code> walks all of them, so the update history is visible, while <a href=../operator%5B%5D/ ><code>operator[]</code></a> only ever sees the <em>first</em> one and <a href=../materialize/ ><code>materialize()</code></a> -- like <a href=../../basic_json/parse/ ><code>BasicJsonType::parse()</code></a> -- keeps only the <em>last</em>.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a settings object whose source text records every update to a key as</span>
|
||||
<span class=w> </span><span class=c1>// a duplicate member. items() visits all of them, in document order, so</span>
|
||||
<span class=w> </span><span class=c1>// the update history is visible; operator[] only ever sees the first</span>
|
||||
<span class=w> </span><span class=c1>// one, and materialize() -- like basic_json::parse() -- keeps the last</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>updates</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{"retries": 1, "timeout": 30, "retries": 5}</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>settings</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>updates</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=o>&</span><span class=w> </span><span class=n>item</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>settings</span><span class=p>.</span><span class=n>items</span><span class=p>())</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>item</span><span class=p>.</span><span class=n>key</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'='</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>item</span><span class=p>.</span><span class=n>value</span><span class=p>().</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"first </span><span class=se>\"</span><span class=s>retries</span><span class=se>\"</span><span class=s> seen by operator[]: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>settings</span><span class=p>[</span><span class=s>"retries"</span><span class=p>].</span><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"last </span><span class=se>\"</span><span class=s>retries</span><span class=se>\"</span><span class=s> kept by materialize(): "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>settings</span><span class=p>.</span><span class=n>materialize</span><span class=p>()[</span><span class=s>"retries"</span><span class=p>].</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=err>re</span><span class=kc>tr</span><span class=err>ies=</span><span class=mi>1</span>
|
||||
<span class=kc>t</span><span class=err>imeou</span><span class=kc>t</span><span class=err>=</span><span class=mi>30</span>
|
||||
<span class=err>re</span><span class=kc>tr</span><span class=err>ies=</span><span class=mi>5</span>
|
||||
<span class=kc>f</span><span class=err>irs</span><span class=kc>t</span><span class=w> </span><span class=s2>"retries"</span><span class=w> </span><span class=err>see</span><span class=kc>n</span><span class=w> </span><span class=err>by</span><span class=w> </span><span class=err>opera</span><span class=kc>t</span><span class=err>or</span><span class=p>[]:</span><span class=w> </span><span class=mi>1</span>
|
||||
<span class=err>las</span><span class=kc>t</span><span class=w> </span><span class=s2>"retries"</span><span class=w> </span><span class=err>kep</span><span class=kc>t</span><span class=w> </span><span class=err>by</span><span class=w> </span><span class=err>ma</span><span class=kc>ter</span><span class=err>ialize()</span><span class=p>:</span><span class=w> </span><span class=mi>5</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../begin/ >begin</a>, <a href=../end/ >end</a> - the iterators <code>items()</code> is built on</li> <li><a href=../../basic_json/items/ ><code>BasicJsonType::items</code></a> - the corresponding function of <code>basic_json</code></li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rLine truncated
|
||||
@@ -0,0 +1,97 @@
|
||||
# nlohmann::basic_json_view::items
|
||||
|
||||
```
|
||||
/* unspecified */ items() const noexcept;
|
||||
```
|
||||
|
||||
Returns a range of items -- (key, value) pairs -- for use in range-based for loops. An item's `key()` is a `string_view_t` (for an array element, its index converted to a string, as for [`BasicJsonType::items()`](https://json.nlohmann.me/api/basic_json/items/index.md)), and its `value()` is a `basic_json_view` of the element.
|
||||
|
||||
The returned type is not part of the public API and may change between versions; use a range-based for loop (see the example), or `decltype(v.items())` if you need to name it.
|
||||
|
||||
```
|
||||
for (const auto& item : v.items())
|
||||
{
|
||||
std::cout << "key: " << item.key() << ", value: " << item.value() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
On C++17, `item` also supports [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding):
|
||||
|
||||
```
|
||||
for (const auto [key, value] : v.items())
|
||||
{
|
||||
std::cout << "key: " << key << ", value: " << value << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Note the `const auto` (by value), not `const auto&`: unlike `BasicJsonType::items()`, whose elements are references into an existing object, a view's `item` is produced on the fly for each step of the iteration, so there is nothing for a reference to bind to.
|
||||
|
||||
## Return value
|
||||
|
||||
A range whose iterators dereference to an item (with `key()` and `value()`, see above) and whose `begin()`/`end()` are equivalent to [`basic_json_view::begin()`](https://json.nlohmann.me/api/basic_json_view/begin/index.md)/[`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md), in document order.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
As for [`begin()`](https://json.nlohmann.me/api/basic_json_view/begin/index.md)/[`end()`](https://json.nlohmann.me/api/basic_json_view/end/index.md), `items()` visits **every** member of an object, including all occurrences of a duplicate key -- unlike [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md), [`at`](https://json.nlohmann.me/api/basic_json_view/at/index.md), [`find`](https://json.nlohmann.me/api/basic_json_view/find/index.md), and [`contains`](https://json.nlohmann.me/api/basic_json_view/contains/index.md), which resolve to the *first* member with a given key. See the [Notes on duplicate keys](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/#notes) of `operator[]`.
|
||||
|
||||
Lifetime issues
|
||||
|
||||
Unlike `BasicJsonType::items()` (see [#2040](https://github.com/nlohmann/json/issues/2040)), calling `items()` on a temporary view is safe, for instance `for (auto item : v["a"].items())`: the returned range holds a copy of the view. What must outlive the loop is the document and, for a borrowed document, the source buffer.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below shows a settings object whose source text records every update to a key as a duplicate member, in the order they happened. `items()` walks all of them, so the update history is visible, while [`operator[]`](https://json.nlohmann.me/api/basic_json_view/operator%5B%5D/index.md) only ever sees the *first* one and [`materialize()`](https://json.nlohmann.me/api/basic_json_view/materialize/index.md) -- like [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) -- keeps only the *last*.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a settings object whose source text records every update to a key as
|
||||
// a duplicate member. items() visits all of them, in document order, so
|
||||
// the update history is visible; operator[] only ever sees the first
|
||||
// one, and materialize() -- like basic_json::parse() -- keeps the last
|
||||
json_document updates = json_document::parse(R"({"retries": 1, "timeout": 30, "retries": 5})");
|
||||
const auto settings = updates.root();
|
||||
|
||||
for (const auto& item : settings.items())
|
||||
{
|
||||
std::cout << item.key() << '=' << item.value().materialize().dump() << '\n';
|
||||
}
|
||||
|
||||
std::cout << "first \"retries\" seen by operator[]: " << settings["retries"].materialize().dump() << '\n';
|
||||
std::cout << "last \"retries\" kept by materialize(): " << settings.materialize()["retries"].dump() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
retries=1
|
||||
timeout=30
|
||||
retries=5
|
||||
first "retries" seen by operator[]: 1
|
||||
last "retries" kept by materialize(): 5
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [begin](https://json.nlohmann.me/api/basic_json_view/begin/index.md), [end](https://json.nlohmann.me/api/basic_json_view/end/index.md) - the iterators `items()` is built on
|
||||
- [`BasicJsonType::items`](https://json.nlohmann.me/api/basic_json/items/index.md) - the corresponding function of `basic_json`
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,106 @@
|
||||
# <small>nlohmann::basic_json_view::</small>materialize
|
||||
|
||||
```cpp
|
||||
BasicJsonType materialize() const;
|
||||
```
|
||||
|
||||
Builds the `BasicJsonType` value of this subtree: the value [`BasicJsonType::parse()`](../basic_json/parse.md) would
|
||||
have produced for the same source text, allocated for the first time by this call.
|
||||
|
||||
## Return value
|
||||
|
||||
The `BasicJsonType` value of this subtree, or a discarded `BasicJsonType` value (`#!cpp BasicJsonType(value_t::discarded)`)
|
||||
if the view is [discarded](is_discarded.md).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to (nothing about either is mutated by
|
||||
this function).
|
||||
|
||||
## Exceptions
|
||||
|
||||
May throw `#!cpp std::bad_alloc` (via `BasicJsonType`'s allocator) if constructing the result fails.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the size of the subtree.
|
||||
|
||||
## Notes
|
||||
|
||||
`materialize()` replays the subtree through the same SAX builder [`BasicJsonType::parse()`](../basic_json/parse.md)
|
||||
uses, so the result matches it exactly -- including, for an object, that a repeated key keeps only its
|
||||
last value. The replay is iterative, so it is not limited by the call stack the way a naive recursive conversion
|
||||
would be; the JSON nesting depth is limited only by available memory, as for `BasicJsonType::parse()` itself.
|
||||
|
||||
Unlike parsing with [`JSON_DIAGNOSTIC_POSITIONS`](../macros/json_diagnostic_positions.md) enabled, the values
|
||||
produced by `materialize()` do not carry source positions. The index has the start offset of every value, but not the
|
||||
end of arrays, objects, and escaped strings, which the positions also need.
|
||||
|
||||
Calling `materialize()` on the same view repeatedly builds a new, independent `BasicJsonType` value each time; it
|
||||
never caches the result.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below skips messages that are not useful -- a discarded value, or an empty array -- using only
|
||||
[`is_array()`](is_array.md) and [`empty()`](empty.md), and calls `materialize()` only for the messages that are
|
||||
actually used, so no `BasicJsonType` value (and none of its per-element allocations) is ever built for the
|
||||
skipped ones.
|
||||
|
||||
```cpp
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// three incoming messages; skip the ones that are not useful without ever
|
||||
// building a nlohmann::json value for them
|
||||
json_document heartbeat = json_document::parse("null");
|
||||
json_document empty_batch = json_document::parse("[]");
|
||||
json_document batch = json_document::parse(R"([{"id": 1}, {"id": 2}, {"id": 3}])");
|
||||
std::array<const json_document*, 3> messages = {{&heartbeat, &empty_batch, &batch}};
|
||||
|
||||
for (const json_document* d : messages)
|
||||
{
|
||||
// is_array()/empty() only look at the flat index: a discarded
|
||||
// heartbeat or an empty batch is never turned into a nlohmann::json
|
||||
// value, so no per-element allocation happens for them
|
||||
if (!d->root().is_array() || d->root().empty())
|
||||
{
|
||||
std::cout << "skipped\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// materialize() replays the subtree through the same SAX builder
|
||||
// basic_json::parse() uses, so the result is exactly what
|
||||
// basic_json::parse() would have produced for the same text
|
||||
nlohmann::json value = d->root().materialize();
|
||||
std::cout << value.dump() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
skipped
|
||||
skipped
|
||||
[{"id":1},{"id":2},{"id":3}]
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [root](../basic_json_document/root.md) - the view of a document's root value
|
||||
- [`BasicJsonType::parse`](../basic_json/parse.md) - build a `BasicJsonType` value directly from a JSON text
|
||||
- [`JSON_DIAGNOSTIC_POSITIONS`](../macros/json_diagnostic_positions.md) - source positions on parsed values (not
|
||||
produced by `materialize()`)
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,38 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/materialize/" rel="canonical"><link href=../items/ rel=prev><link href=../number_format/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>materialize - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewmaterialize class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> materialize </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 1Line truncated
|
||||
</code></pre></div> <p>Builds the <code>BasicJsonType</code> value of this subtree: the value <a href=../../basic_json/parse/ ><code>BasicJsonType::parse()</code></a> would have produced for the same source text, allocated for the first time by this call.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>The <code>BasicJsonType</code> value of this subtree, or a discarded <code>BasicJsonType</code> value (<code class=highlight><span class=n>BasicJsonType</span><span class=p>(</span><span class=n>value_t</span><span class=o>::</span><span class=n>discarded</span><span class=p>)</span></code>) if the view is <a href=../is_discarded/ >discarded</a>.</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: there are no changes to the view or the document it refers to (nothing about either is mutated by this function).</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>May throw <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>bad_alloc</span></code> (via <code>BasicJsonType</code>'s allocator) if constructing the result fails.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the subtree.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p><code>materialize()</code> replays the subtree through the same <abbr title="Simple API for XML">SAX</abbr> builder <a href=../../basic_json/parse/ ><code>BasicJsonType::parse()</code></a> uses, so the result matches it exactly -- including, for an object, that a repeated key keeps only its last value. The replay is iterative, so it is not limited by the call stack the way a naive recursive conversion would be; the <abbr title="JavaScript Object Notation">JSON</abbr> nesting depth is limited only by available memory, as for <code>BasicJsonType::parse()</code> itself.</p> <p>Unlike parsing with <a href=../../macros/json_diagnostic_positions/ ><code>JSON_DIAGNOSTIC_POSITIONS</code></a> enabled, the values produced by <code>materialize()</code> do not carry source positions. The index has the start offset of every value, but not the end of arrays, objects, and escaped strings, which the positions also need.</p> <p>Calling <code>materialize()</code> on the same view repeatedly builds a new, independent <code>BasicJsonType</code> value each time; it never caches the result.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below skips messages that are not useful -- a discarded value, or an empty array -- using only <a href=../is_array/ ><code>is_array()</code></a> and <a href=../empty/ ><code>empty()</code></a>, and calls <code>materialize()</code> only for the messages that are actually used, so no <code>BasicJsonType</code> value (and none of its per-element allocations) is ever built for the skipped ones.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><array></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// three incoming messages; skip the ones that are not useful without ever</span>
|
||||
<span class=w> </span><span class=c1>// building a nlohmann::json value for them</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>heartbeat</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"null"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>empty_batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[]"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>batch</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>[{"id": 1}, {"id": 2}, {"id": 3}]</span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o><</span><span class=k>const</span><span class=w> </span><span class=n>json_document</span><span class=o>*</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=o>></span><span class=w> </span><span class=n>messages</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>{{</span><span class=o>&</span><span class=n>heartbeat</span><span class=p>,</span><span class=w> </span><span class=o>&</span><span class=n>empty_batch</span><span class=p>,</span><span class=w> </span><span class=o>&</span><span class=n>batch</span><span class=p>}};</span>
|
||||
|
||||
<span class=w> </span><span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>json_document</span><span class=o>*</span><span class=w> </span><span class=n>d</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>messages</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// is_array()/empty() only look at the flat index: a discarded</span>
|
||||
<span class=w> </span><span class=c1>// heartbeat or an empty batch is never turned into a nlohmann::json</span>
|
||||
<span class=w> </span><span class=c1>// value, so no per-element allocation happens for them</span>
|
||||
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=o>!</span><span class=n>d</span><span class=o>-></span><span class=n>root</span><span class=p>().</span><span class=n>is_array</span><span class=p>()</span><span class=w> </span><span class=o>||</span><span class=w> </span><span class=n>d</span><span class=o>-></span><span class=n>root</span><span class=p>().</span><span class=n>empty</span><span class=p>())</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"skipped</span><span class=se>\n</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>continue</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// materialize() replays the subtree through the same SAX builder</span>
|
||||
<span class=w> </span><span class=c1>// basic_json::parse() uses, so the result is exactly what</span>
|
||||
<span class=w> </span><span class=c1>// basic_json::parse() would have produced for the same text</span>
|
||||
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=w> </span><span class=n>value</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>d</span><span class=o>-></span><span class=n>root</span><span class=p>().</span><span class=n>materialize</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>value</span><span class=p>.</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=err>skipped</span>
|
||||
<span class=err>skipped</span>
|
||||
<span class=p>[{</span><span class=nt>"id"</span><span class=p>:</span><span class=mi>1</span><span class=p>},{</span><span class=nt>"id"</span><span class=p>:</span><span class=mi>2</span><span class=p>},{</span><span class=nt>"id"</span><span class=p>:</span><span class=mi>3</span><span class=p>}]</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../../basic_json_document/root/ >root</a> - the view of a document's root value</li> <li><a href=../../basic_json/parse/ ><code>BasicJsonType::parse</code></a> - build a <code>BasicJsonType</code> value directly from a <abbr title="JavaScript Object Notation">JSON</abbr> text</li> <li><a href=../../macros/json_diagnostic_positions/ ><code>JSON_DIAGNOSTIC_POSITIONS</code></a> - source positions on parsed values (not produced by <code>materialize()</code>)</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.Line truncated
|
||||
@@ -0,0 +1,91 @@
|
||||
# nlohmann::basic_json_view::materialize
|
||||
|
||||
```
|
||||
BasicJsonType materialize() const;
|
||||
```
|
||||
|
||||
Builds the `BasicJsonType` value of this subtree: the value [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) would have produced for the same source text, allocated for the first time by this call.
|
||||
|
||||
## Return value
|
||||
|
||||
The `BasicJsonType` value of this subtree, or a discarded `BasicJsonType` value (`BasicJsonType(value_t::discarded)`) if the view is [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to (nothing about either is mutated by this function).
|
||||
|
||||
## Exceptions
|
||||
|
||||
May throw `std::bad_alloc` (via `BasicJsonType`'s allocator) if constructing the result fails.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the size of the subtree.
|
||||
|
||||
## Notes
|
||||
|
||||
`materialize()` replays the subtree through the same SAX builder [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) uses, so the result matches it exactly -- including, for an object, that a repeated key keeps only its last value. The replay is iterative, so it is not limited by the call stack the way a naive recursive conversion would be; the JSON nesting depth is limited only by available memory, as for `BasicJsonType::parse()` itself.
|
||||
|
||||
Unlike parsing with [`JSON_DIAGNOSTIC_POSITIONS`](https://json.nlohmann.me/api/macros/json_diagnostic_positions/index.md) enabled, the values produced by `materialize()` do not carry source positions. The index has the start offset of every value, but not the end of arrays, objects, and escaped strings, which the positions also need.
|
||||
|
||||
Calling `materialize()` on the same view repeatedly builds a new, independent `BasicJsonType` value each time; it never caches the result.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below skips messages that are not useful -- a discarded value, or an empty array -- using only [`is_array()`](https://json.nlohmann.me/api/basic_json_view/is_array/index.md) and [`empty()`](https://json.nlohmann.me/api/basic_json_view/empty/index.md), and calls `materialize()` only for the messages that are actually used, so no `BasicJsonType` value (and none of its per-element allocations) is ever built for the skipped ones.
|
||||
|
||||
```
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// three incoming messages; skip the ones that are not useful without ever
|
||||
// building a nlohmann::json value for them
|
||||
json_document heartbeat = json_document::parse("null");
|
||||
json_document empty_batch = json_document::parse("[]");
|
||||
json_document batch = json_document::parse(R"([{"id": 1}, {"id": 2}, {"id": 3}])");
|
||||
std::array<const json_document*, 3> messages = {{&heartbeat, &empty_batch, &batch}};
|
||||
|
||||
for (const json_document* d : messages)
|
||||
{
|
||||
// is_array()/empty() only look at the flat index: a discarded
|
||||
// heartbeat or an empty batch is never turned into a nlohmann::json
|
||||
// value, so no per-element allocation happens for them
|
||||
if (!d->root().is_array() || d->root().empty())
|
||||
{
|
||||
std::cout << "skipped\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// materialize() replays the subtree through the same SAX builder
|
||||
// basic_json::parse() uses, so the result is exactly what
|
||||
// basic_json::parse() would have produced for the same text
|
||||
nlohmann::json value = d->root().materialize();
|
||||
std::cout << value.dump() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
skipped
|
||||
skipped
|
||||
[{"id":1},{"id":2},{"id":3}]
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [root](https://json.nlohmann.me/api/basic_json_document/root/index.md) - the view of a document's root value
|
||||
- [`BasicJsonType::parse`](https://json.nlohmann.me/api/basic_json/parse/index.md) - build a `BasicJsonType` value directly from a JSON text
|
||||
- [`JSON_DIAGNOSTIC_POSITIONS`](https://json.nlohmann.me/api/macros/json_diagnostic_positions/index.md) - source positions on parsed values (not produced by `materialize()`)
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,81 @@
|
||||
# <small>nlohmann::basic_json_view::</small>number_format
|
||||
|
||||
```cpp
|
||||
enum class number_format {
|
||||
shortest,
|
||||
source
|
||||
};
|
||||
```
|
||||
|
||||
This enumeration is used in [`dump`](dump.md) to choose how numbers are written. Two values are differentiated:
|
||||
|
||||
shortest
|
||||
: integers are copied from the source text -- already canonical in JSON -- except that `#!cpp -0` becomes
|
||||
`#!cpp 0`, the way [`BasicJsonType::parse()`](../basic_json/parse.md) reads it; floats are written with the
|
||||
library's shortest round-trip conversion, exactly as [`BasicJsonType::dump()`](../basic_json/dump.md) would (e.g.
|
||||
`#!cpp 1.5`, `#!cpp 100.0`, `#!cpp 1e+100`)
|
||||
|
||||
source
|
||||
: every number is copied exactly as it appears in the source text -- `#!cpp 1.50`, `#!cpp 1E2`, `#!cpp -0`, all
|
||||
digits of an integer literal with more digits than any number type holds -- something `BasicJsonType` cannot do,
|
||||
since parsing already reduces every number to its parsed value
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below writes back a price list received from a supplier: with `number_format::shortest` (the
|
||||
default), a trailing zero and scientific notation are normalized away and a long account number that overflows
|
||||
every number type is rounded, the same way `#!cpp materialize().dump()` (or `basic_json::dump()`) would;
|
||||
`number_format::source` keeps every number exactly as it was written in the source text instead.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a price list received from a supplier feed -- prices and account
|
||||
// numbers must be forwarded exactly, e.g. into an invoice
|
||||
const json_document doc = json_document::parse(R"(
|
||||
[{"sku": "A1", "price": 19.90, "account_id": 12345678901234567890123456},
|
||||
{"sku": "A2", "price": 1E2, "account_id": 98765432109876543210987654}]
|
||||
)");
|
||||
const json_view list = doc.root();
|
||||
|
||||
// number_format::shortest (the default) writes numbers the way
|
||||
// basic_json::dump() would: "19.90" becomes "19.9", "1E2" becomes
|
||||
// "100.0", and each account number -- far beyond any 64-bit integer --
|
||||
// is rounded to the nearest double, exactly as materialize().dump()
|
||||
// (or a plain nlohmann::json) would round it
|
||||
std::cout << list.dump() << '\n';
|
||||
|
||||
// number_format::source copies every number exactly as it was written
|
||||
// in the source text instead -- something basic_json cannot do at all,
|
||||
// since parsing already reduces every number to its parsed value
|
||||
std::cout << list.dump(-1, ' ', false, json_view::number_format::source) << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
[{"sku":"A1","price":19.9,"account_id":1.2345678901234568e+25},{"sku":"A2","price":100.0,"account_id":9.876543210987655e+25}]
|
||||
[{"sku":"A1","price":19.90,"account_id":12345678901234567890123456},{"sku":"A2","price":1E2,"account_id":98765432109876543210987654}]
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [dump](dump.md) - serialize to a JSON-formatted string
|
||||
- [number_token](number_token.md) - get a single number's token text without dumping the whole value
|
||||
- [`BasicJsonType::error_handler_t`](../basic_json/error_handler_t.md) - the analogous enumeration for
|
||||
`BasicJsonType::dump`'s decoding-error behavior
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,35 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/number_format/" rel="canonical"><link href=../materialize/ rel=prev><link href=../number_token/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>number_format - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewnumber_format class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> number_format </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9Line truncated
|
||||
<span class=w> </span><span class=n>shortest</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=n>source</span>
|
||||
<span class=p>};</span>
|
||||
</code></pre></div> <p>This enumeration is used in <a href=../dump/ ><code>dump</code></a> to choose how numbers are written. Two values are differentiated:</p> <dl> <dt>shortest</dt> <dd>integers are copied from the source text -- already canonical in <abbr title="JavaScript Object Notation">JSON</abbr> -- except that <code class=highlight><span class=mi>-0</span></code> becomes <code class=highlight><span class=mi>0</span></code>, the way <a href=../../basic_json/parse/ ><code>BasicJsonType::parse()</code></a> reads it; floats are written with the library's shortest round-trip conversion, exactly as <a href=../../basic_json/dump/ ><code>BasicJsonType::dump()</code></a> would (e.g. <code class=highlight><span class=mf>1.5</span></code>, <code class=highlight><span class=mf>100.0</span></code>, <code class=highlight><span class=mf>1e+100</span></code>)</dd> <dt>source</dt> <dd>every number is copied exactly as it appears in the source text -- <code class=highlight><span class=mf>1.50</span></code>, <code class=highlight><span class=mf>1E2</span></code>, <code class=highlight><span class=mi>-0</span></code>, all digits of an integer literal with more digits than any number type holds -- something <code>BasicJsonType</code> cannot do, since parsing already reduces every number to its parsed value</dd> </dl> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below writes back a price list received from a supplier: with <code>number_format::shortest</code> (the default), a trailing zero and scientific notation are normalized away and a long account number that overflows every number type is rounded, the same way <code class=highlight><span class=n>materialize</span><span class=p>().</span><span class=n>dump</span><span class=p>()</span></code> (or <code>basic_json::dump()</code>) would; <code>number_format::source</code> keeps every number exactly as it was written in the source text instead.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a price list received from a supplier feed -- prices and account</span>
|
||||
<span class=w> </span><span class=c1>// numbers must be forwarded exactly, e.g. into an invoice</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> [{"sku": "A1", "price": 19.90, "account_id": 12345678901234567890123456},</span>
|
||||
<span class=s> {"sku": "A2", "price": 1E2, "account_id": 98765432109876543210987654}]</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>list</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// number_format::shortest (the default) writes numbers the way</span>
|
||||
<span class=w> </span><span class=c1>// basic_json::dump() would: "19.90" becomes "19.9", "1E2" becomes</span>
|
||||
<span class=w> </span><span class=c1>// "100.0", and each account number -- far beyond any 64-bit integer --</span>
|
||||
<span class=w> </span><span class=c1>// is rounded to the nearest double, exactly as materialize().dump()</span>
|
||||
<span class=w> </span><span class=c1>// (or a plain nlohmann::json) would round it</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>list</span><span class=p>.</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// number_format::source copies every number exactly as it was written</span>
|
||||
<span class=w> </span><span class=c1>// in the source text instead -- something basic_json cannot do at all,</span>
|
||||
<span class=w> </span><span class=c1>// since parsing already reduces every number to its parsed value</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>list</span><span class=p>.</span><span class=n>dump</span><span class=p>(</span><span class=mi>-1</span><span class=p>,</span><span class=w> </span><span class=sc>' '</span><span class=p>,</span><span class=w> </span><span class=nb>false</span><span class=p>,</span><span class=w> </span><span class=n>json_view</span><span class=o>::</span><span class=n>number_format</span><span class=o>::</span><span class=n>source</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>[{</span><span class=nt>"sku"</span><span class=p>:</span><span class=s2>"A1"</span><span class=p>,</span><span class=nt>"price"</span><span class=p>:</span><span class=mf>19.9</span><span class=p>,</span><span class=nt>"account_id"</span><span class=p>:</span><span class=mf>1.2345678901234568e+25</span><span class=p>},{</span><span class=nt>"sku"</span><span class=p>:</span><span class=s2>"A2"</span><span class=p>,</span><span class=nt>"price"</span><span class=p>:</span><span class=mf>100.0</span><span class=p>,</span><span class=nt>"account_id"</span><span class=p>:</span><span class=mf>9.876543210987655e+25</span><span class=p>}]</span>
|
||||
<span class=p>[{</span><span class=nt>"sku"</span><span class=p>:</span><span class=s2>"A1"</span><span class=p>,</span><span class=nt>"price"</span><span class=p>:</span><span class=mf>19.90</span><span class=p>,</span><span class=nt>"account_id"</span><span class=p>:</span><span class=mi>12345678901234567890123456</span><span class=p>},{</span><span class=nt>"sku"</span><span class=p>:</span><span class=s2>"A2"</span><span class=p>,</span><span class=nt>"price"</span><span class=p>:</span><span class=mf>1E2</span><span class=p>,</span><span class=nt>"account_id"</span><span class=p>:</span><span class=mi>98765432109876543210987654</span><span class=p>}]</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../dump/ >dump</a> - serialize to a <abbr title="JavaScript Object Notation">JSON</abbr>-formatted string</li> <li><a href=../number_token/ >number_token</a> - get a single number's token text without dumping the whole value</li> <li><a href=../../basic_json/error_handler_t/ ><code>BasicJsonType::error_handler_t</code></a> - the analogous enumeration for <code>BasicJsonType::dump</code>'s decoding-error behavior</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:28 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 Line truncated
|
||||
@@ -0,0 +1,68 @@
|
||||
# nlohmann::basic_json_view::number_format
|
||||
|
||||
```
|
||||
enum class number_format {
|
||||
shortest,
|
||||
source
|
||||
};
|
||||
```
|
||||
|
||||
This enumeration is used in [`dump`](https://json.nlohmann.me/api/basic_json_view/dump/index.md) to choose how numbers are written. Two values are differentiated:
|
||||
|
||||
shortest : integers are copied from the source text -- already canonical in JSON -- except that `-0` becomes `0`, the way [`BasicJsonType::parse()`](https://json.nlohmann.me/api/basic_json/parse/index.md) reads it; floats are written with the library's shortest round-trip conversion, exactly as [`BasicJsonType::dump()`](https://json.nlohmann.me/api/basic_json/dump/index.md) would (e.g. `1.5`, `100.0`, `1e+100`)
|
||||
|
||||
source : every number is copied exactly as it appears in the source text -- `1.50`, `1E2`, `-0`, all digits of an integer literal with more digits than any number type holds -- something `BasicJsonType` cannot do, since parsing already reduces every number to its parsed value
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below writes back a price list received from a supplier: with `number_format::shortest` (the default), a trailing zero and scientific notation are normalized away and a long account number that overflows every number type is rounded, the same way `materialize().dump()` (or `basic_json::dump()`) would; `number_format::source` keeps every number exactly as it was written in the source text instead.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a price list received from a supplier feed -- prices and account
|
||||
// numbers must be forwarded exactly, e.g. into an invoice
|
||||
const json_document doc = json_document::parse(R"(
|
||||
[{"sku": "A1", "price": 19.90, "account_id": 12345678901234567890123456},
|
||||
{"sku": "A2", "price": 1E2, "account_id": 98765432109876543210987654}]
|
||||
)");
|
||||
const json_view list = doc.root();
|
||||
|
||||
// number_format::shortest (the default) writes numbers the way
|
||||
// basic_json::dump() would: "19.90" becomes "19.9", "1E2" becomes
|
||||
// "100.0", and each account number -- far beyond any 64-bit integer --
|
||||
// is rounded to the nearest double, exactly as materialize().dump()
|
||||
// (or a plain nlohmann::json) would round it
|
||||
std::cout << list.dump() << '\n';
|
||||
|
||||
// number_format::source copies every number exactly as it was written
|
||||
// in the source text instead -- something basic_json cannot do at all,
|
||||
// since parsing already reduces every number to its parsed value
|
||||
std::cout << list.dump(-1, ' ', false, json_view::number_format::source) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[{"sku":"A1","price":19.9,"account_id":1.2345678901234568e+25},{"sku":"A2","price":100.0,"account_id":9.876543210987655e+25}]
|
||||
[{"sku":"A1","price":19.90,"account_id":12345678901234567890123456},{"sku":"A2","price":1E2,"account_id":98765432109876543210987654}]
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [dump](https://json.nlohmann.me/api/basic_json_view/dump/index.md) - serialize to a JSON-formatted string
|
||||
- [number_token](https://json.nlohmann.me/api/basic_json_view/number_token/index.md) - get a single number's token text without dumping the whole value
|
||||
- [`BasicJsonType::error_handler_t`](https://json.nlohmann.me/api/basic_json/error_handler_t/index.md) - the analogous enumeration for `BasicJsonType::dump`'s decoding-error behavior
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
@@ -0,0 +1,108 @@
|
||||
# <small>nlohmann::basic_json_view::</small>number_token
|
||||
|
||||
```cpp
|
||||
string_view_t number_token() const;
|
||||
```
|
||||
|
||||
Returns the number exactly as it appears in the source text, without parsing or rounding it.
|
||||
|
||||
## Return value
|
||||
|
||||
The number's token text, as a [`string_view_t`](index.md#member-types) into the document's
|
||||
[`source()`](../basic_json_document/source.md) text -- for example `#!cpp "1.50"`, `#!cpp "1E2"`, `#!cpp "-0"`, or an
|
||||
integer literal with more digits than any number type holds (such as a 30-digit integer, which [`get<T>()`](get.md)
|
||||
and [`materialize()`](materialize.md) can only represent approximately, as a `number_float_t`).
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong guarantee: there are no changes to the view or the document it refers to.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if the value is not a number; example:
|
||||
`"type must be number, but is string"`.
|
||||
|
||||
This exception does not carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no
|
||||
`BasicJsonType` value to point at, so the exception is created without one, even if `BasicJsonType` was built with
|
||||
`JSON_DIAGNOSTICS` enabled.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
`BasicJsonType` has no counterpart to this function: once a number is parsed into `number_integer_t`,
|
||||
`number_unsigned_t`, or `number_float_t`, its original textual form (leading zeros aside, which are already rejected
|
||||
by the grammar; trailing zeros in the fraction; the case and sign of the exponent; ...) is gone. `number_token()` is
|
||||
useful precisely where that form must survive -- a price or an identifier that must be reproduced exactly, or a
|
||||
number too large for any of `BasicJsonType`'s number types to hold without loss.
|
||||
|
||||
The returned [`string_view_t`](index.md#member-types) always points into the document's
|
||||
[`source()`](../basic_json_document/source.md) text -- numbers are never decoded into the document's separate string
|
||||
buffer -- and is valid exactly as long as that text is, see the [validity rules](index.md) of `basic_json_view`.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
The example below keeps a price and an order ID exactly as they were written in an incoming order, where
|
||||
converting them with [`get<T>()`](get.md) would lose information: the price picks up floating-point rounding, and
|
||||
the order ID -- more digits than a 64-bit integer holds -- can only be approximated as a `#!cpp double` once
|
||||
`#!cpp materialize()`d.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a price and an order id from an incoming order -- both need to be
|
||||
// reproduced exactly, e.g. for an invoice or an audit log
|
||||
json_document doc = json_document::parse(R"(
|
||||
{"price": 19.90, "order_id": 1234567890123456789012345, "quantity": 3}
|
||||
)");
|
||||
const json_view order = doc.root();
|
||||
|
||||
// number_token() returns the number exactly as written in the source
|
||||
std::cout << order["price"].number_token() << '\n';
|
||||
std::cout << order["order_id"].number_token() << '\n';
|
||||
|
||||
// get<double>() converts it instead -- the exact source text is gone:
|
||||
// "19.90" becomes the double closest to 19.9, printed without the
|
||||
// trailing zero, and the 25-digit order id -- far beyond any 64-bit
|
||||
// integer -- can only be approximated as a double
|
||||
std::cout << order["price"].get<double>() << '\n';
|
||||
std::cout << order.materialize()["order_id"].dump() << '\n';
|
||||
|
||||
// an ordinary quantity has nothing to lose either way
|
||||
std::cout << order["quantity"].number_token() << " == " << order["quantity"].get<int>() << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
19.90
|
||||
1234567890123456789012345
|
||||
19.9
|
||||
1.2345678901234568e+24
|
||||
3 == 3
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [get](get.md) - convert the value to a given type
|
||||
- [get_string](get_string.md) - the string, without a copy
|
||||
- [`BasicJsonType::number_integer_t`](../basic_json/number_integer_t.md),
|
||||
[`number_unsigned_t`](../basic_json/number_unsigned_t.md), [`number_float_t`](../basic_json/number_float_t.md) - the
|
||||
number types `#!cpp get<T>()` and `materialize()` convert into
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0.
|
||||
@@ -0,0 +1,36 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/api/basic_json_view/number_token/" rel="canonical"><link href=../number_format/ rel=prev><link href=../operator_ltlt/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>number_token - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#nlohmannbasic_json_viewnumber_token class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> number_token </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.Line truncated
|
||||
</code></pre></div> <p>Returns the number exactly as it appears in the source text, without parsing or rounding it.</p> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>The number's token text, as a <a href=../#member-types><code>string_view_t</code></a> into the document's <a href=../../basic_json_document/source/ ><code>source()</code></a> text -- for example <code class=highlight><span class=s>"1.50"</span></code>, <code class=highlight><span class=s>"1E2"</span></code>, <code class=highlight><span class=s>"-0"</span></code>, or an integer literal with more digits than any number type holds (such as a 30-digit integer, which <a href=../get/ ><code>get<T>()</code></a> and <a href=../materialize/ ><code>materialize()</code></a> can only represent approximately, as a <code>number_float_t</code>).</p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: there are no changes to the view or the document it refers to.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error302><code>type_error.302</code></a> if the value is not a number; example: <code>"type must be number, but is string"</code>.</p> <p>This exception does not carry a <a href=../../macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> path: the view has no <code>BasicJsonType</code> value to point at, so the exception is created without one, even if <code>BasicJsonType</code> was built with <code>JSON_DIAGNOSTICS</code> enabled.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Constant.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <p><code>BasicJsonType</code> has no counterpart to this function: once a number is parsed into <code>number_integer_t</code>, <code>number_unsigned_t</code>, or <code>number_float_t</code>, its original textual form (leading zeros aside, which are already rejected by the grammar; trailing zeros in the fraction; the case and sign of the exponent; ...) is gone. <code>number_token()</code> is useful precisely where that form must survive -- a price or an identifier that must be reproduced exactly, or a number too large for any of <code>BasicJsonType</code>'s number types to hold without loss.</p> <p>The returned <a href=../#member-types><code>string_view_t</code></a> always points into the document's <a href=../../basic_json_document/source/ ><code>source()</code></a> text -- numbers are never decoded into the document's separate string buffer -- and is valid exactly as long as that text is, see the <a href=../ >validity rules</a> of <code>basic_json_view</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example below keeps a price and an order ID exactly as they were written in an incoming order, where converting them with <a href=../get/ ><code>get<T>()</code></a> would lose information: the price picks up floating-point rounding, and the order ID -- more digits than a 64-bit integer holds -- can only be approximated as a <code class=highlight><span class=kt>double</span></code> once <code class=highlight><span class=n>materialize</span><span class=p>()</span></code>d.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a price and an order id from an incoming order -- both need to be</span>
|
||||
<span class=w> </span><span class=c1>// reproduced exactly, e.g. for an invoice or an audit log</span>
|
||||
<span class=w> </span><span class=n>json_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span>
|
||||
<span class=s> {"price": 19.90, "order_id": 1234567890123456789012345, "quantity": 3}</span>
|
||||
<span class=s> </span><span class=dl>)</span><span class=s>"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>json_view</span><span class=w> </span><span class=n>order</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>();</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// number_token() returns the number exactly as written in the source</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>[</span><span class=s>"price"</span><span class=p>].</span><span class=n>number_token</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>[</span><span class=s>"order_id"</span><span class=p>].</span><span class=n>number_token</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// get<double>() converts it instead -- the exact source text is gone:</span>
|
||||
<span class=w> </span><span class=c1>// "19.90" becomes the double closest to 19.9, printed without the</span>
|
||||
<span class=w> </span><span class=c1>// trailing zero, and the 25-digit order id -- far beyond any 64-bit</span>
|
||||
<span class=w> </span><span class=c1>// integer -- can only be approximated as a double</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>[</span><span class=s>"price"</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=kt>double</span><span class=o>></span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>.</span><span class=n>materialize</span><span class=p>()[</span><span class=s>"order_id"</span><span class=p>].</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// an ordinary quantity has nothing to lose either way</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>[</span><span class=s>"quantity"</span><span class=p>].</span><span class=n>number_token</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>" == "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>order</span><span class=p>[</span><span class=s>"quantity"</span><span class=p>].</span><span class=n>get</span><span class=o><</span><span class=kt>int</span><span class=o>></span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=mf>19.90</span>
|
||||
<span class=mi>1234567890123456789012345</span>
|
||||
<span class=mf>19.9</span>
|
||||
<span class=mf>1.2345678901234568e+24</span>
|
||||
<span class=mi>3</span><span class=w> </span><span class=err>==</span><span class=w> </span><span class=mi>3</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../get/ >get</a> - convert the value to a given type</li> <li><a href=../get_string/ >get_string</a> - the string, without a copy</li> <li><a href=../../basic_json/number_integer_t/ ><code>BasicJsonType::number_integer_t</code></a>, <a href=../../basic_json/number_unsigned_t/ ><code>number_unsigned_t</code></a>, <a href=../../basic_json/number_float_t/ ><code>number_float_t</code></a> - the number types <code class=highlight><span class=n>get</span><span class=o><</span><span class=n>T</span><span class=o>></span><span class=p>()</span></code> and <code>materialize()</code> convert into</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 21, 2026 08:21:27 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0Line truncated
|
||||
Loaded 100 of 714 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user