mirror of
https://github.com/nlohmann/json.git
synced 2026-10-06 22:47:13 +00:00
deploy: 36187cacfb
This commit is contained in:
1 parent
e5ef5a4cd3
commit
57c8031f9d
255 files changed
+506
-506
No files matched your search
@@ -138,4 +138,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 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="August 19, 2026 18:19:47 UTC">August 19, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-5Line 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 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="August 20, 2026 10:32:34 UTC">August 20, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-5Line truncated
|
||||
Reference in new issue
Block a user