This commit is contained in:
nlohmann committed 2026-10-15 12:09:22 +00:00
1 parent dcc7a74a18
commit 98c9bea1d1
105 files changed
+537 -508

No files matched your search

+3 -3
View File
@@ -8,7 +8,7 @@
<span class=n>json</span><span class=w> </span><span class=n>obj</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>{{</span><span class=s>"key"</span><span class=p>,</span><span class=w> </span><span class=s>"value"</span><span class=p>}};</span>
<span class=n>json</span><span class=w> </span><span class=n>j</span><span class=p>{</span><span class=n>obj</span><span class=p>};</span><span class=w> </span><span class=c1>// -&gt; {"key":"value"} (copy, not array)</span>
</code></pre></div> <p>Without the macro (default behavior), <code>json j{obj}</code> creates <code>[{"key":"value"}]</code>. This opt-in macro fixes issue #5074 while preserving backwards compatibility for existing code.</p> <h2 id=limitations>Limitations<a class=headerlink href=#limitations title="Permanent link">&para;</a></h2> <h3 id=relaxed-parsing>Relaxed parsing<a class=headerlink href=#relaxed-parsing title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Can you add an option to ignore trailing commas?</p> </div> <p>This library does not support any feature that would jeopardize interoperability.</p> <h3 id=parse-errors-reading-non-ascii-characters>Parse errors reading non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters<a class=headerlink href=#parse-errors-reading-non-ascii-characters title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why is the parser complaining about a Chinese character?</li> <li>Does the library support Unicode?</li> <li>I get an exception <code>[json.exception.parse_error.101] parse error at line 1, column 53: syntax error while parsing value - invalid string: ill-formed UTF-8 byte; last read: '"Testé$')"</code></li> </ul> </div> <p>The library supports <strong>Unicode input</strong> as follows:</p> <ul> <li>Only <strong><abbr title="Unicode Transformation Format">UTF</abbr>-8</strong> encoded input is supported, which is the default encoding for <abbr title="JavaScript Object Notation">JSON</abbr>, according to <a href="https://tools.ietf.org/html/rfc8259.html#section-8.1"><abbr title="Request for Comments">RFC</abbr> 8259</a>.</li> <li><code>std::u16string</code> and <code>std::u32string</code> can be parsed, assuming <abbr title="Unicode Transformation Format">UTF</abbr>-16 and <abbr title="Unicode Transformation Format">UTF</abbr>-32 encoding, respectively. These encodings are not supported when reading from files or other input containers.</li> <li>Other encodings such as Latin-1 or <abbr title="International Organization for Standardization">ISO</abbr> 8859-1 are <strong>not</strong> supported and will yield parse or serialization errors.</li> <li>The library will not replace <a href="http://www.unicode.org/faq/private_use.html#nonchar1">Unicode noncharacters</a>.</li> <li>Invalid surrogates (e.g., incomplete pairs such as <code>\uDEAD</code>) will yield parse errors.</li> <li>The strings stored in the library are <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded. When using the default string type (<code>std::string</code>), note that its length/size functions return the number of stored bytes rather than the number of characters or glyphs.</li> <li>When you store strings with different encodings in the library, calling <a href=../../api/basic_json/dump/ ><code>dump()</code></a> may throw an exception unless <code>json::error_handler_t::replace</code>, <code>json::error_handler_t::ignore</code>, or <code>json::error_handler_t::keep</code> are used as error handlers.</li> </ul> <p>In most cases, the parser is right to complain, because the input is not <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded. This is especially true for Microsoft Windows, where Latin-1 or <abbr title="International Organization for Standardization">ISO</abbr> 8859-1 is often the standard encoding.</p> <h3 id=nul-bytes-in-the-input>NUL bytes in the input<a class=headerlink href=#nul-bytes-in-the-input title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why does <a href=../../api/basic_json/parse/ ><code>json::parse()</code></a> silently ignore part of my input?</li> <li>Why does a <code>std::string</code>/buffer with extra data after the <abbr title="JavaScript Object Notation">JSON</abbr> text parse without error, while a similar-looking string with extra text does not?</li> </ul> </div> <p>A <code>'\0'</code> (NUL) byte anywhere in the input is treated the same as the real end of the input, rather than as an ordinary (and, outside of a string, invalid) byte. Everything from that byte onward is silently ignored, without a parse error — including further, otherwise well-formed <abbr title="JavaScript Object Notation">JSON</abbr>:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=p>(</span><span class=s>"123"</span><span class=p>)</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=sc>'\0'</span><span class=p>);</span><span class=w> </span><span class=c1>// == 123, no error</span>
</code></pre></div> <p>Without the macro (default behavior), <code>json j{obj}</code> creates <code>[{"key":"value"}]</code>. This opt-in macro fixes issue #5074 while preserving backwards compatibility for existing code.</p> <h2 id=limitations>Limitations<a class=headerlink href=#limitations title="Permanent link">&para;</a></h2> <h3 id=relaxed-parsing>Relaxed parsing<a class=headerlink href=#relaxed-parsing title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Can you add an option to ignore trailing commas?</p> </div> <p>Yes, trailing commas in arrays and objects can be allowed by setting parameter <code>ignore_trailing_commas</code> of <a href=../../api/basic_json/parse/ ><code>parse</code></a> to <code class=highlight><span class=nb>true</span></code>; see <a href=../../features/trailing_commas/ >trailing commas</a>. They are rejected by default, and the library never adds them when serializing.</p> <h3 id=parse-errors-reading-non-ascii-characters>Parse errors reading non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters<a class=headerlink href=#parse-errors-reading-non-ascii-characters title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why is the parser complaining about a Chinese character?</li> <li>Does the library support Unicode?</li> <li>I get an exception <code>[json.exception.parse_error.101] parse error at line 1, column 53: syntax error while parsing value - invalid string: ill-formed UTF-8 byte; last read: '"Testé$')"</code></li> </ul> </div> <p>The library supports <strong>Unicode input</strong> as follows:</p> <ul> <li>Only <strong><abbr title="Unicode Transformation Format">UTF</abbr>-8</strong> encoded input is supported, which is the default encoding for <abbr title="JavaScript Object Notation">JSON</abbr>, according to <a href="https://tools.ietf.org/html/rfc8259.html#section-8.1"><abbr title="Request for Comments">RFC</abbr> 8259</a>.</li> <li><code>std::u16string</code> and <code>std::u32string</code> can be parsed, assuming <abbr title="Unicode Transformation Format">UTF</abbr>-16 and <abbr title="Unicode Transformation Format">UTF</abbr>-32 encoding, respectively. These encodings are not supported when reading from files or other input containers.</li> <li>Other encodings such as Latin-1 or <abbr title="International Organization for Standardization">ISO</abbr> 8859-1 are <strong>not</strong> supported and will yield parse or serialization errors.</li> <li>The library will not replace <a href="http://www.unicode.org/faq/private_use.html#nonchar1">Unicode noncharacters</a>.</li> <li>Invalid surrogates (e.g., incomplete pairs such as <code>\uDEAD</code>) will yield parse errors.</li> <li>The strings stored in the library are <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded. When using the default string type (<code>std::string</code>), note that its length/size functions return the number of stored bytes rather than the number of characters or glyphs.</li> <li>When you store strings with different encodings in the library, calling <a href=../../api/basic_json/dump/ ><code>dump()</code></a> may throw an exception unless <code>json::error_handler_t::replace</code>, <code>json::error_handler_t::ignore</code>, or <code>json::error_handler_t::keep</code> are used as error handlers.</li> </ul> <p>In most cases, the parser is right to complain, because the input is not <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded. This is especially true for Microsoft Windows, where Latin-1 or <abbr title="International Organization for Standardization">ISO</abbr> 8859-1 is often the standard encoding.</p> <h3 id=nul-bytes-in-the-input>NUL bytes in the input<a class=headerlink href=#nul-bytes-in-the-input title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why does <a href=../../api/basic_json/parse/ ><code>json::parse()</code></a> silently ignore part of my input?</li> <li>Why does a <code>std::string</code>/buffer with extra data after the <abbr title="JavaScript Object Notation">JSON</abbr> text parse without error, while a similar-looking string with extra text does not?</li> </ul> </div> <p>A <code>'\0'</code> (NUL) byte anywhere in the input is treated the same as the real end of the input, rather than as an ordinary (and, outside of a string, invalid) byte. Everything from that byte onward is silently ignored, without a parse error — including further, otherwise well-formed <abbr title="JavaScript Object Notation">JSON</abbr>:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=p>(</span><span class=s>"123"</span><span class=p>)</span><span class=w>Line truncated
<span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=p>(</span><span class=s>"123"</span><span class=p>)</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=sc>'\0'</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=s>"true"</span><span class=p>);</span><span class=w> </span><span class=c1>// == 123, the "true" is silently ignored too</span>
</code></pre></div> <p>This is different from any other unexpected trailing byte, which <em>does</em> raise <a href=../exceptions/#jsonexceptionparse_error101><code>parse_error.101</code></a>:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"123x"</span><span class=p>);</span><span class=w> </span><span class=c1>// throws parse_error.101: unexpected additional data</span>
</code></pre></div> <p>This falls out of the same convention used when no explicit input length is given at all: <code>json::parse(const char*)</code> already stops at the first NUL byte via <code>strlen()</code>, since a bare pointer has no length of its own. The library applies that same NUL-terminated-C-string convention uniformly, rather than only when a length is genuinely unavailable — so a <code>std::string</code>, iterator range, or container whose content happens to include a NUL byte is affected the same way a raw <code>const char*</code> would be.</p> <p>If your input may contain a trailing or embedded NUL that is <strong>not</strong> meant to signal the end of the <abbr title="JavaScript Object Notation">JSON</abbr> text — for instance, a fixed-size, zero-padded buffer — trim it yourself before calling <code>parse()</code>, since the library will otherwise silently stop there instead of raising an error:</p> <div class=highlight><pre><span></span><code><span class=n>s</span><span class=p>.</span><span class=n>resize</span><span class=p>(</span><span class=n>s</span><span class=p>.</span><span class=n>find</span><span class=p>(</span><span class=sc>'\0'</span><span class=p>));</span><span class=w> </span><span class=c1>// drop everything from the first NUL onward, if any</span>
@@ -39,7 +39,7 @@
<span class=w> </span><span class=nt>"encoded"</span><span class=p>:</span><span class=w> </span><span class=s2>"車B1234 こんにちは"</span><span class=p>,</span>
<span class=w> </span><span class=nt>"original"</span><span class=p>:</span><span class=w> </span><span class=p>[</span><span class=mi>36554</span><span class=p>,</span><span class=w> </span><span class=mi>66</span><span class=p>,</span><span class=w> </span><span class=mi>49</span><span class=p>,</span><span class=w> </span><span class=mi>50</span><span class=p>,</span><span class=w> </span><span class=mi>51</span><span class=p>,</span><span class=w> </span><span class=mi>52</span><span class=p>,</span><span class=w> </span><span class=mi>32</span><span class=p>,</span><span class=w> </span><span class=mi>12371</span><span class=p>,</span><span class=w> </span><span class=mi>12435</span><span class=p>,</span><span class=w> </span><span class=mi>12395</span><span class=p>,</span><span class=w> </span><span class=mi>12385</span><span class=p>,</span><span class=w> </span><span class=mi>12399</span><span class=p>]</span>
<span class=p>}</span>
</code></pre></div> </div> <h2 id=usage>Usage<a class=headerlink href=#usage title="Permanent link">&para;</a></h2> <h3 id=thread-safety>Thread safety<a class=headerlink href=#thread-safety title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Is <code>basic_json</code> thread-safe?</p> </div> <p>No. <code>basic_json</code> provides no built-in synchronization, the same as <code>std::map</code> or <code>std::vector</code>. Concurrent reads of the same value from multiple threads are safe, as are concurrent (non-overlapping) accesses to independent <code>json</code> objects. However, any concurrent write to a <code>json</code> object -- or a concurrent read while another thread writes to the same object -- is a data race and requires external synchronization (e.g., a <code>std::mutex</code>) by the caller.</p> <h3 id=schema-validation>Schema validation<a class=headerlink href=#schema-validation title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Does this library support <abbr title="JavaScript Object Notation">JSON</abbr> Schema validation?</p> </div> <p>Not directly, but the companion project <a href="https://github.com/pboettch/json-schema-validator">json-schema-validator</a> builds <abbr title="JavaScript Object Notation">JSON</abbr> Schema (draft 7; draft 4 in its older, now-superseded 1.x releases) validation on top of this library and is a common recommendation for this use case.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">&para;</a></h2> <h3 id=parsing-without-exceptions>Parsing without exceptions<a class=headerlink href=#parsing-without-exceptions title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Is it possible to indicate a parse error without throwing an exception?</p> </div> <p>Yes, see <a href=../../features/parsing/parse_exceptions/ >Parsing and exceptions</a>.</p> <h3 id=key-name-in-exceptions>Key name in exceptions<a class=headerlink href=#key-name-in-exceptions title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Can I get the key of the object item that caused an exception?</p> </div> <p>Yes, you can. Please define the symbol <a href=../../api/macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> to get <a href=../exceptions/#extended-diagnostic-messages>extended diagnostics messages</a>.</p> <h2 id=serialization-issues>Serialization issues<a class=headerlink href=#serialization-issues title="Permanent link">&para;</a></h2> <h3 id=number-precision>Number precision<a class=headerlink href=#number-precision title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <ul> <li>It seems that precision is lost when serializing a double.</li> <li>Can I change the precision for floating-point serialization?</li> </ul> </div> <p>The library uses <code>std::numeric_limits&lt;number_float_t&gt;::digits10</code> (15 for <abbr title="Institute of Electrical and Electronics Engineers">IEEE</abbr> <code>double</code>s) digits for serialization. This value is sufficient to guarantee roundtripping. If one uses more than this number of digits of precision, then string -&gt; value -&gt; string is not guaranteed to round-trip.</p> <div class="admonition quote"> <p class=admonition-title><a href="https://en.cppreference.com/w/cpp/types/numeric_limits/digits10">cppreference.com</a></p> <p>The value of <code>std::numeric_limits&lt;T&gt;::digits10</code> is the number of base-10 digits that can be represented by the type T without change, that is, any number with this many significant decimal digits can be converted to a value of type T and back to decimal form, without change due to rounding or overflow. </p> </div> <div class="admonition tip"> <p class=admonition-title>Tip</p> <p>The website <a href="https://float.exposed">https://float.exposed</a> gives a good insight into the internal storage of floating-point numbers.</p> </div> <p>See <a href=../../features/types/number_handling/#number-serialization>this section</a> on the library's number handling for more information.</p> <h3 id=serializing-untrusted-or-invalid-utf-8>Serializing untrusted or invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8<a class=headerlink href=#serializing-untrusted-or-invalid-utf-8 title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why does <code>dump()</code> throw when I serialize data that came from the network?</li> <li>Is CVE-2024-34363 a vulnerability in this library?</li> </ul> </div> <p>Crashes reported against this library that stem from an uncaught <a href=../exceptions/#jsonexceptiontype_error316><code>type_error.316</code></a> while serializing unvalidated input (e.g., CVE-2024-34363) aLine truncated
</code></pre></div> </div> <h2 id=usage>Usage<a class=headerlink href=#usage title="Permanent link">&para;</a></h2> <h3 id=thread-safety>Thread safety<a class=headerlink href=#thread-safety title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Is <code>basic_json</code> thread-safe?</p> </div> <p>No. <code>basic_json</code> provides no built-in synchronization, the same as <code>std::map</code> or <code>std::vector</code>. Concurrent reads of the same value from multiple threads are safe, as are concurrent (non-overlapping) accesses to independent <code>json</code> objects. However, any concurrent write to a <code>json</code> object -- or a concurrent read while another thread writes to the same object -- is a data race and requires external synchronization (e.g., a <code>std::mutex</code>) by the caller.</p> <h3 id=schema-validation>Schema validation<a class=headerlink href=#schema-validation title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Does this library support <abbr title="JavaScript Object Notation">JSON</abbr> Schema validation?</p> </div> <p>Not directly, but the companion project <a href="https://github.com/pboettch/json-schema-validator">json-schema-validator</a> builds <abbr title="JavaScript Object Notation">JSON</abbr> Schema (draft 7; draft 4 in its older, now-superseded 1.x releases) validation on top of this library and is a common recommendation for this use case.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">&para;</a></h2> <h3 id=parsing-without-exceptions>Parsing without exceptions<a class=headerlink href=#parsing-without-exceptions title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Is it possible to indicate a parse error without throwing an exception?</p> </div> <p>Yes, see <a href=../../features/parsing/parse_exceptions/ >Parsing and exceptions</a>.</p> <h3 id=key-name-in-exceptions>Key name in exceptions<a class=headerlink href=#key-name-in-exceptions title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Can I get the key of the object item that caused an exception?</p> </div> <p>Yes, you can. Please define the symbol <a href=../../api/macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a> to get <a href=../exceptions/#extended-diagnostic-messages>extended diagnostics messages</a>.</p> <h2 id=serialization-issues>Serialization issues<a class=headerlink href=#serialization-issues title="Permanent link">&para;</a></h2> <h3 id=number-precision>Number precision<a class=headerlink href=#number-precision title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <ul> <li>It seems that precision is lost when serializing a double.</li> <li>Can I change the precision for floating-point serialization?</li> </ul> </div> <p>The library serializes floating-point numbers with the shortest representation that parses back to the same value, using at most <code>std::numeric_limits&lt;number_float_t&gt;::max_digits10</code> (17 for <abbr title="Institute of Electrical and Electronics Engineers">IEEE</abbr> <code>double</code>s) significant digits. This guarantees that value -&gt; string -&gt; value round-trips. The reverse direction, string -&gt; value -&gt; string, is only guaranteed for numbers with at most <code>std::numeric_limits&lt;number_float_t&gt;::digits10</code> (15 for <abbr title="Institute of Electrical and Electronics Engineers">IEEE</abbr> <code>double</code>s) significant digits; a number with more digits may not be representable exactly and is then serialized as the closest <code>double</code>.</p> <div class="admonition quote"> <p class=admonition-title><a href="https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10">cppreference.com</a></p> <p>The value of <code>std::numeric_limits&lt;T&gt;::max_digits10</code> is the number of base-10 digits that are necessary to uniquely represent all distinct values of the type T, such as necessary for serialization/deserialization to text.</p> </div> <div class="admonition tip"> <p class=admonition-title>Tip</p> <p>The website <a href="https://float.exposed">https://float.exposed</a> gives a good insight into the internal storage of floating-point numbers.</p> </div> <p>See <a href=../../features/types/number_handling/#number-serialization>this section</a> on the library's number handling for more information.</p> <h3 id=serializing-untrusted-or-invalid-utf-8>Serializing untrusted or invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8<a class=headerlink href=#serializing-untrusted-or-invalid-utf-8 title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why does <code>dump()</code> throw when I serializLine truncated
<span class=k>const</span><span class=w> </span><span class=k>auto</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=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</span><span class=o>::</span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>replace</span><span class=p>);</span>
</code></pre></div> <h3 id=using-json-values-with-stdformat-or-fmt>Using <abbr title="JavaScript Object Notation">JSON</abbr> values with <code>std::format</code> or <code>fmt</code><a class=headerlink href=#using-json-values-with-stdformat-or-fmt title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <ul> <li>Can I use <code>std::format("{}", j)</code> on a <abbr title="JavaScript Object Notation">JSON</abbr> value?</li> <li>Can I use <code>fmt::format("{}", j)</code> or <code>fmt::print("{}", j)</code> (the <a href="https://github.com/fmtlib/fmt">{fmt}</a> library) on a <abbr title="JavaScript Object Notation">JSON</abbr> value?</li> </ul> </div> <p><code>std::format</code> works out of the box since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, as long as the standard library provides <code>&lt;format&gt;</code> (see <a href=../../api/macros/json_has_std_format/ ><code>JSON_HAS_STD_FORMAT</code></a>); see <a href=../../api/basic_json/std_formatter/ ><code>std::formatter&lt;basic_json&gt;</code></a> for details, including the <code class=highlight><span class=s>"{:#}"</span></code> pretty-print spec, indent widths (<code class=highlight><span class=s>"{:2}"</span></code>), and custom indent characters (<code class=highlight><span class=s>"{:.&gt;#}"</span></code>).</p> <p>For <code>fmt</code>, the library ships <a href=../../api/basic_json/format_as/ ><code>format_as</code></a>, a small customization point <code>fmt</code> looks for via argument-dependent lookup. It only has an effect on fmt 10.0.0 through 11.0.2 — from fmt 11.1.0 onwards, <code>fmt</code> no longer picks up a <code>format_as</code> overload that returns a <code>std::string</code>. On such versions (or any version, if you also want the same <code class=highlight><span class=s>"{:#}"</span></code>/width/fill-and-align spec support that <code>std::formatter&lt;basic_json&gt;</code> has), define your own <code>fmt::formatter</code> specialization; see <a href=../../api/basic_json/format_as/ ><code>format_as</code></a> for a recipe that mirrors it.</p> <p>If you get ambiguous-overload errors when passing a <abbr title="JavaScript Object Notation">JSON</abbr> value to <code>fmt::format</code>/<code>fmt::print</code> without any <code>fmt::formatter&lt;json&gt;</code> specialization in scope, that's <code>fmt</code> picking up <code>basic_json</code>'s implicit <code>operator ValueType()</code> conversion operator (see <a href="https://github.com/nlohmann/json/issues/964">#964</a> and <a href="https://github.com/nlohmann/json/issues/958">#958</a>); disabling it via <a href=../../api/macros/json_use_implicit_conversions/ ><code>JSON_USE_IMPLICIT_CONVERSIONS 0</code></a> avoids the ambiguity.</p> <h2 id=compilation-issues>Compilation issues<a class=headerlink href=#compilation-issues title="Permanent link">&para;</a></h2> <h3 id=android-sdk>Android <abbr title="Software Development Kit">SDK</abbr><a class=headerlink href=#android-sdk title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Why does the code not compile with Android <abbr title="Software Development Kit">SDK</abbr>?</p> </div> <p>Since <a href="https://github.com/android/ndk/wiki/Changelog-r18"><abbr title="Native Development Kit">NDK</abbr> r18</a> (2018), <abbr title="GNU Compiler Collection">GCC</abbr> and the <code>gnustl</code>/<code>stlport</code> C++ libraries have been removed from the Android <abbr title="Native Development Kit">NDK</abbr>; Clang and <code>libc++</code> are now the only compiler and C++ library, and they support C++11 and later out of the box. With a current <abbr title="Native Development Kit">NDK</abbr>, no special configuration is needed to use this library.</p> <p>Only very old NDKs (before r18), which defaulted to <abbr title="GNU Compiler Collection">GCC</abbr> and <code>gnustl</code>, lacked C++11 library features such as <code>std::to_string</code>. If you run into this, update to a current <abbr title="Native Development Kit">NDK</abbr>.</p> <h3 id=incomplete-detector-type-with-gcc-11>Incomplete <code>detector</code> type with <abbr title="GNU Compiler Collection">GCC</abbr> &lt; 11<a class=headerlink href=#incomplete-detector-type-with-gcc-11 title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Question</p> <p>Why does <abbr title="GNU Compiler Collection">GCC</abbr> 10 or older fail with <code>invalid use of incomplete type 'struct nlohmann::detail::detector&lt;..., to_json_function, ...&gt;'</code> for a type that holds an <code>optional</code> member?</p> </div> <p>This happens with <abbr title="GNU Compiler Collection">GCC</abbr> 10 and older in C++11/C++14 mode when all of these hold:</p> <ul> <li>a class <code>Holder</code> has an <code>optional&lt;Dummy&gt;</code> member (e.g., <code>boost::optiLine truncated
<span class=w> </span><span class=k>public</span><span class=o>:</span>
@@ -56,4 +56,4 @@
<span class=w> </span><span class=k>friend</span><span class=w> </span><span class=kt>void</span><span class=w> </span><span class=nf>to_json</span><span class=p>(</span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>&amp;</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>Holder</span><span class=o>&amp;</span><span class=w> </span><span class=n>h</span><span class=p>)</span><span class=w> </span><span class=p>{</span><span class=w> </span><span class=cm>/* ... */</span><span class=w> </span><span class=p>}</span>
<span class=p>};</span>
</code></pre></div> <p>The <a href=../../api/macros/nlohmann_define_type_intrusive/ ><code>NLOHMANN_DEFINE_TYPE_INTRUSIVE</code></a> macros define hidden friends as well. See <a href="https://github.com/nlohmann/json/issues/3669">#3669</a> for details.</p> <h3 id=missing-stl-function>Missing <abbr title="Standard Template Library">STL</abbr> function<a class=headerlink href=#missing-stl-function title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why do I get a compilation error <code>'to_string' is not a member of 'std'</code> (or similarly, for <code>strtod</code> or <code>strtof</code>)?</li> <li>Why does the code not compile with MinGW or Android <abbr title="Software Development Kit">SDK</abbr>?</li> </ul> </div> <p>This is not an issue with the code, but rather with the compiler itself. On Android, use a current <abbr title="Native Development Kit">NDK</abbr> (see above). For MinGW, please refer to <a href="http://tehsausage.com/mingw-to-string">this site</a> and <a href="https://github.com/nlohmann/json/issues/136">this discussion</a> for information on how to fix this bug.</p> <!-- 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 7, 2026 17:17:57 UTC">October 7, 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://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, CodLine truncated
</code></pre></div> <p>The <a href=../../api/macros/nlohmann_define_type_intrusive/ ><code>NLOHMANN_DEFINE_TYPE_INTRUSIVE</code></a> macros define hidden friends as well. See <a href="https://github.com/nlohmann/json/issues/3669">#3669</a> for details.</p> <h3 id=missing-stl-function>Missing <abbr title="Standard Template Library">STL</abbr> function<a class=headerlink href=#missing-stl-function title="Permanent link">&para;</a></h3> <div class="admonition question"> <p class=admonition-title>Questions</p> <ul> <li>Why do I get a compilation error <code>'to_string' is not a member of 'std'</code> (or similarly, for <code>strtod</code> or <code>strtof</code>)?</li> <li>Why does the code not compile with MinGW or Android <abbr title="Software Development Kit">SDK</abbr>?</li> </ul> </div> <p>This is not an issue with the code, but rather with the compiler itself. On Android, use a current <abbr title="Native Development Kit">NDK</abbr> (see above). For MinGW, please refer to <a href="http://tehsausage.com/mingw-to-string">this site</a> and <a href="https://github.com/nlohmann/json/issues/136">this discussion</a> for information on how to fix this bug.</p> <!-- 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 14, 2026 15:40:55 UTC">October 14, 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://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, CLine truncated
+4 -4
View File
@@ -66,7 +66,7 @@ Question
Can you add an option to ignore trailing commas?
This library does not support any feature that would jeopardize interoperability.
Yes, trailing commas in arrays and objects can be allowed by setting parameter `ignore_trailing_commas` of [`parse`](https://json.nlohmann.me/api/basic_json/parse/index.md) to `true`; see [trailing commas](https://json.nlohmann.me/features/trailing_commas/index.md). They are rejected by default, and the library never adds them when serializing.
### Parse errors reading non-ASCII characters
@@ -220,11 +220,11 @@ Question
- It seems that precision is lost when serializing a double.
- Can I change the precision for floating-point serialization?
The library uses `std::numeric_limits<number_float_t>::digits10` (15 for IEEE `double`s) digits for serialization. This value is sufficient to guarantee roundtripping. If one uses more than this number of digits of precision, then string -> value -> string is not guaranteed to round-trip.
The library serializes floating-point numbers with the shortest representation that parses back to the same value, using at most `std::numeric_limits<number_float_t>::max_digits10` (17 for IEEE `double`s) significant digits. This guarantees that value -> string -> value round-trips. The reverse direction, string -> value -> string, is only guaranteed for numbers with at most `std::numeric_limits<number_float_t>::digits10` (15 for IEEE `double`s) significant digits; a number with more digits may not be representable exactly and is then serialized as the closest `double`.
[cppreference.com](https://en.cppreference.com/w/cpp/types/numeric_limits/digits10)
[cppreference.com](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10)
The value of `std::numeric_limits<T>::digits10` is the number of base-10 digits that can be represented by the type T without change, that is, any number with this many significant decimal digits can be converted to a value of type T and back to decimal form, without change due to rounding or overflow.
The value of `std::numeric_limits<T>::max_digits10` is the number of base-10 digits that are necessary to uniquely represent all distinct values of the type T, such as necessary for serialization/deserialization to text.
Tip