mirror of
https://github.com/nlohmann/json.git
synced 2026-10-08 23:47:13 +00:00
deploy: ed8ba0201f
This commit is contained in:
1 parent
4e5f6962d3
commit
207a4df1e4
20 files changed
+356
-311
No files matched your search
@@ -64,6 +64,7 @@ serialization fails by default. The fourth argument of `dump` selects an
|
||||
- `strict` (default) — throw a [`type_error.316`](../home/exceptions.md#jsonexceptiontype_error316) exception.
|
||||
- `replace` — replace invalid bytes with the Unicode replacement character U+FFFD (`�`).
|
||||
- `ignore` — silently drop invalid bytes.
|
||||
- `keep` — copy invalid bytes to the output unchanged; the result is not valid UTF-8.
|
||||
|
||||
??? example "Example: serialize invalid UTF-8 with different error handlers"
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@
|
||||
</code></pre></div> </details> <p>The indentation character can be changed with the second argument (e.g., a tab <code class=highlight><span class=sc>'\t'</span></code>). An <code>indent</code> of <code>0</code> inserts newlines but no leading spaces, and the default of <code class=highlight><span class=mi>-1</span></code> selects the compact single-line form.</p> <h2 id=non-ascii-characters>Non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters<a class=headerlink href=#non-ascii-characters title="Permanent link">¶</a></h2> <p>Strings are stored and serialized as <abbr title="Unicode Transformation Format">UTF</abbr>-8 (see <a href=../types/#strings>types</a>). By default, <code>dump</code> copies valid non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters as-is. Setting the third argument <code>ensure_ascii</code> to <code class=highlight><span class=nb>true</span></code> escapes all non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters with <code>\uXXXX</code> sequences, so that the output contains only <abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters:</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>"苹果"</span><span class=p>;</span>
|
||||
<span class=n>j</span><span class=p>.</span><span class=n>dump</span><span class=p>();</span><span class=w> </span><span class=c1>// "苹果"</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>true</span><span class=p>);</span><span class=w> </span><span class=c1>// "苹果"</span>
|
||||
</code></pre></div> <h2 id=handling-invalid-utf-8>Handling invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8<a class=headerlink href=#handling-invalid-utf-8 title="Permanent link">¶</a></h2> <p>If a string contains invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8 sequences (for example, because it holds data in another encoding such as Latin-1), serialization fails by default. The fourth argument of <code>dump</code> selects an <a href=../../api/basic_json/error_handler_t/ ><code>error_handler</code></a>:</p> <ul> <li><code>strict</code> (default) — throw a <a href=../../home/exceptions/#jsonexceptiontype_error316><code>type_error.316</code></a> exception.</li> <li><code>replace</code> — replace invalid bytes with the Unicode replacement character U+FFFD (<code>�</code>).</li> <li><code>ignore</code> — silently drop invalid bytes.</li> </ul> <details class=example> <summary>Example: serialize invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8 with different error handlers</summary> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
</code></pre></div> <h2 id=handling-invalid-utf-8>Handling invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8<a class=headerlink href=#handling-invalid-utf-8 title="Permanent link">¶</a></h2> <p>If a string contains invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8 sequences (for example, because it holds data in another encoding such as Latin-1), serialization fails by default. The fourth argument of <code>dump</code> selects an <a href=../../api/basic_json/error_handler_t/ ><code>error_handler</code></a>:</p> <ul> <li><code>strict</code> (default) — throw a <a href=../../home/exceptions/#jsonexceptiontype_error316><code>type_error.316</code></a> exception.</li> <li><code>replace</code> — replace invalid bytes with the Unicode replacement character U+FFFD (<code>�</code>).</li> <li><code>ignore</code> — silently drop invalid bytes.</li> <li><code>keep</code> — copy invalid bytes to the output unchanged; the result is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8.</li> </ul> <details class=example> <summary>Example: serialize invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8 with different error handlers</summary> <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.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</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=p>;</span>
|
||||
@@ -140,4 +140,4 @@
|
||||
</code></pre></div> </details> <div class="admonition tip"> <p class=admonition-title>Avoiding invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8</p> <p>The best fix is to ensure that all strings are <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded before storing them. See the <a href=../../home/faq/#parse-errors-reading-non-ascii-characters><abbr title="Frequently Asked Questions">FAQ</abbr> on non-<abbr title="American Standard Code for Information Interchange">ASCII</abbr> characters</a> for how to convert wide or Latin-1 strings.</p> </div> <h2 id=numbers-nan-and-binary-values>Numbers, <abbr title="Not a Number">NaN</abbr>, and binary values<a class=headerlink href=#numbers-nan-and-binary-values title="Permanent link">¶</a></h2> <ul> <li><strong>Numbers</strong> are serialized with enough precision to round-trip; see <a href=../types/number_handling/#number-serialization>number serialization</a>.</li> <li><strong><abbr title="Not a Number">NaN</abbr> and infinity</strong> cannot be represented in <abbr title="JavaScript Object Notation">JSON</abbr> and are serialized as <code class=highlight><span class=kc>null</span></code>; see <a href=../types/number_handling/#nan-handling><abbr title="Not a Number">NaN</abbr> handling</a>. The <a href=../binary_formats/ >binary formats</a> can preserve them.</li> <li><strong>Binary values</strong> have no <abbr title="JavaScript Object Notation">JSON</abbr> representation and are serialized as a helper object for debugging only; see <a href=../binary_values/#serialization>binary values</a>.</li> </ul> <h2 id=using-stdformat-stdprint-and-fmt>Using <code>std::format</code>, <code>std::print</code>, and <code>fmt</code><a class=headerlink href=#using-stdformat-stdprint-and-fmt title="Permanent link">¶</a></h2> <p>Since version 3.12.0, <abbr title="JavaScript Object Notation">JSON</abbr> values can be formatted directly with C++20's <a href="https://en.cppreference.com/w/cpp/utility/format/format"><code>std::format</code></a> whenever the standard library provides the <code><format></code> header (controlled by <a href=../../api/macros/json_has_std_format/ ><code>JSON_HAS_STD_FORMAT</code></a>). This is enabled by the <a href=../../api/basic_json/std_formatter/ ><code>std::formatter<basic_json></code></a> specialization, which also makes <abbr title="JavaScript Object Notation">JSON</abbr> values work with <code>std::format_to</code> and with C++23's <code>std::print</code>/<code>std::println</code>:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>print</span><span class=p>(</span><span class=s>"{}"</span><span class=p>,</span><span class=w> </span><span class=n>j</span><span class=p>);</span><span class=w> </span><span class=c1>// compact, like j.dump()</span>
|
||||
<span class=n>std</span><span class=o>::</span><span class=n>print</span><span class=p>(</span><span class=s>"{:2}"</span><span class=p>,</span><span class=w> </span><span class=n>j</span><span class=p>);</span><span class=w> </span><span class=c1>// pretty-printed with indent 2 (like j.dump(2))</span>
|
||||
<span class=n>std</span><span class=o>::</span><span class=n>println</span><span class=p>(</span><span class=s>"{:#}"</span><span class=p>,</span><span class=w> </span><span class=n>j</span><span class=p>);</span><span class=w> </span><span class=c1>// pretty-printed with the default indent</span>
|
||||
</code></pre></div> <p>The format spec mirrors the <code>dump</code> parameters: <code class=highlight><span class=s>"{:#}"</span></code> pretty-prints, a width such as <code class=highlight><span class=s>"{:2}"</span></code> sets the indent, and a fill-and-align prefix such as <code class=highlight><span class=s>"{:.>#}"</span></code> sets the indent character.</p> <p>For the <a href="https://github.com/fmtlib/fmt">{fmt}</a> library, the library ships a <a href=../../api/basic_json/format_as/ ><code>format_as</code></a> helper. Note its behavior depends on the <code>fmt</code> version; see the <a href=../../home/faq/#using-json-values-with-stdformat-or-fmt><abbr title="Frequently Asked Questions">FAQ</abbr> entry</a> for the details and a recipe for a full <code>fmt::formatter</code> specialization.</p> <h2 id=serializing-to-other-formats>Serializing to other formats<a class=headerlink href=#serializing-to-other-formats title="Permanent link">¶</a></h2> <p>Besides <abbr title="JavaScript Object Notation">JSON</abbr> text, a value can also be serialized to the more compact <a href=../binary_formats/ >binary formats</a> (<abbr title="Binary JData">BJData</abbr>, <abbr title="Binary Object Notation 8">BON8</abbr>, <abbr title="Binary JSON">BSON</abbr>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, <abbr title="Universal Binary JSON">UBJSON</abbr>).</p> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../../api/basic_json/dump/ ><code>dump</code></a> - serialize to a <abbr title="JavaScript Object Notation">JSON</abbr>-formatted string</li> <li><a href=../../api/operator_ltlt/ ><code>operator<<</code></a> - serialize to a stream</li> <li><a href=../../api/basic_json/to_string/ ><code>to_string</code></a> - user-defined-conversion helper</li> <li><a href=../../api/basic_json/std_formatter/ ><code>std::formatter<basic_json></code></a> - use <abbr title="JavaScript Object Notation">JSON</abbr> values with <code>std::format</code> and <code>std::print</code></li> <li><a href=../../api/basic_json/format_as/ ><code>format_as</code></a> - use <abbr title="JavaScript Object Notation">JSON</abbr> values with the {fmt} library</li> <li><a href=../parsing/ >Parsing</a> - the reverse operation</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 2, 2026 09:32:15 UTC">October 2, 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.Line truncated
|
||||
</code></pre></div> <p>The format spec mirrors the <code>dump</code> parameters: <code class=highlight><span class=s>"{:#}"</span></code> pretty-prints, a width such as <code class=highlight><span class=s>"{:2}"</span></code> sets the indent, and a fill-and-align prefix such as <code class=highlight><span class=s>"{:.>#}"</span></code> sets the indent character.</p> <p>For the <a href="https://github.com/fmtlib/fmt">{fmt}</a> library, the library ships a <a href=../../api/basic_json/format_as/ ><code>format_as</code></a> helper. Note its behavior depends on the <code>fmt</code> version; see the <a href=../../home/faq/#using-json-values-with-stdformat-or-fmt><abbr title="Frequently Asked Questions">FAQ</abbr> entry</a> for the details and a recipe for a full <code>fmt::formatter</code> specialization.</p> <h2 id=serializing-to-other-formats>Serializing to other formats<a class=headerlink href=#serializing-to-other-formats title="Permanent link">¶</a></h2> <p>Besides <abbr title="JavaScript Object Notation">JSON</abbr> text, a value can also be serialized to the more compact <a href=../binary_formats/ >binary formats</a> (<abbr title="Binary JData">BJData</abbr>, <abbr title="Binary Object Notation 8">BON8</abbr>, <abbr title="Binary JSON">BSON</abbr>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, <abbr title="Universal Binary JSON">UBJSON</abbr>).</p> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../../api/basic_json/dump/ ><code>dump</code></a> - serialize to a <abbr title="JavaScript Object Notation">JSON</abbr>-formatted string</li> <li><a href=../../api/operator_ltlt/ ><code>operator<<</code></a> - serialize to a stream</li> <li><a href=../../api/basic_json/to_string/ ><code>to_string</code></a> - user-defined-conversion helper</li> <li><a href=../../api/basic_json/std_formatter/ ><code>std::formatter<basic_json></code></a> - use <abbr title="JavaScript Object Notation">JSON</abbr> values with <code>std::format</code> and <code>std::print</code></li> <li><a href=../../api/basic_json/format_as/ ><code>format_as</code></a> - use <abbr title="JavaScript Object Notation">JSON</abbr> values with the {fmt} library</li> <li><a href=../parsing/ >Parsing</a> - the reverse operation</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 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 © 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.Line truncated
|
||||
@@ -154,6 +154,7 @@ If a string contains invalid UTF-8 sequences (for example, because it holds data
|
||||
- `strict` (default) — throw a [`type_error.316`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) exception.
|
||||
- `replace` — replace invalid bytes with the Unicode replacement character U+FFFD (`�`).
|
||||
- `ignore` — silently drop invalid bytes.
|
||||
- `keep` — copy invalid bytes to the output unchanged; the result is not valid UTF-8.
|
||||
|
||||
Example: serialize invalid UTF-8 with different error handlers
|
||||
|
||||
|
||||
Reference in new issue
Block a user