This commit is contained in:
nlohmann committed 2026-08-04 07:02:38 +00:00
1 parent ce3ce5a2b8
commit 3de3beb9b0
263 files changed
+462 -287

No files matched your search

+10 -5
View File
@@ -80,15 +80,20 @@
</code></pre></div> <div class="admonition info"> <p class=admonition-title>Extracting references into a tuple</p> <p>A tuple type may also hold references (e.g. <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>tuple</span><span class=o>&lt;</span><span class=kt>double</span><span class=o>&amp;</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=o>&amp;&gt;</span></code>) to avoid copying: <code>get</code> then returns a tuple of references pointing directly at the elements stored inside the <code>basic_json</code> array, rather than a tuple of copies:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>{</span><span class=mf>1.0</span><span class=p>,</span><span class=w> </span><span class=s>&quot;hello&quot;</span><span class=p>};</span>
<span class=k>auto</span><span class=w> </span><span class=n>refs</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>tuple</span><span class=o>&lt;</span><span class=kt>double</span><span class=o>&amp;</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=o>&amp;&gt;&gt;</span><span class=p>();</span>
<span class=n>std</span><span class=o>::</span><span class=n>get</span><span class=o>&lt;</span><span class=mi>1</span><span class=o>&gt;</span><span class=p>(</span><span class=n>refs</span><span class=p>)</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=s>&quot;world&quot;</span><span class=p>;</span><span class=w> </span><span class=c1>// modifies j[1] in place</span>
</code></pre></div> <p>A referenced type must be one the library actually stores (or an arithmetic type it can convert to/from); otherwise this is a compile error.</p> </div> <h2 id=implicit-conversions>Implicit conversions<a class=headerlink href=#implicit-conversions title="Permanent link">&para;</a></h2> <p>By default, a <abbr title="JavaScript Object Notation">JSON</abbr> value implicitly converts to a compatible C++ type, so the explicit <code>get</code> call can often be omitted:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=s>&quot;Hello&quot;</span><span class=p>;</span>
</code></pre></div> <p>A referenced element must name the type the library actually <em>stores</em> — one of <a href=../../api/basic_json/boolean_t/ ><code>boolean_t</code></a>, <a href=../../api/basic_json/number_integer_t/ ><code>number_integer_t</code></a>, <a href=../../api/basic_json/number_unsigned_t/ ><code>number_unsigned_t</code></a>, <a href=../../api/basic_json/number_float_t/ ><code>number_float_t</code></a>, <a href=../../api/basic_json/string_t/ ><code>string_t</code></a>, <a href=../../api/basic_json/binary_t/ ><code>binary_t</code></a>, <a href=../../api/basic_json/array_t/ ><code>array_t</code></a>, or <a href=../../api/basic_json/object_t/ ><code>object_t</code></a>. There is nothing else to refer to, so a reference to any other type is a compile error even when a conversion would exist: <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>tuple</span><span class=o>&lt;</span><span class=kt>int</span><span class=o>&amp;&gt;</span></code> is rejected, because the library stores a <code class=highlight><span class=n>number_integer_t</span></code> (<code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int64_t</span></code> by default) and not an <code class=highlight><span class=kt>int</span></code>. This restriction applies only to reference elements — a plain <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>tuple</span><span class=o>&lt;</span><span class=kt>int</span><span class=o>&gt;</span></code> converts by value as usual.</p> </div> <h2 id=implicit-conversions>Implicit conversions<a class=headerlink href=#implicit-conversions title="Permanent link">&para;</a></h2> <p>By default, a <abbr title="JavaScript Object Notation">JSON</abbr> value implicitly converts to a compatible C++ type, so the explicit <code>get</code> call can often be omitted:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=s>&quot;Hello&quot;</span><span class=p>;</span>
<span class=n>std</span><span class=o>::</span><span class=n>string</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>j</span><span class=p>;</span><span class=w> </span><span class=c1>// implicit conversion, same as j.get&lt;std::string&gt;()</span>
</code></pre></div> <p>Implicit conversions are convenient but can be surprising (for example, in overload resolution or with <code>auto</code>). They can be disabled by defining <a href=../../api/macros/json_use_implicit_conversions/ ><code>JSON_USE_IMPLICIT_CONVERSIONS</code></a> to <code class=highlight><span class=mi>0</span></code>, which forces the explicit <code>get</code> form and can catch unintended conversions at compile time.</p> <div class="admonition warning"> <p class=admonition-title>Conversions do not range-check numbers</p> <p>Just like C++ itself, the <code>get</code> family performs numeric conversions without range checks — retrieving a floating-point value as an integer truncates it, and narrowing conversions may overflow. See <a href=../types/number_handling/#number-conversion>number conversion</a> for details and how to guard against it.</p> </div> <div class="admonition warning"> <p class=admonition-title>std::optional direct construction from <abbr title="JavaScript Object Notation">JSON</abbr> null throws</p> <p>Constructing or assigning <code>std::optional&lt;T&gt;</code> directly from a <abbr title="JavaScript Object Notation">JSON</abbr> value does not correctly produce <code>std::nullopt</code> for a <abbr title="JavaScript Object Notation">JSON</abbr> <code>null</code>:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=w> </span><span class=n>j_null</span><span class=p>;</span>
<span class=n>std</span><span class=o>::</span><span class=n>optional</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=o>&gt;</span><span class=w> </span><span class=n>opt</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j_null</span><span class=p>;</span><span class=w> </span><span class=c1>// ❌ throws type_error 302</span>
</code></pre></div> <p>This is due to C++ language rules: <code>std::optional&lt;T&gt;</code> has its own converting constructor that is chosen over <code>basic_json::operator T()</code> when both are viable. Use <code>get&lt;std::optional&lt;T&gt;&gt;()</code> or <code>get_to()</code> instead:</p> <div class=highlight><pre><span></span><code><span class=k>auto</span><span class=w> </span><span class=n>opt</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j_null</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>optional</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=o>&gt;&gt;</span><span class=p>();</span><span class=w> </span><span class=c1>// ✅ std::nullopt</span>
<span class=n>j_null</span><span class=p>.</span><span class=n>get_to</span><span class=p>(</span><span class=n>opt</span><span class=p>);</span><span class=w> </span><span class=c1>// ✅ std::nullopt</span>
</code></pre></div> </div> <div class="admonition warning"> <p class=admonition-title><code>static_cast</code> and <code>get&lt;std::optional&lt;T&gt;&gt;()</code> are not guaranteed equivalent</p> <p><code>operator ValueType()</code> (used by <code>static_cast</code> and implicit conversions) intentionally excludes <code>std::optional&lt;T&gt;</code> from delegating to <code>get&lt;T&gt;()</code>, to avoid a constructor ambiguity with <code>std::optional&lt;T&gt;</code>'s own converting constructor from <code>basic_json</code>. As a result, <code>static_cast&lt;std::optional&lt;T&gt;&gt;(json_value)</code> goes through <code>std::optional&lt;T&gt;</code>'s own converting constructor rather than through <code>get&lt;std::optional&lt;T&gt;&gt;()</code>, which can behave differently -- for example, with a custom <code>adl_serializer&lt;std::optional&lt;T&gt;&gt;</code> specialization. Prefer <code>get&lt;std::optional&lt;T&gt;&gt;()</code>/<code>get_to()</code> over <code>static_cast</code> for optional types.</p> </div> <div class="admonition warning"> <p class=admonition-title>Converting to a fixed-size <code>std::array</code> does not check length</p> <p>Converting a <abbr title="JavaScript Object Notation">JSON</abbr> array to <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o>&lt;</span><span class=n>T</span><span class=p>,</span><span class=w> </span><span class=n>N</span><span class=o>&gt;</span></code> does not check that the <abbr title="JavaScript Object Notation">JSON</abbr> array's size matches <code>N</code>: if the <abbr title="JavaScript Object Notation">JSON</abbr> array is longer, the extra elements are silently dropped; if it is shorter, the remaining <code>std::array</code> elements are left default-constructed. No exception is thrown in either case.</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>{</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=p>,</span><span class=w> </span><span class=mi>4</span><span class=p>,</span><span class=w> </span><span class=mi>5</span><span class=p>};</span>
<span class=k>auto</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>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o>&lt;</span><span class=kt>int</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=o>&gt;&gt;</span><span class=p>();</span><span class=w> </span><span class=c1>// {1, 2, 3} -- elements 4 and 5 silently dropped</span>
</code></pre></div> </div> <h2 id=omitting-a-field-when-serializing-stdoptional>Omitting a field when serializing <code>std::optional</code><a class=headerlink href=#omitting-a-field-when-serializing-stdoptional title="Permanent link">&para;</a></h2> <p>By default, <code>to_json</code> for <code>std::optional&lt;T&gt;</code> writes either the value or <code class=highlight><span class=kc>null</span></code> -- there is no built-in way to make a field disappear from the serialized object entirely when the <code>std::optional</code> is <code>std::nullopt</code>. Because a specialization of <code>adl_serializer&lt;std::optional&lt;T&gt;&gt;</code> only controls how the <em>value</em> is converted (it cannot prevent the containing object's <code>to_json</code> from inserting the key in the first place), omission has to be implemented in the <em>containing</em> type's <code>to_json</code>:</p> <div class=highlight><pre><span></span><code><span class=k>struct</span><span class=w> </span><span class=nc>person</span><span class=w> </span><span class=p>{</span>
</code></pre></div> </div> <div class="admonition warning"> <p class=admonition-title><code>static_cast</code> and <code>get&lt;std::optional&lt;T&gt;&gt;()</code> are not guaranteed equivalent</p> <p><code>operator ValueType()</code> (used by <code>static_cast</code> and implicit conversions) intentionally excludes <code>std::optional&lt;T&gt;</code> from delegating to <code>get&lt;T&gt;()</code>, to avoid a constructor ambiguity with <code>std::optional&lt;T&gt;</code>'s own converting constructor from <code>basic_json</code>. As a result, <code>static_cast&lt;std::optional&lt;T&gt;&gt;(json_value)</code> goes through <code>std::optional&lt;T&gt;</code>'s own converting constructor rather than through <code>get&lt;std::optional&lt;T&gt;&gt;()</code>, which can behave differently -- for example, with a custom <code>adl_serializer&lt;std::optional&lt;T&gt;&gt;</code> specialization. Prefer <code>get&lt;std::optional&lt;T&gt;&gt;()</code>/<code>get_to()</code> over <code>static_cast</code> for optional types.</p> </div> <div class="admonition warning"> <p class=admonition-title>Converting to a fixed-size destination does not check the array size</p> <p>Some destination types have a size that is fixed by their C++ type rather than by the <abbr title="JavaScript Object Notation">JSON</abbr> value: <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>pair</span><span class=o>&lt;</span><span class=n>A</span><span class=p>,</span><span class=w> </span><span class=n>B</span><span class=o>&gt;</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>tuple</span><span class=o>&lt;</span><span class=n>Ts</span><span class=p>...</span><span class=o>&gt;</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o>&lt;</span><span class=n>T</span><span class=p>,</span><span class=w> </span><span class=n>N</span><span class=o>&gt;</span></code>, C arrays <code class=highlight><span class=n>T</span><span class=p>[</span><span class=n>N</span><span class=p>]</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 a non-string key type (which is read from an array of two-element arrays). All of them read exactly as many elements as they need via <a href=../../api/basic_json/at/ ><code>at</code></a> and <strong>never compare the <abbr title="JavaScript Object Notation">JSON</abbr> array's size to that number</strong>. The two mismatch directions therefore behave differently:</p> <ul> <li>The <abbr title="JavaScript Object Notation">JSON</abbr> array has <strong>too many</strong> elements: the surplus is <strong>silently discarded</strong>, and no exception is thrown.</li> <li>The <abbr title="JavaScript Object Notation">JSON</abbr> array has <strong>too few</strong> elements: <code>at</code> throws <a href=../../home/exceptions/#jsonexceptionout_of_range401><code>out_of_range.401</code></a> for the first missing index -- an out-of-range error, not a <a href=../../home/exceptions/#type-errors><code>type_error</code></a>, even though the cause is a shape mismatch.</li> </ul> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>{</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=p>,</span><span class=w> </span><span class=mi>4</span><span class=p>,</span><span class=w> </span><span class=mi>5</span><span class=p>};</span>
<span class=k>auto</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>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o>&lt;</span><span class=kt>int</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=o>&gt;&gt;</span><span class=p>();</span><span class=w> </span><span class=c1>// {1, 2, 3} -- elements 4 and 5 silently dropped</span>
<span class=k>auto</span><span class=w> </span><span class=n>p</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>pair</span><span class=o>&lt;</span><span class=kt>int</span><span class=p>,</span><span class=w> </span><span class=kt>int</span><span class=o>&gt;&gt;</span><span class=p>();</span><span class=w> </span><span class=c1>// (1, 2) -- elements 3, 4, and 5 silently dropped</span>
<span class=n>json</span><span class=w> </span><span class=n>k</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>{</span><span class=mi>1</span><span class=p>};</span>
<span class=k>auto</span><span class=w> </span><span class=n>q</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>k</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=n>pair</span><span class=o>&lt;</span><span class=kt>int</span><span class=p>,</span><span class=w> </span><span class=kt>int</span><span class=o>&gt;&gt;</span><span class=p>();</span><span class=w> </span><span class=c1>// ❌ throws out_of_range.401</span>
</code></pre></div> <p>If a size mismatch is an error in your application, check the size yourself before converting.</p> </div> <h2 id=omitting-a-field-when-serializing-stdoptional>Omitting a field when serializing <code>std::optional</code><a class=headerlink href=#omitting-a-field-when-serializing-stdoptional title="Permanent link">&para;</a></h2> <p>By default, <code>to_json</code> for <code>std::optional&lt;T&gt;</code> writes either the value or <code class=highlight><span class=kc>null</span></code> -- there is no built-in way to make a field disappear from the serialized object entirely when the <code>std::optional</code> is <code>std::nullopt</code>. Because a specialization of <code>adl_serializer&lt;std::optional&lt;T&gt;&gt;</code> only controls how the <em>value</em> is converted (it cannot prevent the containing object's <code>to_json</code> from inserting the key in the first place), omission has to be implemented in the <em>containing</em> type's <code>to_json</code>:</p> <div class=highlight><pre><span></span><code><span class=k>struct</span><span class=w> </span><span class=nc>person</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>string</span><span class=w> </span><span class=n>name</span><span class=p>;</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>optional</span><span class=o>&lt;</span><span class=kt>int</span><span class=o>&gt;</span><span class=w> </span><span class=n>age</span><span class=p>;</span>
<span class=p>};</span>
@@ -104,4 +109,4 @@
</code></pre></div> <div class="admonition info"> <p class=admonition-title>Constructing from a C++20 range view</p> <p>A <code>json</code> array can also be constructed directly from a C++20 range view (<code>std::ranges::view</code>), such as the result of <code>std::views::filter</code> or <code>std::views::transform</code> -- no intermediate container is needed:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=kt>int</span><span class=o>&gt;</span><span class=w> </span><span class=n>nums</span><span class=p>{</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=mi>37</span><span class=p>,</span><span class=w> </span><span class=mi>42</span><span class=p>,</span><span class=w> </span><span class=mi>21</span><span class=p>};</span>
<span class=k>auto</span><span class=w> </span><span class=n>filtered</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nums</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>views</span><span class=o>::</span><span class=n>filter</span><span class=p>([](</span><span class=kt>int</span><span class=w> </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>return</span><span class=w> </span><span class=n>i</span><span class=w> </span><span class=o>&gt;</span><span class=w> </span><span class=mi>10</span><span class=p>;</span><span class=w> </span><span class=p>});</span>
<span class=n>json</span><span class=w> </span><span class=nf>j</span><span class=p>(</span><span class=n>filtered</span><span class=p>);</span><span class=w> </span><span class=c1>// [37,42,21]</span>
</code></pre></div> <p>This requires <a href=../../api/macros/json_has_ranges/ ><code>JSON_HAS_RANGES</code></a> to be enabled and is unavailable on MinGW due to incomplete C++20 ranges support there.</p> </div> <h2 id=your-own-types>Your own types<a class=headerlink href=#your-own-types title="Permanent link">&para;</a></h2> <p>The conversions above are built in for standard types. To make the same syntax work for <strong>your own</strong> types, provide <code>to_json</code>/<code>from_json</code> functions (or use one of the convenience macros). This is described in detail on the <a href=../arbitrary_types/ >arbitrary types conversions</a> page. Enums can be mapped to strings as described in <a href=../enum_conversion/ >specializing enum conversion</a>.</p> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../../api/basic_json/get/ ><code>get</code></a> - get a copy converted to a given type</li> <li><a href=../../api/basic_json/get_to/ ><code>get_to</code></a> - convert into an existing variable</li> <li><a href=../../api/basic_json/get_ref/ ><code>get_ref</code></a> / <a href=../../api/basic_json/get_ptr/ ><code>get_ptr</code></a> - access the stored value without copying</li> <li><a href=../arbitrary_types/ >Arbitrary types conversions</a> - support your own types</li> <li><a href=../../api/macros/json_use_implicit_conversions/ ><code>JSON_USE_IMPLICIT_CONVERSIONS</code></a> - toggle implicit conversions</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="August 4, 2026 06:45:02 UTC">August 4, 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 &copy; 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://wLine truncated
</code></pre></div> <p>This requires <a href=../../api/macros/json_has_ranges/ ><code>JSON_HAS_RANGES</code></a> to be enabled and is unavailable on MinGW due to incomplete C++20 ranges support there.</p> </div> <h2 id=your-own-types>Your own types<a class=headerlink href=#your-own-types title="Permanent link">&para;</a></h2> <p>The conversions above are built in for standard types. To make the same syntax work for <strong>your own</strong> types, provide <code>to_json</code>/<code>from_json</code> functions (or use one of the convenience macros). This is described in detail on the <a href=../arbitrary_types/ >arbitrary types conversions</a> page. Enums can be mapped to strings as described in <a href=../enum_conversion/ >specializing enum conversion</a>.</p> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../../api/basic_json/get/ ><code>get</code></a> - get a copy converted to a given type</li> <li><a href=../../api/basic_json/get_to/ ><code>get_to</code></a> - convert into an existing variable</li> <li><a href=../../api/basic_json/get_ref/ ><code>get_ref</code></a> / <a href=../../api/basic_json/get_ptr/ ><code>get_ptr</code></a> - access the stored value without copying</li> <li><a href=../arbitrary_types/ >Arbitrary types conversions</a> - support your own types</li> <li><a href=../../api/macros/json_use_implicit_conversions/ ><code>JSON_USE_IMPLICIT_CONVERSIONS</code></a> - toggle implicit conversions</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="August 4, 2026 06:45:50 UTC">August 4, 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 &copy; 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://wLine truncated
+14 -4
View File
@@ -124,7 +124,7 @@ auto refs = j.get<std::tuple<double&, std::string&>>();
std::get<1>(refs) = "world"; // modifies j[1] in place
```
A referenced type must be one the library actually stores (or an arithmetic type it can convert to/from); otherwise this is a compile error.
A referenced element must name the type the library actually *stores* — one of [`boolean_t`](https://json.nlohmann.me/api/basic_json/boolean_t/index.md), [`number_integer_t`](https://json.nlohmann.me/api/basic_json/number_integer_t/index.md), [`number_unsigned_t`](https://json.nlohmann.me/api/basic_json/number_unsigned_t/index.md), [`number_float_t`](https://json.nlohmann.me/api/basic_json/number_float_t/index.md), [`string_t`](https://json.nlohmann.me/api/basic_json/string_t/index.md), [`binary_t`](https://json.nlohmann.me/api/basic_json/binary_t/index.md), [`array_t`](https://json.nlohmann.me/api/basic_json/array_t/index.md), or [`object_t`](https://json.nlohmann.me/api/basic_json/object_t/index.md). There is nothing else to refer to, so a reference to any other type is a compile error even when a conversion would exist: `std::tuple<int&>` is rejected, because the library stores a `number_integer_t` (`std::int64_t` by default) and not an `int`. This restriction applies only to reference elements — a plain `std::tuple<int>` converts by value as usual.
## Implicit conversions
@@ -161,15 +161,25 @@ j_null.get_to(opt); // ✅ std::nullopt
`operator ValueType()` (used by `static_cast` and implicit conversions) intentionally excludes `std::optional<T>` from delegating to `get<T>()`, to avoid a constructor ambiguity with `std::optional<T>`'s own converting constructor from `basic_json`. As a result, `static_cast<std::optional<T>>(json_value)` goes through `std::optional<T>`'s own converting constructor rather than through `get<std::optional<T>>()`, which can behave differently -- for example, with a custom `adl_serializer<std::optional<T>>` specialization. Prefer `get<std::optional<T>>()`/`get_to()` over `static_cast` for optional types.
Converting to a fixed-size `std::array` does not check length
Converting to a fixed-size destination does not check the array size
Converting a JSON array to `std::array<T, N>` does not check that the JSON array's size matches `N`: if the JSON array is longer, the extra elements are silently dropped; if it is shorter, the remaining `std::array` elements are left default-constructed. No exception is thrown in either case.
Some destination types have a size that is fixed by their C++ type rather than by the JSON value: `std::pair<A, B>`, `std::tuple<Ts...>`, `std::array<T, N>`, C arrays `T[N]`, and `std::map`/`std::unordered_map` with a non-string key type (which is read from an array of two-element arrays). All of them read exactly as many elements as they need via [`at`](https://json.nlohmann.me/api/basic_json/at/index.md) and **never compare the JSON array's size to that number**. The two mismatch directions therefore behave differently:
- The JSON array has **too many** elements: the surplus is **silently discarded**, and no exception is thrown.
- The JSON array has **too few** elements: `at` throws [`out_of_range.401`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range401) for the first missing index -- an out-of-range error, not a [`type_error`](https://json.nlohmann.me/home/exceptions/#type-errors), even though the cause is a shape mismatch.
```
json j = {1, 2, 3, 4, 5};
auto a = j.get<std::array<int, 3>>(); // {1, 2, 3} -- elements 4 and 5 silently dropped
auto a = j.get<std::array<int, 3>>(); // {1, 2, 3} -- elements 4 and 5 silently dropped
auto p = j.get<std::pair<int, int>>(); // (1, 2) -- elements 3, 4, and 5 silently dropped
json k = {1};
auto q = k.get<std::pair<int, int>>(); // ❌ throws out_of_range.401
```
If a size mismatch is an error in your application, check the size yourself before converting.
## Omitting a field when serializing `std::optional`
By default, `to_json` for `std::optional<T>` writes either the value or `null` -- there is no built-in way to make a field disappear from the serialized object entirely when the `std::optional` is `std::nullopt`. Because a specialization of `adl_serializer<std::optional<T>>` only controls how the *value* is converted (it cannot prevent the containing object's `to_json` from inserting the key in the first place), omission has to be implemented in the *containing* type's `to_json`: