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

+4 -3
View File
@@ -267,9 +267,10 @@ unsigned 8-bit integer. If no subtype is given, the generic binary subtype 0x00
### CBOR
[CBOR](binary_formats/cbor.md) supports binary values, but no subtypes. Subtypes will be serialized as tags. Any binary
value will be serialized as byte strings. The library will choose the smallest representation using the length of the
byte array.
[CBOR](binary_formats/cbor.md) supports binary values and subtypes. Any binary value will be serialized as a byte
string; a subtype is serialized as a tag preceding it. When parsing, tags are only read back as subtypes with
`cbor_tag_handler_t::store` (see [`from_cbor`](../api/basic_json/from_cbor.md)). The library will choose the smallest
representation using the length of the byte array.
??? example "Example: serialize a binary value to CBOR"
+2 -2
View File
@@ -104,7 +104,7 @@ vector &lt;|-- binary_t</code></pre> <p>By default, binary values are stored as
<span class=w> </span><span class=nt>"subtype"</span><span class=p>:</span><span class=w> </span><span class=mi>42</span>
<span class=w> </span><span class=p>}</span>
<span class=p>}</span>
</code></pre></div> </details> <h3 id=cbor><abbr title="Concise Binary Object Representation">CBOR</abbr><a class=headerlink href=#cbor title="Permanent link">&para;</a></h3> <p><a href=../binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a> supports binary values, but no subtypes. Subtypes will be serialized as tags. Any binary value will be serialized as byte strings. The library will choose the smallest representation using the length of the byte array.</p> <details class=example> <summary>Example: serialize a binary value to <abbr title="Concise Binary Object Representation">CBOR</abbr></summary> <p>Code:</p> <div class=highlight><pre><span></span><code><span class=c1>// create a binary value of subtype 42</span>
</code></pre></div> </details> <h3 id=cbor><abbr title="Concise Binary Object Representation">CBOR</abbr><a class=headerlink href=#cbor title="Permanent link">&para;</a></h3> <p><a href=../binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a> supports binary values and subtypes. Any binary value will be serialized as a byte string; a subtype is serialized as a tag preceding it. When parsing, tags are only read back as subtypes with <code>cbor_tag_handler_t::store</code> (see <a href=../../api/basic_json/from_cbor/ ><code>from_cbor</code></a>). The library will choose the smallest representation using the length of the byte array.</p> <details class=example> <summary>Example: serialize a binary value to <abbr title="Concise Binary Object Representation">CBOR</abbr></summary> <p>Code:</p> <div class=highlight><pre><span></span><code><span class=c1>// create a binary value of subtype 42</span>
<span class=n>json</span><span class=w> </span><span class=n>j</span><span class=p>;</span>
<span class=n>j</span><span class=p>[</span><span class=s>"binary"</span><span class=p>]</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json</span><span class=o>::</span><span class=n>binary</span><span class=p>({</span><span class=mh>0xCA</span><span class=p>,</span><span class=w> </span><span class=mh>0xFE</span><span class=p>,</span><span class=w> </span><span class=mh>0xBA</span><span class=p>,</span><span class=w> </span><span class=mh>0xBE</span><span class=p>},</span><span class=w> </span><span class=mi>42</span><span class=p>);</span>
@@ -167,4 +167,4 @@ vector &lt;|-- binary_t</code></pre> <p>By default, binary values are stored as
</code></pre></div> <p>Note that subtype (42) is <strong>not</strong> serialized and that <abbr title="Universal Binary JSON">UBJSON</abbr> has <strong>no binary type</strong>, and deserializing <code>v</code> would yield the following value:</p> <div class=highlight><pre><span></span><code><span class=p>{</span>
<span class=w> </span><span class=nt>"binary"</span><span class=p>:</span><span class=w> </span><span class=p>[</span><span class=mi>202</span><span class=p>,</span><span class=w> </span><span class=mi>254</span><span class=p>,</span><span class=w> </span><span class=mi>186</span><span class=p>,</span><span class=w> </span><span class=mi>190</span><span class=p>]</span>
<span class=p>}</span>
</code></pre></div> </details> <!-- 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 &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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 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="M162.7 210c-1.8 3.3-25.2 44.4-70.1 123.5-4.9 8.3-10.8 12.5-17.7 12.5H9.8c-7.7 0-12.1-7.5-8.5-14.4l69-121.3q.3 0 0-.3l-43.9-75.6c-4.3-7.8.3-14.1 8.5-14.1H100c7.3 0 13.3 4.1 18 12.2zM382.6 46.1l-144 253v.3L330.2 466c3.9 7.1.2 14.1-8.5 14.1h-65.2c-7.6 0-13.6-4-18-12.2l-92.4-168.5c3.3-5.8 51.5-90.8 144.Line truncated
</code></pre></div> </details> <!-- 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 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="M162.7 210c-1.8 3.3-25.2 44.4-70.1 123.5-4.9 8.3-10.8 12.5-17.7 12.5H9.8c-7.7 0-12.1-7.5-8.5-14.4l69-121.3q.3 0 0-.3l-43.9-75.6c-4.3-7.8.3-14.1 8.5-14.1H100c7.3 0 13.3 4.1 18 12.2zM382.6 46.1l-144 253v.3L330.2 466c3.9 7.1.2 14.1-8.5 14.1h-65.2c-7.6 0-13.6-4-18-12.2l-92.4-168.5c3.3-5.8 51.5-90.8 14Line truncated
+1 -1
View File
@@ -250,7 +250,7 @@ Note that the serialization preserves the subtype, and deserializing `v` would y
### CBOR
[CBOR](https://json.nlohmann.me/features/binary_formats/cbor/index.md) supports binary values, but no subtypes. Subtypes will be serialized as tags. Any binary value will be serialized as byte strings. The library will choose the smallest representation using the length of the byte array.
[CBOR](https://json.nlohmann.me/features/binary_formats/cbor/index.md) supports binary values and subtypes. Any binary value will be serialized as a byte string; a subtype is serialized as a tag preceding it. When parsing, tags are only read back as subtypes with `cbor_tag_handler_t::store` (see [`from_cbor`](https://json.nlohmann.me/api/basic_json/from_cbor/index.md)). The library will choose the smallest representation using the length of the byte array.
Example: serialize a binary value to CBOR
+3 -3
View File
@@ -243,9 +243,9 @@ See [full documentation of `JSON_USE_GLOBAL_UDLS`](../api/macros/json_use_global
## `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`
When defined to `1`, the library restores the legacy behavior in which a discarded value compared equal to itself. This
behavior is [deprecated](../integration/migration_guide.md#miscellaneous-functions) and switched off (`0`) by
default.
When defined to `1`, the library restores the legacy behavior in which comparing a discarded value with `<=` or `>=`
yields `#!cpp true` instead of `#!cpp false`. This behavior is
[deprecated](../integration/migration_guide.md#miscellaneous-functions) and switched off (`0`) by default.
See [full documentation of `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`](../api/macros/json_use_legacy_discarded_value_comparison.md).
+1 -1
View File
@@ -1 +1 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/macros/" rel="canonical"><link href=../enum_conversion/ rel=prev><link href=../trailing_commas/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Supported Macros - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#supported-macros class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Supported Macros </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </labelLine truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/macros/" rel="canonical"><link href=../enum_conversion/ rel=prev><link href=../trailing_commas/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Supported Macros - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#supported-macros class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Supported Macros </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </labelLine truncated
+1 -1
View File
@@ -184,7 +184,7 @@ See [full documentation of `JSON_USE_GLOBAL_UDLS`](https://json.nlohmann.me/api/
## `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`
When defined to `1`, the library restores the legacy behavior in which a discarded value compared equal to itself. This behavior is [deprecated](https://json.nlohmann.me/integration/migration_guide/#miscellaneous-functions) and switched off (`0`) by default.
When defined to `1`, the library restores the legacy behavior in which comparing a discarded value with `<=` or `>=` yields `true` instead of `false`. This behavior is [deprecated](https://json.nlohmann.me/integration/migration_guide/#miscellaneous-functions) and switched off (`0`) by default.
See [full documentation of `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`](https://json.nlohmann.me/api/macros/json_use_legacy_discarded_value_comparison/index.md).
+1 -1
View File
@@ -1,6 +1,6 @@
# JSON Merge Patch
The library supports JSON Merge Patch ([RFC 7386](https://tools.ietf.org/html/rfc7386)) as a patch format.
The library supports JSON Merge Patch ([RFC 7396](https://tools.ietf.org/html/rfc7396)) as a patch format.
The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of
modifications to a target resource's content. This function applies a merge patch to the current JSON value.
+2 -2
View File
@@ -1,4 +1,4 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/merge_patch/" rel="canonical"><link href=../iterators/ rel=prev><link href=../json_patch/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>JSON Merge Patch - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#json-merge-patch class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> JSON Merge Patch </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <divLine truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/merge_patch/" rel="canonical"><link href=../iterators/ rel=prev><link href=../json_patch/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>JSON Merge Patch - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#json-merge-patch class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> JSON Merge Patch </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <divLine truncated
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iomanip&gt;</span><span class=w> </span><span class=c1>// for std::setw</span>
@@ -50,4 +50,4 @@
<span class=w> </span><span class=p>],</span>
<span class=w> </span><span class=nt>"title"</span><span class=p>:</span><span class=w> </span><span class=s2>"Hello!"</span>
<span class=p>}</span>
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch and Diff</a> - a more expressive alternative that describes a sequence of operations</li> <li><a href=../json_pointer/ ><abbr title="JavaScript Object Notation">JSON</abbr> Pointer</a> - the addressing scheme used by <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> <li>Function <a href=../../api/basic_json/merge_patch/ ><code>merge_patch</code></a></li> <li>Function <a href=../../api/basic_json/merge_diff/ ><code>merge_diff</code></a></li> <li>Function <a href=../../api/basic_json/update/ ><code>update</code></a> - merge objects, optionally recursively</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 12, 2026 07:33:43 UTC">October 12, 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9Line truncated
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch and Diff</a> - a more expressive alternative that describes a sequence of operations</li> <li><a href=../json_pointer/ ><abbr title="JavaScript Object Notation">JSON</abbr> Pointer</a> - the addressing scheme used by <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> <li>Function <a href=../../api/basic_json/merge_patch/ ><code>merge_patch</code></a></li> <li>Function <a href=../../api/basic_json/merge_diff/ ><code>merge_diff</code></a></li> <li>Function <a href=../../api/basic_json/update/ ><code>update</code></a> - merge objects, optionally recursively</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 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9Line truncated
+1 -1
View File
@@ -1,6 +1,6 @@
# JSON Merge Patch
The library supports JSON Merge Patch ([RFC 7386](https://tools.ietf.org/html/rfc7386)) as a patch format. The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content. This function applies a merge patch to the current JSON value.
The library supports JSON Merge Patch ([RFC 7396](https://tools.ietf.org/html/rfc7396)) as a patch format. The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content. This function applies a merge patch to the current JSON value.
Instead of using [JSON Pointer](https://json.nlohmann.me/features/json_pointer/index.md) to specify values to be manipulated, it describes the changes using a syntax that closely mimics the document being modified. Unlike [JSON Patch](https://json.nlohmann.me/features/json_patch/index.md), a JSON Merge Patch cannot express every kind of change (e.g., it cannot reorder array elements or remove a specific array element), but it is easier to read and write for object-shaped documents.
+2 -2
View File
@@ -107,7 +107,7 @@ config.update(user_settings, true);
// {"log":{"file":"app.log","level":"debug"},"retries":3}
```
[JSON Merge Patch](merge_patch.md) ([RFC 7386](https://tools.ietf.org/html/rfc7386)) also merges objects recursively,
[JSON Merge Patch](merge_patch.md) ([RFC 7396](https://tools.ietf.org/html/rfc7396)) also merges objects recursively,
but it is a different tool: a `#!json null` in the patch means "remove this key". It is meant for applying merge patch
documents (e.g., received via HTTP PATCH). To merge configuration-like objects, use `#!cpp update(..., true)`. To apply
a sequence of well-defined edit operations, see [JSON Patch](json_patch.md).
@@ -131,6 +131,6 @@ a.erase(1); // [1,3,4] (erase by index)
- [`push_back`](../api/basic_json/push_back.md) / [`emplace_back`](../api/basic_json/emplace_back.md) - append to an array
- [`emplace`](../api/basic_json/emplace.md) - insert into an object if the key is absent
- [`update`](../api/basic_json/update.md) - merge objects (shallow, or recursive with `merge_objects`)
- [`merge_patch`](../api/basic_json/merge_patch.md) - apply an RFC 7386 merge patch
- [`merge_patch`](../api/basic_json/merge_patch.md) - apply an RFC 7396 merge patch
- [`erase`](../api/basic_json/erase.md) / [`clear`](../api/basic_json/clear.md) - remove elements
- [JSON Patch and Diff](json_patch.md) and [JSON Merge Patch](merge_patch.md) - structured modifications
+2 -2
View File
@@ -55,9 +55,9 @@
<span class=n>json</span><span class=w> </span><span class=n>config</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>defaults</span><span class=p>;</span>
<span class=n>config</span><span class=p>.</span><span class=n>update</span><span class=p>(</span><span class=n>user_settings</span><span class=p>,</span><span class=w> </span><span class=nb>true</span><span class=p>);</span>
<span class=c1>// {"log":{"file":"app.log","level":"debug"},"retries":3}</span>
</code></pre></div> <p><a href=../merge_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</a> (<a href="https://tools.ietf.org/html/rfc7386"><abbr title="Request for Comments">RFC</abbr> 7386</a>) also merges objects recursively, but it is a different tool: a <code class=highlight><span class=kc>null</span></code> in the patch means "remove this key". It is meant for applying merge patch documents (e.g., received via <abbr title="Hypertext Transfer Protocol">HTTP</abbr> PATCH). To merge configuration-like objects, use <code class=highlight><span class=n>update</span><span class=p>(...,</span><span class=w> </span><span class=nb>true</span><span class=p>)</span></code>. To apply a sequence of well-defined edit operations, see <a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch</a>.</p> <h2 id=removing-elements>Removing elements<a class=headerlink href=#removing-elements title="Permanent link">&para;</a></h2> <p>Elements are removed with <a href=../../api/basic_json/erase/ ><code>erase</code></a>, which accepts an object key, an array index, or an iterator. <a href=../../api/basic_json/clear/ ><code>clear</code></a> empties a value while keeping its type, and <a href=../element_access/unchecked_access/ ><code>operator[]</code></a> combined with assignment can overwrite a value entirely.</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=s>"a"</span><span class=p>,</span><span class=w> </span><span class=mi>1</span><span class=p>},</span><span class=w> </span><span class=p>{</span><span class=s>"b"</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>},</span><span class=w> </span><span class=p>{</span><span class=s>"c"</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=p>}};</span>
</code></pre></div> <p><a href=../merge_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</a> (<a href="https://tools.ietf.org/html/rfc7396"><abbr title="Request for Comments">RFC</abbr> 7396</a>) also merges objects recursively, but it is a different tool: a <code class=highlight><span class=kc>null</span></code> in the patch means "remove this key". It is meant for applying merge patch documents (e.g., received via <abbr title="Hypertext Transfer Protocol">HTTP</abbr> PATCH). To merge configuration-like objects, use <code class=highlight><span class=n>update</span><span class=p>(...,</span><span class=w> </span><span class=nb>true</span><span class=p>)</span></code>. To apply a sequence of well-defined edit operations, see <a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch</a>.</p> <h2 id=removing-elements>Removing elements<a class=headerlink href=#removing-elements title="Permanent link">&para;</a></h2> <p>Elements are removed with <a href=../../api/basic_json/erase/ ><code>erase</code></a>, which accepts an object key, an array index, or an iterator. <a href=../../api/basic_json/clear/ ><code>clear</code></a> empties a value while keeping its type, and <a href=../element_access/unchecked_access/ ><code>operator[]</code></a> combined with assignment can overwrite a value entirely.</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=s>"a"</span><span class=p>,</span><span class=w> </span><span class=mi>1</span><span class=p>},</span><span class=w> </span><span class=p>{</span><span class=s>"b"</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>},</span><span class=w> </span><span class=p>{</span><span class=s>"c"</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=p>}};</span>
<span class=n>j</span><span class=p>.</span><span class=n>erase</span><span class=p>(</span><span class=s>"b"</span><span class=p>);</span><span class=w> </span><span class=c1>// {"a":1,"c":3}</span>
<span class=n>json</span><span class=w> </span><span class=n>a</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=n>a</span><span class=p>.</span><span class=n>erase</span><span class=p>(</span><span class=mi>1</span><span class=p>);</span><span class=w> </span><span class=c1>// [1,3,4] (erase by index)</span>
</code></pre></div> <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/push_back/ ><code>push_back</code></a> / <a href=../../api/basic_json/emplace_back/ ><code>emplace_back</code></a> - append to an array</li> <li><a href=../../api/basic_json/emplace/ ><code>emplace</code></a> - insert into an object if the key is absent</li> <li><a href=../../api/basic_json/update/ ><code>update</code></a> - merge objects (shallow, or recursive with <code>merge_objects</code>)</li> <li><a href=../../api/basic_json/merge_patch/ ><code>merge_patch</code></a> - apply an <abbr title="Request for Comments">RFC</abbr> 7386 merge patch</li> <li><a href=../../api/basic_json/erase/ ><code>erase</code></a> / <a href=../../api/basic_json/clear/ ><code>clear</code></a> - remove elements</li> <li><a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch and Diff</a> and <a href=../merge_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</a> - structured modifications</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 11, 2026 06:22:10 UTC">October 11, 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C1Line truncated
</code></pre></div> <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/push_back/ ><code>push_back</code></a> / <a href=../../api/basic_json/emplace_back/ ><code>emplace_back</code></a> - append to an array</li> <li><a href=../../api/basic_json/emplace/ ><code>emplace</code></a> - insert into an object if the key is absent</li> <li><a href=../../api/basic_json/update/ ><code>update</code></a> - merge objects (shallow, or recursive with <code>merge_objects</code>)</li> <li><a href=../../api/basic_json/merge_patch/ ><code>merge_patch</code></a> - apply an <abbr title="Request for Comments">RFC</abbr> 7396 merge patch</li> <li><a href=../../api/basic_json/erase/ ><code>erase</code></a> / <a href=../../api/basic_json/clear/ ><code>clear</code></a> - remove elements</li> <li><a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch and Diff</a> and <a href=../merge_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</a> - structured modifications</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 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C1Line truncated
+2 -2
View File
@@ -97,7 +97,7 @@ config.update(user_settings, true);
// {"log":{"file":"app.log","level":"debug"},"retries":3}
```
[JSON Merge Patch](https://json.nlohmann.me/features/merge_patch/index.md) ([RFC 7386](https://tools.ietf.org/html/rfc7386)) also merges objects recursively, but it is a different tool: a `null` in the patch means "remove this key". It is meant for applying merge patch documents (e.g., received via HTTP PATCH). To merge configuration-like objects, use `update(..., true)`. To apply a sequence of well-defined edit operations, see [JSON Patch](https://json.nlohmann.me/features/json_patch/index.md).
[JSON Merge Patch](https://json.nlohmann.me/features/merge_patch/index.md) ([RFC 7396](https://tools.ietf.org/html/rfc7396)) also merges objects recursively, but it is a different tool: a `null` in the patch means "remove this key". It is meant for applying merge patch documents (e.g., received via HTTP PATCH). To merge configuration-like objects, use `update(..., true)`. To apply a sequence of well-defined edit operations, see [JSON Patch](https://json.nlohmann.me/features/json_patch/index.md).
## Removing elements
@@ -116,6 +116,6 @@ a.erase(1); // [1,3,4] (erase by index)
- [`push_back`](https://json.nlohmann.me/api/basic_json/push_back/index.md) / [`emplace_back`](https://json.nlohmann.me/api/basic_json/emplace_back/index.md) - append to an array
- [`emplace`](https://json.nlohmann.me/api/basic_json/emplace/index.md) - insert into an object if the key is absent
- [`update`](https://json.nlohmann.me/api/basic_json/update/index.md) - merge objects (shallow, or recursive with `merge_objects`)
- [`merge_patch`](https://json.nlohmann.me/api/basic_json/merge_patch/index.md) - apply an RFC 7386 merge patch
- [`merge_patch`](https://json.nlohmann.me/api/basic_json/merge_patch/index.md) - apply an RFC 7396 merge patch
- [`erase`](https://json.nlohmann.me/api/basic_json/erase/index.md) / [`clear`](https://json.nlohmann.me/api/basic_json/clear/index.md) - remove elements
- [JSON Patch and Diff](https://json.nlohmann.me/features/json_patch/index.md) and [JSON Merge Patch](https://json.nlohmann.me/features/merge_patch/index.md) - structured modifications
+4 -10
View File
@@ -50,19 +50,13 @@ stack, however deeply it is nested.
Once a value has been parsed, operations that walk it recursively -- serializing it with
[`dump`](../../api/basic_json/dump.md), hashing it, copying it, comparing two values with `#!cpp ==`, `#!cpp <`, or (in
C++20) `#!cpp <=>`, merging with [`update`](../../api/basic_json/update.md), and applying a
[`merge_patch`](../../api/basic_json/merge_patch.md) -- descend at most 128 levels on the call stack and continue
C++20) `#!cpp <=>`, merging with [`update`](../../api/basic_json/update.md), applying a
[`merge_patch`](../../api/basic_json/merge_patch.md), creating a patch with [`diff`](../../api/basic_json/diff.md) or
[`merge_diff`](../../api/basic_json/merge_diff.md), [`flatten`](../../api/basic_json/flatten.md), and the binary
writers (`to_cbor`, `to_msgpack`, ...) -- descend at most 128 levels on the call stack and continue
below that with an explicit stack instead, so none of them can exhaust the stack either, however deeply the value is
nested. Destroying a value (its destructor) never recurses at all, regardless of nesting depth, for the same reason.
!!! note "Not every operation is bounded yet"
[`diff`](../../api/basic_json/diff.md), [`flatten`](../../api/basic_json/flatten.md), and the binary writers
(`to_cbor`, `to_msgpack`, ...) still recurse once per nesting level; this is called out as work in progress in the
[assurance case](../../community/assurance_case.md#secure-design). A value deep enough to matter for these
operations would typically first have to survive parsing without hitting a self-imposed depth limit, as described
above.
## Input size
The library does not limit the overall size of a JSON text; a value nested or wide enough will use memory
+1 -1
View File
@@ -1 +1 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/parsing/untrusted_input/" rel="canonical"><link href=../sax_interface/ rel=prev><link href=../../assertions/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Parsing Untrusted Input - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#parsing-untrusted-input class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Parsing Untrusted Input </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9Line truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/parsing/untrusted_input/" rel="canonical"><link href=../sax_interface/ rel=prev><link href=../../assertions/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Parsing Untrusted Input - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#parsing-untrusted-input class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Parsing Untrusted Input </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9Line truncated
+1 -5
View File
@@ -22,11 +22,7 @@ No built-in depth limit while parsing
Neither the parser nor the binary readers impose a limit on how deep the input may nest. An attacker can still exhaust memory (though not the call stack) with a sufficiently deep document. If you need to reject over-deep untrusted input outright, track the depth yourself, either with a [parser callback](https://json.nlohmann.me/features/parsing/parser_callbacks/#recipe-max-nesting-depth-via-a-callback) for the JSON parser, or by counting `start_object`/`start_array` and `end_object`/`end_array` calls in a [SAX handler](https://json.nlohmann.me/features/parsing/sax_interface/index.md) (for the JSON parser or a binary format alike) and throwing once your limit is exceeded.
Once a value has been parsed, operations that walk it recursively -- serializing it with [`dump`](https://json.nlohmann.me/api/basic_json/dump/index.md), hashing it, copying it, comparing two values with `==`, `<`, or (in C++20) `<=>`, merging with [`update`](https://json.nlohmann.me/api/basic_json/update/index.md), and applying a [`merge_patch`](https://json.nlohmann.me/api/basic_json/merge_patch/index.md) -- descend at most 128 levels on the call stack and continue below that with an explicit stack instead, so none of them can exhaust the stack either, however deeply the value is nested. Destroying a value (its destructor) never recurses at all, regardless of nesting depth, for the same reason.
Not every operation is bounded yet
[`diff`](https://json.nlohmann.me/api/basic_json/diff/index.md), [`flatten`](https://json.nlohmann.me/api/basic_json/flatten/index.md), and the binary writers (`to_cbor`, `to_msgpack`, ...) still recurse once per nesting level; this is called out as work in progress in the [assurance case](https://json.nlohmann.me/community/assurance_case/#secure-design). A value deep enough to matter for these operations would typically first have to survive parsing without hitting a self-imposed depth limit, as described above.
Once a value has been parsed, operations that walk it recursively -- serializing it with [`dump`](https://json.nlohmann.me/api/basic_json/dump/index.md), hashing it, copying it, comparing two values with `==`, `<`, or (in C++20) `<=>`, merging with [`update`](https://json.nlohmann.me/api/basic_json/update/index.md), applying a [`merge_patch`](https://json.nlohmann.me/api/basic_json/merge_patch/index.md), creating a patch with [`diff`](https://json.nlohmann.me/api/basic_json/diff/index.md) or [`merge_diff`](https://json.nlohmann.me/api/basic_json/merge_diff/index.md), [`flatten`](https://json.nlohmann.me/api/basic_json/flatten/index.md), and the binary writers (`to_cbor`, `to_msgpack`, ...) -- descend at most 128 levels on the call stack and continue below that with an explicit stack instead, so none of them can exhaust the stack either, however deeply the value is nested. Destroying a value (its destructor) never recurses at all, regardless of nesting depth, for the same reason.
## Input size
+1 -1
View File
@@ -155,7 +155,7 @@ with `\uXXXX` sequences, so that the output contains only ASCII characters:
```cpp
json j = "苹果";
j.dump(); // "苹果"
j.dump(-1, ' ', true); // "苹果"
j.dump(-1, ' ', true); // "\u82f9\u679c"
```
## Handling invalid UTF-8
+2 -2
View File
@@ -107,7 +107,7 @@
<span class=err>s</span><span class=kc>tr</span><span class=err>i</span><span class=kc>n</span><span class=err>g</span><span class=w> </span><span class=err>wi</span><span class=kc>t</span><span class=err>h</span><span class=w> </span><span class=err>ig</span><span class=kc>n</span><span class=err>ored</span><span class=w> </span><span class=err>i</span><span class=kc>n</span><span class=err>valid</span><span class=w> </span><span class=err>charac</span><span class=kc>ters</span><span class=p>:</span><span class=w> </span><span class=s2>"äü"</span>
</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">&para;</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>
<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>// "\u82f9\u679c"</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">&para;</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>&lt;iostream&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</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">&para;</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">&para;</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>&lt;format&gt;</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&lt;basic_json&gt;</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>"{:.&gt;#}"</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">&para;</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">&para;</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&lt;&lt;</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&lt;basic_json&gt;</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 &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.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>"{:.&gt;#}"</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">&para;</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">&para;</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&lt;&lt;</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&lt;basic_json&gt;</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 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 2Line truncated
+1 -1
View File
@@ -144,7 +144,7 @@ Strings are stored and serialized as UTF-8 (see [types](https://json.nlohmann.me
```
json j = "苹果";
j.dump(); // "苹果"
j.dump(-1, ' ', true); // "苹果"
j.dump(-1, ' ', true); // "\u82f9\u679c"
```
## Handling invalid UTF-8
+8 -8
View File
@@ -120,7 +120,7 @@ requirements are collected in [Template Parameter Requirements](template_paramet
### Default type
With the default values for *ObjectType* (`std::map`), *StringType* (`std::string`), and *AllocatorType* (`std::allocator`), the default value for `object_t` is:
With the default values for *ObjectType* (`std::map`), *StringType* (`std::string`), and *AllocatorType* (`std::allocator`), the default type for `object_t` is:
```cpp
std::map<
@@ -177,7 +177,7 @@ json j = m;
### Default type
With the default values for *ArrayType* (`std::vector`) and *AllocatorType* (`std::allocator`), the default value for `array_t` is:
With the default values for *ArrayType* (`std::vector`) and *AllocatorType* (`std::allocator`), the default type for `array_t` is:
```cpp
std::vector<
@@ -209,7 +209,7 @@ Unicode values are split by the JSON class into byte-sized characters during des
### Default type
With the default values for *StringType* (`std::string`), the default value for `string_t` is `#!cpp std::string`.
With the default values for *StringType* (`std::string`), the default type for `string_t` is `#!cpp std::string`.
### Encoding
@@ -234,7 +234,7 @@ String values are stored as pointers in a `basic_json` type. That is, for any ac
### Default type
With the default values for *BooleanType* (`#!cpp bool`), the default value for `boolean_t` is `#!cpp bool`.
With the default values for *BooleanType* (`#!cpp bool`), the default type for `boolean_t` is `#!cpp bool`.
### Storage
@@ -252,9 +252,9 @@ This description includes both integer and floating-point numbers. However, C++
### Default types
With the default values for *NumberIntegerType* (`std::int64_t`), the default value for `number_integer_t` is `std::int64_t`.
With the default values for *NumberUnsignedType* (`std::uint64_t`), the default value for `number_unsigned_t` is `std::uint64_t`.
With the default values for *NumberFloatType* (`#!cpp double`), the default value for `number_float_t` is `#!cpp double`.
With the default values for *NumberIntegerType* (`std::int64_t`), the default type for `number_integer_t` is `std::int64_t`.
With the default values for *NumberUnsignedType* (`std::uint64_t`), the default type for `number_unsigned_t` is `std::uint64_t`.
With the default values for *NumberFloatType* (`#!cpp double`), the default type for `number_float_t` is `#!cpp double`.
### Default behavior
@@ -281,7 +281,7 @@ As this range is a subrange of the exactly supported range [`INT64_MIN`, `INT64_
> This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision.
This implementation does exactly follow this approach, as it uses double precision floating-point numbers. Note values smaller than `#!c -1.79769313486232e+308` and values greater than `#!c 1.79769313486232e+308` will be stored as NaN internally and be serialized to `#!json null`.
This implementation does exactly follow this approach, as it uses double precision floating-point numbers by default (see *NumberFloatType* above). Note that with the default `#!cpp double`, numbers smaller than `#!c -1.79769313486232e+308` or greater than `#!c 1.79769313486232e+308` are rejected during parsing with [`out_of_range.406`](../../home/exceptions.md#jsonexceptionout_of_range406). Non-finite values (NaN and infinity) can only be stored by assigning them directly and are serialized to `#!json null`.
### Storage
+3 -3
View File
@@ -68,7 +68,7 @@ basic_json .. value_t</code></pre> <h2 id=template-arguments>Template arguments<
<span class=k>using</span><span class=w> </span><span class=n>number_float_t</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>NumberFloatType</span><span class=p>;</span>
<span class=k>using</span><span class=w> </span><span class=n>binary_t</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>byte_container_with_subtype</span><span class=o>&lt;</span><span class=n>BinaryType</span><span class=o>&gt;</span><span class=p>;</span>
</code></pre></div> <p>Not every type can be passed for these template arguments: the library uses the resulting types in ways that imply a number of requirements, for instance that <code>StringType</code> is <code>char</code>-based or that <code>ArrayType</code> is vector-like. These requirements are collected in <a href=template_parameters/ >Template Parameter Requirements</a>.</p> <h2 id=objects>Objects<a class=headerlink href=#objects title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> objects as follows:</p> <blockquote> <p>An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.</p> </blockquote> <h3 id=default-type>Default type<a class=headerlink href=#default-type title="Permanent link">&para;</a></h3> <p>With the default values for <em>ObjectType</em> (<code>std::map</code>), <em>StringType</em> (<code>std::string</code>), and <em>AllocatorType</em> (<code>std::allocator</code>), the default value for <code>object_t</code> is:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>map</span><span class=o>&lt;</span>
</code></pre></div> <p>Not every type can be passed for these template arguments: the library uses the resulting types in ways that imply a number of requirements, for instance that <code>StringType</code> is <code>char</code>-based or that <code>ArrayType</code> is vector-like. These requirements are collected in <a href=template_parameters/ >Template Parameter Requirements</a>.</p> <h2 id=objects>Objects<a class=headerlink href=#objects title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> objects as follows:</p> <blockquote> <p>An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.</p> </blockquote> <h3 id=default-type>Default type<a class=headerlink href=#default-type title="Permanent link">&para;</a></h3> <p>With the default values for <em>ObjectType</em> (<code>std::map</code>), <em>StringType</em> (<code>std::string</code>), and <em>AllocatorType</em> (<code>std::allocator</code>), the default type for <code>object_t</code> is:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>map</span><span class=o>&lt;</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=p>,</span><span class=w> </span><span class=c1>// key_type</span>
<span class=w> </span><span class=n>basic_json</span><span class=p>,</span><span class=w> </span><span class=c1>// value_type</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>less</span><span class=o>&lt;&gt;</span><span class=p>,</span><span class=w> </span><span class=c1>// key_compare</span>
@@ -77,8 +77,8 @@ basic_json .. value_t</code></pre> <h2 id=template-arguments>Template arguments<
</code></pre></div> <h3 id=behavior>Behavior<a class=headerlink href=#behavior title="Permanent link">&para;</a></h3> <p>The choice of <code>object_t</code> influences the behavior of the <abbr title="JavaScript Object Notation">JSON</abbr> class. With the default type, objects have the following behavior:</p> <ul> <li>When all names are unique, objects will be interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings.</li> <li>When the names within an object are not unique, it is unspecified which one of the values for a given key will be chosen. For instance, <code class=highlight><span class=p>{</span><span class=nt>"key"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=nt>"key"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>}</span></code> could be equal to either <code class=highlight><span class=p>{</span><span class=nt>"key"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>}</span></code> or <code class=highlight><span class=p>{</span><span class=nt>"key"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>}</span></code>. To reject duplicate keys instead of silently resolving them one way or another, see <a href=../parsing/parser_callbacks/#recipe-rejecting-duplicate-object-keys>this parsing recipe</a>.</li> <li>Internally, name/value pairs are stored in lexicographical order of the names. Objects will also be serialized (see <code>dump</code>) in this order. For instance, both <code class=highlight><span class=p>{</span><span class=nt>"b"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=nt>"a"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>}</span></code> and <code class=highlight><span class=p>{</span><span class=nt>"a"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=nt>"b"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>}</span></code> will be stored and serialized as <code class=highlight><span class=p>{</span><span class=nt>"a"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=nt>"b"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>}</span></code>.</li> <li>When comparing objects, the order of the name/value pairs is irrelevant. This makes objects interoperable in the sense that they will not be affected by these differences. For instance, <code class=highlight><span class=p>{</span><span class=nt>"b"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=nt>"a"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>}</span></code> and <code class=highlight><span class=p>{</span><span class=nt>"a"</span><span class=p>:</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=nt>"b"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>}</span></code> will be treated as equal.</li> </ul> <h3 id=key-order>Key order<a class=headerlink href=#key-order title="Permanent link">&para;</a></h3> <p>The order in which name/value pairs are added to the object is <em>not</em> preserved by the library. Therefore, iterating an object may return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as <code>std::map</code> with <code>std::less</code> is used by default. Please note this behavior conforms to <a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a>, because any order implements the specified "unordered" nature of <abbr title="JavaScript Object Notation">JSON</abbr> objects.</p> <h3 id=limits>Limits<a class=headerlink href=#limits title="Permanent link">&para;</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> specifies:</p> <blockquote> <p>An implementation may set limits on the maximum depth of nesting.</p> </blockquote> <p>In this class, the object's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the <code>max_size</code> function of a <abbr title="JavaScript Object Notation">JSON</abbr> object.</p> <h3 id=storage_1>Storage<a class=headerlink href=#storage_1 title="Permanent link">&para;</a></h3> <p>Objects are stored as pointers in a <code>basic_json</code> type. That is, for any access to obLine truncated
<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=n>m</span><span class=p>;</span>
<span class=c1>// j is [[1,"one"],[2,"two"]], not {"1":"one","2":"two"}</span>
</code></pre></div> <h2 id=arrays>Arrays<a class=headerlink href=#arrays title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> arrays as follows:</p> <blockquote> <p>An array is an ordered sequence of zero or more values.</p> </blockquote> <h3 id=default-type_1>Default type<a class=headerlink href=#default-type_1 title="Permanent link">&para;</a></h3> <p>With the default values for <em>ArrayType</em> (<code>std::vector</code>) and <em>AllocatorType</em> (<code>std::allocator</code>), the default value for <code>array_t</code> is:</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>
</code></pre></div> <h2 id=arrays>Arrays<a class=headerlink href=#arrays title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> arrays as follows:</p> <blockquote> <p>An array is an ordered sequence of zero or more values.</p> </blockquote> <h3 id=default-type_1>Default type<a class=headerlink href=#default-type_1 title="Permanent link">&para;</a></h3> <p>With the default values for <em>ArrayType</em> (<code>std::vector</code>) and <em>AllocatorType</em> (<code>std::allocator</code>), the default type for <code>array_t</code> is:</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=w> </span><span class=n>basic_json</span><span class=p>,</span><span class=w> </span><span class=c1>// value_type</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>allocator</span><span class=o>&lt;</span><span class=n>basic_json</span><span class=o>&gt;</span><span class=w> </span><span class=c1>// allocator_type</span>
<span class=o>&gt;</span>
</code></pre></div> <h3 id=limits_1>Limits<a class=headerlink href=#limits_1 title="Permanent link">&para;</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> specifies:</p> <blockquote> <p>An implementation may set limits on the maximum depth of nesting.</p> </blockquote> <p>In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the <code>max_size</code> function of a <abbr title="JavaScript Object Notation">JSON</abbr> array.</p> <h3 id=storage_2>Storage<a class=headerlink href=#storage_2 title="Permanent link">&para;</a></h3> <p>Arrays are stored as pointers in a <code>basic_json</code> type. That is, for any access to array values, a pointer of type <code>array_t*</code> must be dereferenced.</p> <h2 id=strings>Strings<a class=headerlink href=#strings title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> strings as follows:</p> <blockquote> <p>A string is a sequence of zero or more Unicode characters.</p> </blockquote> <p>Unicode values are split by the <abbr title="JavaScript Object Notation">JSON</abbr> class into byte-sized characters during deserialization.</p> <h3 id=default-type_2>Default type<a class=headerlink href=#default-type_2 title="Permanent link">&para;</a></h3> <p>With the default values for <em>StringType</em> (<code>std::string</code>), the default value for <code>string_t</code> is <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code>.</p> <h3 id=encoding>Encoding<a class=headerlink href=#encoding title="Permanent link">&para;</a></h3> <p>Strings are stored in <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoding. Therefore, functions like <code>std::string::size()</code> or <code>std::string::length()</code> return the number of <strong>bytes</strong> in the string rather than the number of characters or glyphs.</p> <h3 id=string-comparison>String comparison<a class=headerlink href=#string-comparison title="Permanent link">&para;</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> states:</p> <blockquote> <p>Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that <code>"a\\b"</code> and <code>"a\u005Cb"</code> are not equal.</p> </blockquote> <p>This implementation is interoperable as it does compare strings code unit by code unit.</p> <h3 id=storage_3>Storage<a class=headerlink href=#storage_3 title="Permanent link">&para;</a></h3> <p>String values are stored as pointers in a <code>basic_json</code> type. That is, for any access to string values, a pointer of type <code>string_t*</code> must be dereferenced.</p> <h2 id=booleans>Booleans<a class=headerlink href=#booleans title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> implicitly describes a boolean as a type which differentiates the two literals <code>true</code> and <code>false</code>.</p> <h3 id=default-type_3>Default type<a class=headerlink href=#default-type_3 title="Permanent link">&para;</a></h3> <p>With the default values for <em>BooleanType</em> (<code class=highlight><span class=kt>bool</span></code>), the default value for <code>boolean_t</code> is <code class=highlight><span class=kt>bool</span></code>.</p> <h3 id=storage_4>Storage<a class=headerlink href=#storage_4 title="Permanent link">&para;</a></h3> <p>Boolean values are stored directly inside a <code>basic_json</code> type.</p> <h2 id=numbers>Numbers<a class=headerlink href=#numbers title="Permanent link">&para;</a></h2> <p>See the <a href=number_handling/ >number handling</a> article for a detailed discussion on how numbers are handled by this library.</p> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes numbers as follows:</p> <blockquote> <p>The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be reprLine truncated
</code></pre></div> <h3 id=limits_1>Limits<a class=headerlink href=#limits_1 title="Permanent link">&para;</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> specifies:</p> <blockquote> <p>An implementation may set limits on the maximum depth of nesting.</p> </blockquote> <p>In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the <code>max_size</code> function of a <abbr title="JavaScript Object Notation">JSON</abbr> array.</p> <h3 id=storage_2>Storage<a class=headerlink href=#storage_2 title="Permanent link">&para;</a></h3> <p>Arrays are stored as pointers in a <code>basic_json</code> type. That is, for any access to array values, a pointer of type <code>array_t*</code> must be dereferenced.</p> <h2 id=strings>Strings<a class=headerlink href=#strings title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> strings as follows:</p> <blockquote> <p>A string is a sequence of zero or more Unicode characters.</p> </blockquote> <p>Unicode values are split by the <abbr title="JavaScript Object Notation">JSON</abbr> class into byte-sized characters during deserialization.</p> <h3 id=default-type_2>Default type<a class=headerlink href=#default-type_2 title="Permanent link">&para;</a></h3> <p>With the default values for <em>StringType</em> (<code>std::string</code>), the default type for <code>string_t</code> is <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code>.</p> <h3 id=encoding>Encoding<a class=headerlink href=#encoding title="Permanent link">&para;</a></h3> <p>Strings are stored in <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoding. Therefore, functions like <code>std::string::size()</code> or <code>std::string::length()</code> return the number of <strong>bytes</strong> in the string rather than the number of characters or glyphs.</p> <h3 id=string-comparison>String comparison<a class=headerlink href=#string-comparison title="Permanent link">&para;</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> states:</p> <blockquote> <p>Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that <code>"a\\b"</code> and <code>"a\u005Cb"</code> are not equal.</p> </blockquote> <p>This implementation is interoperable as it does compare strings code unit by code unit.</p> <h3 id=storage_3>Storage<a class=headerlink href=#storage_3 title="Permanent link">&para;</a></h3> <p>String values are stored as pointers in a <code>basic_json</code> type. That is, for any access to string values, a pointer of type <code>string_t*</code> must be dereferenced.</p> <h2 id=booleans>Booleans<a class=headerlink href=#booleans title="Permanent link">&para;</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> implicitly describes a boolean as a type which differentiates the two literals <code>true</code> and <code>false</code>.</p> <h3 id=default-type_3>Default type<a class=headerlink href=#default-type_3 title="Permanent link">&para;</a></h3> <p>With the default values for <em>BooleanType</em> (<code class=highlight><span class=kt>bool</span></code>), the default type for <code>boolean_t</code> is <code class=highlight><span class=kt>bool</span></code>.</p> <h3 id=storage_4>Storage<a class=headerlink href=#storage_4 title="Permanent link">&para;</a></h3> <p>Boolean values are stored directly inside a <code>basic_json</code> type.</p> <h2 id=numbers>Numbers<a class=headerlink href=#numbers title="Permanent link">&para;</a></h2> <p>See the <a href=number_handling/ >number handling</a> article for a detailed discussion on how numbers are handled by this library.</p> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes numbers as follows:</p> <blockquote> <p>The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be represLine truncated
+6 -6
View File
@@ -117,7 +117,7 @@ Not every type can be passed for these template arguments: the library uses the
### Default type
With the default values for *ObjectType* (`std::map`), *StringType* (`std::string`), and *AllocatorType* (`std::allocator`), the default value for `object_t` is:
With the default values for *ObjectType* (`std::map`), *StringType* (`std::string`), and *AllocatorType* (`std::allocator`), the default type for `object_t` is:
```
std::map<
@@ -171,7 +171,7 @@ json j = m;
### Default type
With the default values for *ArrayType* (`std::vector`) and *AllocatorType* (`std::allocator`), the default value for `array_t` is:
With the default values for *ArrayType* (`std::vector`) and *AllocatorType* (`std::allocator`), the default type for `array_t` is:
```
std::vector<
@@ -202,7 +202,7 @@ Unicode values are split by the JSON class into byte-sized characters during des
### Default type
With the default values for *StringType* (`std::string`), the default value for `string_t` is `std::string`.
With the default values for *StringType* (`std::string`), the default type for `string_t` is `std::string`.
### Encoding
@@ -226,7 +226,7 @@ String values are stored as pointers in a `basic_json` type. That is, for any ac
### Default type
With the default values for *BooleanType* (`bool`), the default value for `boolean_t` is `bool`.
With the default values for *BooleanType* (`bool`), the default type for `boolean_t` is `bool`.
### Storage
@@ -244,7 +244,7 @@ This description includes both integer and floating-point numbers. However, C++
### Default types
With the default values for *NumberIntegerType* (`std::int64_t`), the default value for `number_integer_t` is `std::int64_t`. With the default values for *NumberUnsignedType* (`std::uint64_t`), the default value for `number_unsigned_t` is `std::uint64_t`. With the default values for *NumberFloatType* (`double`), the default value for `number_float_t` is `double`.
With the default values for *NumberIntegerType* (`std::int64_t`), the default type for `number_integer_t` is `std::int64_t`. With the default values for *NumberUnsignedType* (`std::uint64_t`), the default type for `number_unsigned_t` is `std::uint64_t`. With the default values for *NumberFloatType* (`double`), the default type for `number_float_t` is `double`.
### Default behavior
@@ -271,7 +271,7 @@ As this range is a subrange of the exactly supported range \[`INT64_MIN`, `INT64
> This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision.
This implementation does exactly follow this approach, as it uses double precision floating-point numbers. Note values smaller than `-1.79769313486232e+308` and values greater than `1.79769313486232e+308` will be stored as NaN internally and be serialized to `null`.
This implementation does exactly follow this approach, as it uses double precision floating-point numbers by default (see *NumberFloatType* above). Note that with the default `double`, numbers smaller than `-1.79769313486232e+308` or greater than `1.79769313486232e+308` are rejected during parsing with [`out_of_range.406`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range406). Non-finite values (NaN and infinity) can only be stored by assigning them directly and are serialized to `null`.
### Storage
+14 -12
View File
@@ -82,12 +82,13 @@ flowchart TD
- Numbers with a decimal digit or scientific notation are always stored as `#!c double`.
- The number types can be changed, see [Template number types](#template-number-types).
- Integers are converted by the library's own digit parser. Floating-point numbers are converted with
[`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars) if the library is compiled with C++17
and the standard library supports it, then with an exact fast path for `#!c double` values with few significant
digits, and otherwise with the locale-aware
[`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) (`std::strtof`/`std::strtold` for the
other floating-point types). Before version 3.13.0, the conversion was realized by
- The library converts integers and floating-point numbers itself, independent of the locale. Floating-point
numbers are correctly rounded (to nearest, ties to even). Only a `#!c long double` that is not IEEE 754 binary64
(e.g., the 80-bit x87 format) is converted with `#!cpp std::from_chars` where available, or else with
[`std::strtold`](https://en.cppreference.com/w/cpp/string/byte/strtof). For that call, the library temporarily
replaces the `.` with the decimal point of the current locale (which may be longer than one byte, e.g., in
`fa_IR.UTF-8`), so the result does not depend on the locale either. Changing the locale in another thread during
parsing is undefined behavior of the C library, though. Before version 3.13.0, the conversion was realized by
[`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul),
[`std::strtoll`](https://en.cppreference.com/w/cpp/string/byte/strtol), and `std::strtod`, respectively.
@@ -100,10 +101,10 @@ flowchart TD
### Number limits
- Any 64-bit signed or unsigned integer can be stored without loss of precision.
- Numbers exceeding the limits of `#!c double` (i.e., numbers that after conversion via
[`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) are not satisfying
- Numbers exceeding the limits of `#!c double` (i.e., numbers whose rounded value is not satisfying
[`std::isfinite`](https://en.cppreference.com/w/cpp/numeric/math/isfinite) such as `#!c 1E400`) will throw exception
[`json.exception.out_of_range.406`](../../home/exceptions.md#jsonexceptionout_of_range406) during parsing.
[`json.exception.out_of_range.406`](../../home/exceptions.md#jsonexceptionout_of_range406) during parsing. Numbers too
small for `#!c double` (such as `#!c 1E-400`) become zero, with the sign of the number.
- Floating-point numbers are rounded to the next number representable as `double`. For instance
`#!c 3.141592653589793238462643383279` is stored as [`0x400921fb54442d18`](https://float.exposed/0x400921fb54442d18).
This is the same behavior as the code `#!c double x = 3.141592653589793238462643383279;`.
@@ -133,9 +134,10 @@ That is, `-0` is stored as a signed integer, but the serialization does not repr
### Number serialization
- Integer numbers are serialized as is; that is, no scientific notation is used.
- Floating-point numbers are serialized as specified by the `#!c %g` printf modifier with
[`std::numeric_limits<double>::max_digits10`](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10)
significant digits. The rationale is to use the shortest representation while still allowing round-tripping.
- Floating-point numbers are serialized with the fewest digits that read back as the same value (the closest such
digits if there are several), in the layout of the `#!c %g` printf modifier: `#!c 1.5`, `#!c 100.0`, `#!c 1e+100`.
Doubles are converted with the algorithm of [Żmij](https://github.com/vitaut/zmij), floats with Grisu2, which
can write more digits than necessary.
!!! hint "Notes regarding precision of floating-point numbers"
+2 -2
View File
@@ -5,7 +5,7 @@
C --&gt;|"yes"| D["try number_integer_t"]
C --&gt;|"no"| E["try number_unsigned_t"]
D --&gt;|"overflow"| F
E --&gt;|"overflow"| F</code></pre> <div class="admonition info"> <p class=admonition-title>Notes</p> <ul> <li>Numbers with a decimal digit or scientific notation are always stored as <code class=highlight><span class=kt>double</span></code>.</li> <li>The number types can be changed, see <a href=#template-number-types>Template number types</a>. </li> <li>Integers are converted by the library's own digit parser. Floating-point numbers are converted with <a href="https://en.cppreference.com/w/cpp/utility/from_chars"><code>std::from_chars</code></a> if the library is compiled with C++17 and the standard library supports it, then with an exact fast path for <code class=highlight><span class=kt>double</span></code> values with few significant digits, and otherwise with the locale-aware <a href="https://en.cppreference.com/w/cpp/string/byte/strtof"><code>std::strtod</code></a> (<code>std::strtof</code>/<code>std::strtold</code> for the other floating-point types). Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, the conversion was realized by <a href="https://en.cppreference.com/w/cpp/string/byte/strtoul"><code>std::strtoull</code></a>, <a href="https://en.cppreference.com/w/cpp/string/byte/strtol"><code>std::strtoll</code></a>, and <code>std::strtod</code>, respectively.</li> </ul> </div> <div class="admonition example"> <p class=admonition-title>Examples</p> <ul> <li>Integer <code class=highlight><span class=mi>-12345678912345789123456789</span></code> is smaller than <code class=highlight><span class=n>INT64_MIN</span></code> and will be stored as floating-point number <code class=highlight><span class=mf>-1.2345678912345788e+25</span></code>.</li> <li>Integer <code class=highlight><span class=mf>1E3</span></code> will be stored as floating-point number <code class=highlight><span class=mf>1000.0</span></code>.</li> </ul> </div> <h3 id=number-limits>Number limits<a class=headerlink href=#number-limits title="Permanent link">&para;</a></h3> <ul> <li>Any 64-bit signed or unsigned integer can be stored without loss of precision.</li> <li>Numbers exceeding the limits of <code class=highlight><span class=kt>double</span></code> (i.e., numbers that after conversion via <a href="https://en.cppreference.com/w/cpp/string/byte/strtof"><code>std::strtod</code></a> are not satisfying <a href="https://en.cppreference.com/w/cpp/numeric/math/isfinite"><code>std::isfinite</code></a> such as <code class=highlight><span class=mf>1E400</span></code>) will throw exception <a href=../../../home/exceptions/#jsonexceptionout_of_range406><code>json.exception.out_of_range.406</code></a> during parsing.</li> <li>Floating-point numbers are rounded to the next number representable as <code>double</code>. For instance <code class=highlight><span class=mf>3.141592653589793238462643383279</span></code> is stored as <a href="https://float.exposed/0x400921fb54442d18"><code>0x400921fb54442d18</code></a>. This is the same behavior as the code <code class=highlight><span class=kt>double</span><span class=w> </span><span class=n>x</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mf>3.141592653589793238462643383279</span><span class=p>;</span></code>.</li> </ul> <div class="admonition success"> <p class=admonition-title>Interoperability</p> <ul> <li>The library is interoperable with respect to the specification, because its supported range [-2<sup>63</sup>, 2<sup>64</sup>-1] is larger than the described range [-2<sup>53</sup>+1, 2<sup>53</sup>-1].</li> <li>All integers outside the range [-2<sup>63</sup>, 2<sup>64</sup>-1], as well as floating-point numbers are stored as <code>double</code>. This also concurs with the specification above.</li> </ul> </div> <h3 id=zeros>Zeros<a class=headerlink href=#zeros title="Permanent link">&para;</a></h3> <p>The <abbr title="JavaScript Object Notation">JSON</abbr> number grammar allows for different ways to express zero, and this library will store zeros differently:</p> <table> <thead> <tr> <th>Literal</th> <th>Stored value and type</th> <th>Serialization</th> </tr> </thead> <tbody> <tr> <td><code>0</code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint64_t</span><span class=p>(</span><span class=mi>0</span><span class=p>)</span></code></td> <td><code>0</code></td> </tr> <tr> <td><code>-0</code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int64_t</span><span class=p>(</span><span class=mi>0</span><span class=p>)</span></code></td> <td><code>0</code></td> </tr> <tr> <td><code>0.0</code></td> <td><code class=highlight><span class=kt>double</span><span class=p>(</span><span class=mf>0.0</span><span class=p>)</span></code></td> <td><code>0.0</code></td> </tr> <tr> <td><code>-0.0</code></td> <td><code class=highlight><span class=kt>double</span><span class=p>(</span><span class=mf>-0.0</spanLine truncated
E --&gt;|"overflow"| F</code></pre> <div class="admonition info"> <p class=admonition-title>Notes</p> <ul> <li>Numbers with a decimal digit or scientific notation are always stored as <code class=highlight><span class=kt>double</span></code>.</li> <li>The number types can be changed, see <a href=#template-number-types>Template number types</a>. </li> <li>The library converts integers and floating-point numbers itself, independent of the locale. Floating-point numbers are correctly rounded (to nearest, ties to even). Only a <code class=highlight><span class=kt>long</span><span class=w> </span><span class=kt>double</span></code> that is not <abbr title="Institute of Electrical and Electronics Engineers">IEEE</abbr> 754 binary64 (e.g., the 80-bit x87 format) is converted with <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>from_chars</span></code> where available, or else with <a href="https://en.cppreference.com/w/cpp/string/byte/strtof"><code>std::strtold</code></a>. For that call, the library temporarily replaces the <code>.</code> with the decimal point of the current locale (which may be longer than one byte, e.g., in <code>fa_IR.UTF-8</code>), so the result does not depend on the locale either. Changing the locale in another thread during parsing is undefined behavior of the C library, though. Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, the conversion was realized by <a href="https://en.cppreference.com/w/cpp/string/byte/strtoul"><code>std::strtoull</code></a>, <a href="https://en.cppreference.com/w/cpp/string/byte/strtol"><code>std::strtoll</code></a>, and <code>std::strtod</code>, respectively.</li> </ul> </div> <div class="admonition example"> <p class=admonition-title>Examples</p> <ul> <li>Integer <code class=highlight><span class=mi>-12345678912345789123456789</span></code> is smaller than <code class=highlight><span class=n>INT64_MIN</span></code> and will be stored as floating-point number <code class=highlight><span class=mf>-1.2345678912345788e+25</span></code>.</li> <li>Integer <code class=highlight><span class=mf>1E3</span></code> will be stored as floating-point number <code class=highlight><span class=mf>1000.0</span></code>.</li> </ul> </div> <h3 id=number-limits>Number limits<a class=headerlink href=#number-limits title="Permanent link">&para;</a></h3> <ul> <li>Any 64-bit signed or unsigned integer can be stored without loss of precision.</li> <li>Numbers exceeding the limits of <code class=highlight><span class=kt>double</span></code> (i.e., numbers whose rounded value is not satisfying <a href="https://en.cppreference.com/w/cpp/numeric/math/isfinite"><code>std::isfinite</code></a> such as <code class=highlight><span class=mf>1E400</span></code>) will throw exception <a href=../../../home/exceptions/#jsonexceptionout_of_range406><code>json.exception.out_of_range.406</code></a> during parsing. Numbers too small for <code class=highlight><span class=kt>double</span></code> (such as <code class=highlight><span class=mf>1E-400</span></code>) become zero, with the sign of the number.</li> <li>Floating-point numbers are rounded to the next number representable as <code>double</code>. For instance <code class=highlight><span class=mf>3.141592653589793238462643383279</span></code> is stored as <a href="https://float.exposed/0x400921fb54442d18"><code>0x400921fb54442d18</code></a>. This is the same behavior as the code <code class=highlight><span class=kt>double</span><span class=w> </span><span class=n>x</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mf>3.141592653589793238462643383279</span><span class=p>;</span></code>.</li> </ul> <div class="admonition success"> <p class=admonition-title>Interoperability</p> <ul> <li>The library is interoperable with respect to the specification, because its supported range [-2<sup>63</sup>, 2<sup>64</sup>-1] is larger than the described range [-2<sup>53</sup>+1, 2<sup>53</sup>-1].</li> <li>All integers outside the range [-2<sup>63</sup>, 2<sup>64</sup>-1], as well as floating-point numbers are stored as <code>double</code>. This also concurs with the specification above.</li> </ul> </div> <h3 id=zeros>Zeros<a class=headerlink href=#zeros title="Permanent link">&para;</a></h3> <p>The <abbr title="JavaScript Object Notation">JSON</abbr> number grammar allows for different ways to express zero, and this library will store zeros differently:</p> <table> <thead> <tr> <th>Literal</th> <th>Stored value and type</th> <th>Serialization</th> </tr> </thead> <tbody> <tr> <td><code>0</code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint64_t</span><span class=p>(</span><span class=mi>0</span><span class=p>)</span></code></td> <td><code>0</code></td> </tr> <tr> <td><code>-0</code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span>Line truncated
<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=n>f</span><span class=p>;</span>
<span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
</code></pre></div> yields <code class=highlight><span class=mf>0.30000001192092896</span></code>.</li> </ul> <p>All examples here can be reproduced by passing the original double value to</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>printf</span><span class=p>(</span><span class=s>"%.*g</span><span class=se>\n</span><span class=s>"</span><span class=p>,</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>numeric_limits</span><span class=o>&lt;</span><span class=kt>double</span><span class=o>&gt;::</span><span class=n>max_digits10</span><span class=p>,</span><span class=w> </span><span class=n>double_value</span><span class=p>);</span>
@@ -60,4 +60,4 @@ val=nan
<span class=w> </span><span class=c1>// ...</span>
<span class=p>}</span>
</code></pre></div> <p>Note this approach also has the advantage that it can react on non-numerical <abbr title="JavaScript Object Notation">JSON</abbr> value types such as strings.</p> <p>(Example taken from <a href="https://github.com/nlohmann/json/issues/777#issuecomment-459968458">#777</a>.)</p> </div> <h3 id=determine-number-types>Determine number types<a class=headerlink href=#determine-number-types title="Permanent link">&para;</a></h3> <p>As the example in <a href=#number-conversion>Number conversion</a> shows, there are different functions to determine the type of the stored number:</p> <ul> <li><a href=../../../api/basic_json/is_number/ ><code>is_number()</code></a> returns <code class=highlight><span class=nb>true</span></code> for any number type</li> <li><a href=../../../api/basic_json/is_number_integer/ ><code>is_number_integer()</code></a> returns <code class=highlight><span class=nb>true</span></code> for signed and unsigned integers</li> <li><a href=../../../api/basic_json/is_number_unsigned/ ><code>is_number_unsigned()</code></a> returns <code class=highlight><span class=nb>true</span></code> for unsigned integers only</li> <li><a href=../../../api/basic_json/is_number_float/ ><code>is_number_float()</code></a> returns <code class=highlight><span class=nb>true</span></code> for floating-point numbers</li> <li><a href=../../../api/basic_json/type_name/ ><code>type_name()</code></a> returns <code class=highlight><span class=s>"number"</span></code> for any number type</li> <li><a href=../../../api/basic_json/type/ ><code>type()</code></a> returns a different enumerator of <a href=../../../api/basic_json/value_t/ ><code>value_t</code></a> for all number types</li> </ul> <table> <thead> <tr> <th>function</th> <th>unsigned integer</th> <th>signed integer</th> <th>floating-point</th> <th>string</th> </tr> </thead> <tbody> <tr> <td><a href=../../../api/basic_json/is_number/ ><code>is_number()</code></a></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/is_number_integer/ ><code>is_number_integer()</code></a></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/is_number_unsigned/ ><code>is_number_unsigned()</code></a></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/is_number_float/ ><code>is_number_float()</code></a></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/type_name/ ><code>type_name()</code></a></td> <td><code class=highlight><span class=s>"number"</span></code></td> <td><code class=highlight><span class=s>"number"</span></code></td> <td><code class=highlight><span class=s>"number"</span></code></td> <td><code class=highlight><span class=s>"string"</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/type/ ><code>type()</code></a></td> <td><code>number_unsigned</code></td> <td><code>number_integer</code></td> <td><code>number_float</code></td> <td><code>string</code></td> </tr> </tbody> </table> <h3 id=template-number-types>Template number types<a class=headerlink href=#template-number-types title="Permanent link">&para;</a></h3> <p>The number types can be changed with template parameters.</p> <table> <thead> <tr> <th>position</th> <th>number type</th> <th>default type</th> <th>possible values</th> </tr> </thead> <tbody> <tr> <td>5</td> <td>signed integers</td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int64_t</span></code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int32_t</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int16_t</span></code>, etc.</td> </tr> <tr> <td>6</td> <td>unsigned integers</td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint64_t</span></code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint32_t</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</spanLine truncated
</span></code></pre></div> <p>Note values should then be parsed with <code>json_ld::parse</code> rather than <code>json::parse</code> as the latter would parse floating-point values to <code class=highlight><span class=kt>double</span></code> before then converting them to <code class=highlight><span class=kt>long</span><span class=w> </span><span class=kt>double</span></code>.</p> </div> <!-- 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 8, 2026 14:11:16 UTC">October 8, 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 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, Line truncated
</span></code></pre></div> <p>Note values should then be parsed with <code>json_ld::parse</code> rather than <code>json::parse</code> as the latter would parse floating-point values to <code class=highlight><span class=kt>double</span></code> before then converting them to <code class=highlight><span class=kt>long</span><span class=w> </span><span class=kt>double</span></code>.</p> </div> <!-- 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:38:37 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, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 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.1Line truncated
+3 -3
View File
@@ -55,7 +55,7 @@ Notes
- Numbers with a decimal digit or scientific notation are always stored as `double`.
- The number types can be changed, see [Template number types](#template-number-types).
- Integers are converted by the library's own digit parser. Floating-point numbers are converted with [`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars) if the library is compiled with C++17 and the standard library supports it, then with an exact fast path for `double` values with few significant digits, and otherwise with the locale-aware [`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) (`std::strtof`/`std::strtold` for the other floating-point types). Before version 3.13.0 unreleased, the conversion was realized by [`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul), [`std::strtoll`](https://en.cppreference.com/w/cpp/string/byte/strtol), and `std::strtod`, respectively.
- The library converts integers and floating-point numbers itself, independent of the locale. Floating-point numbers are correctly rounded (to nearest, ties to even). Only a `long double` that is not IEEE 754 binary64 (e.g., the 80-bit x87 format) is converted with `std::from_chars` where available, or else with [`std::strtold`](https://en.cppreference.com/w/cpp/string/byte/strtof). For that call, the library temporarily replaces the `.` with the decimal point of the current locale (which may be longer than one byte, e.g., in `fa_IR.UTF-8`), so the result does not depend on the locale either. Changing the locale in another thread during parsing is undefined behavior of the C library, though. Before version 3.13.0 unreleased, the conversion was realized by [`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul), [`std::strtoll`](https://en.cppreference.com/w/cpp/string/byte/strtol), and `std::strtod`, respectively.
Examples
@@ -65,7 +65,7 @@ Examples
### Number limits
- Any 64-bit signed or unsigned integer can be stored without loss of precision.
- Numbers exceeding the limits of `double` (i.e., numbers that after conversion via [`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) are not satisfying [`std::isfinite`](https://en.cppreference.com/w/cpp/numeric/math/isfinite) such as `1E400`) will throw exception [`json.exception.out_of_range.406`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range406) during parsing.
- Numbers exceeding the limits of `double` (i.e., numbers whose rounded value is not satisfying [`std::isfinite`](https://en.cppreference.com/w/cpp/numeric/math/isfinite) such as `1E400`) will throw exception [`json.exception.out_of_range.406`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range406) during parsing. Numbers too small for `double` (such as `1E-400`) become zero, with the sign of the number.
- Floating-point numbers are rounded to the next number representable as `double`. For instance `3.141592653589793238462643383279` is stored as [`0x400921fb54442d18`](https://float.exposed/0x400921fb54442d18). This is the same behavior as the code `double x = 3.141592653589793238462643383279;`.
Interoperability
@@ -91,7 +91,7 @@ That is, `-0` is stored as a signed integer, but the serialization does not repr
### Number serialization
- Integer numbers are serialized as is; that is, no scientific notation is used.
- Floating-point numbers are serialized as specified by the `%g` printf modifier with [`std::numeric_limits<double>::max_digits10`](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10) significant digits. The rationale is to use the shortest representation while still allowing round-tripping.
- Floating-point numbers are serialized with the fewest digits that read back as the same value (the closest such digits if there are several), in the layout of the `%g` printf modifier: `1.5`, `100.0`, `1e+100`. Doubles are converted with the algorithm of [Żmij](https://github.com/vitaut/zmij), floats with Grisu2, which can write more digits than necessary.
Notes regarding precision of floating-point numbers
+21 -14
View File
@@ -30,9 +30,9 @@ Requirements are split into two groups:
diagnosed with dedicated error messages, and violating most of them results in a compiler error somewhere inside
the library. Four violations are not caught at compile time at all:
- A [`StringType`](#stringtype) whose `data()` is not null-terminated compiles and can silently misparse
floating-point numbers, because the lexer may hand the buffer to `#!cpp std::strtod`, which reads up to the
terminating null character.
- A [`StringType`](#stringtype) whose `data()` is not null-terminated compiles and silently misparses numbers
stored as a `#!cpp long double` that is not IEEE 754 binary64 (e.g., the 80-bit x87 format), because the lexer
hands the buffer to `#!cpp std::strtold`.
- A stateful [`AllocatorType`](#allocatortype) compiles and silently ignores its state: allocation, deallocation,
and [`get_allocator()`](../../api/basic_json/get_allocator.md) each use a different default-constructed instance.
- The two [cross-specialization conversions](#cross-specialization-conversions) below. These abort on an assertion
@@ -705,16 +705,21 @@ using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
### Always required
- A member type `value_type` that is one byte wide and `char`-compatible. The library stores and processes UTF-8
encoded `char` data and passes `data()` to functions that take a `#!cpp const char*`, such as `#!cpp std::strtod`.
encoded `char` data and passes `data()` to functions that take a `#!cpp const char*`, such as `#!cpp std::strtold`
(only used to parse a `#!cpp long double` that is not IEEE 754 binary64, see
[`NumberFloatType`](#numberfloattype)).
`#!cpp std::wstring`, `#!cpp std::u16string`, and `#!cpp std::u32string` are **not** valid choices; see the FAQ on
[wide string handling](../../home/faq.md#wide-string-handling).
- Constructors: default, copy, move, from `#!cpp const char*` (which must not be `#!cpp explicit`), from
`#!cpp (const char*, size_type)`, and from `#!cpp (size_type, char)`; and copy or move assignment.
- Member functions `size()`, `clear()`, `resize(n, c)`, `data()`, `push_back(char)`, and `operator[]`
(const and non-const, returning references). `c_str()` and `back()` are **not** required.
- `data()` must return a pointer to a contiguous, **null-terminated** buffer -- the parser may hand it to
`#!cpp std::strtod`, which reads up to the null character. A type whose `data()` is not null-terminated does not
fail to compile; it can silently misparse floating-point numbers.
- `data()` must return a pointer to a contiguous, **null-terminated** buffer. `#!cpp float`, `#!cpp double`, and a
`#!cpp long double` that is IEEE 754 binary64 are converted by the library itself and do not depend on this. For any
other `NumberFloatType` (a `#!cpp long double` of another format), the parser falls back to `#!cpp std::strtold` when
`#!cpp std::from_chars` is not available or declines the token, and `std::strtold` reads up to the null character. A type whose `data()`
is not null-terminated does not fail to compile; with such a `NumberFloatType` it can silently misparse
floating-point numbers.
- `append(const char*, size_type)`, used by [`dump`](../../api/basic_json/dump.md), and `append(const StringType&)`,
used by the CBOR reader for indefinite-length strings. The library's internal string concatenation additionally has
to append a `#!cpp char` and a `#!cpp const char*`; for each it selects between `append(arg)`, `#!cpp operator+=`,
@@ -1067,16 +1072,18 @@ therefore silently changes parse results rather than raising an error. See
`NumberFloatType` must be one of `#!cpp float`, `#!cpp double`, or `#!cpp long double`:
- The [parser](../parsing/index.md) converts number literals with `#!cpp std::from_chars` or, as a fallback, with
`#!cpp std::strtof`, `#!cpp std::strtod`, or `#!cpp std::strtold`; the library provides overloads for exactly these
three types.
- The [parser](../parsing/index.md) converts number literals to `#!cpp float`, `#!cpp double`, and a
`#!cpp long double` that is IEEE 754 binary64 itself; other `#!cpp long double` formats are converted with
`#!cpp std::from_chars` where available, or with `#!cpp std::strtold`. The library provides overloads for exactly
these three types.
- [`dump`](../../api/basic_json/dump.md) falls back to `#!cpp std::snprintf` with the `%g` and `%Lg` conversion
specifiers, for which the library likewise provides only `#!cpp double` and `#!cpp long double` overloads
(`#!cpp float` is promoted to `#!cpp double`).
If `#!cpp std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary32 or binary64 number, `dump` uses the
Grisu2 algorithm, which produces the shortest representation that round-trips. Otherwise the `snprintf` fallback with
`max_digits10` digits is used.
If `#!cpp std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary64 number, `dump` uses the algorithm of
Żmij, which produces the shortest representation that round-trips. For IEEE 754 binary32 numbers, it uses Grisu2,
which produces a short representation that round-trips. Otherwise the `snprintf` fallback with `max_digits10` digits is
used.
### Required for the binary formats
@@ -1088,7 +1095,7 @@ binary32 or binary64 field and have no encoding for `#!cpp long double`.
| Type | Support |
|--------------------------|-----------------------------------------------------------------------------------------------------------------------|
| `#!cpp double` (default) | full; short round-trip output through Grisu2 |
| `#!cpp double` (default) | full; shortest round-trip output through Żmij |
| `#!cpp float` | full; short round-trip output through Grisu2 |
| `#!cpp long double` | `dump` and `parse` only; the binary format writers do not compile, as they only handle IEEE 754 binary32 and binary64 |
| any other type | not usable |
@@ -1,4 +1,4 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/types/template_parameters/" rel="canonical"><link href=../number_handling/ rel=prev><link href=../../../integration/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Template Parameter Requirements - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#template-parameter-requirements class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Template Parameter Requirements </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5Line truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/types/template_parameters/" rel="canonical"><link href=../number_handling/ rel=prev><link href=../../../integration/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Template Parameter Requirements - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#template-parameter-requirements class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Template Parameter Requirements </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5Line truncated
<span class=w> </span><span class=n>basic_json</span><span class=p>,</span><span class=w> </span><span class=c1>// mapped_type</span>
<span class=w> </span><span class=n>default_object_comparator_t</span><span class=p>,</span><span class=w> </span><span class=c1>// key_compare</span>
<span class=w> </span><span class=n>AllocatorType</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=k>const</span><span class=w> </span><span class=n>StringType</span><span class=p>,</span>
@@ -374,7 +374,7 @@
<span class=p>}</span>
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=mi>2</span><span class=p>,</span><span class=mi>3</span><span class=p>]</span>
<span class=kc>true</span>
</code></pre></div> </details> <h3 id=compatible-containers_1>Compatible containers<a class=headerlink href=#compatible-containers_1 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Notes</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span></code> (default)</td> <td></td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>deque</span></code></td> <td>references survive appends, but not insertions elsewhere; see the <code>capacity()</code> note above</td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>pmr</span><span class=o>::</span><span class=n>vector</span></code></td> <td>through an alias, as the allocator comes from <code>AllocatorType</code> instead</td> </tr> <tr> <td><code>boost::container::vector</code>, <code>deque</code>, <code>devector</code></td> <td></td> </tr> <tr> <td><code>boost::container::stable_vector</code></td> <td>the only one tried that keeps references valid across <em>every</em> insertion</td> </tr> <tr> <td><code>boost::container::small_vector</code>, <code>folly::small_vector</code></td> <td>through an alias that fixes the inline capacity</td> </tr> <tr> <td><code>boost::container::static_vector</code></td> <td>through the same kind of alias, for arrays that stay within the fixed capacity</td> </tr> <tr> <td><code>folly::fbvector</code></td> <td>requires C++20, see the note above</td> </tr> </tbody> </table> <h3 id=containers-that-cannot-be-used_1>Containers that cannot be used<a class=headerlink href=#containers-that-cannot-be-used_1 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Reason</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>list</span></code></td> <td>no <code>operator[]</code>, and no random-access iterators</td> </tr> <tr> <td><code>eastl::vector</code>, <code>QList</code>, <code>QVector</code></td> <td>no <code>max_size()</code>; they handle the incomplete value type fine</td> </tr> <tr> <td><code>absl::InlinedVector</code></td> <td>requires a complete value type, see the note above</td> </tr> <tr> <td><code>absl::FixedArray</code></td> <td>the size is fixed at construction, so <code>resize</code>, <code>push_back</code>, <code>insert</code> and <code>erase</code> are missing</td> </tr> </tbody> </table> <h2 id=stringtype><code>StringType</code><a class=headerlink href=#stringtype title="Permanent link">&para;</a></h2> <p><code>StringType</code> is used <strong>both</strong> for <abbr title="JavaScript Object Notation">JSON</abbr> string values and for the keys of <abbr title="JavaScript Object Notation">JSON</abbr> objects (<code>string_t</code> and <code>object_t::key_type</code>).</p> <h3 id=always-required_2>Always required<a class=headerlink href=#always-required_2 title="Permanent link">&para;</a></h3> <ul> <li>A member type <code>value_type</code> that is one byte wide and <code>char</code>-compatible. The library stores and processes <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded <code>char</code> data and passes <code>data()</code> to functions that take a <code class=highlight><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span></code>, such as <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>strtod</span></code>. <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>wstring</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>u16string</span></code>, and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>u32string</span></code> are <strong>not</strong> valid choices; see the <abbr title="Frequently Asked Questions">FAQ</abbr> on <a href=../../../home/faq/#wide-string-handling>wide string handling</a>.</li> <li>Constructors: default, copy, move, from <code class=highlight><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span></code> (which must not be <code class=highlight><span class=k>explicit</span></code>), from <code class=highlight><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=p>,</span><span class=w> </span><span class=n>size_type</span><span class=p>)</span></code>, and from <code class=highlight><span class=p>(</span><span class=n>size_type</span><span class=p>,</span><span class=w> </span><span class=kt>char</span><span class=p>)</span></code>; and copy or move assignment.</li> <li>Member functions <code>size()</code>, <code>clear()</code>, <code>resize(n, c)</code>, <code>data()</code>, <code>push_back(char)</code>, and <code>operator[]</code> (const and non-const, returning references). <code>c_str()</code>Line truncated
</code></pre></div> </details> <h3 id=compatible-containers_1>Compatible containers<a class=headerlink href=#compatible-containers_1 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Notes</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span></code> (default)</td> <td></td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>deque</span></code></td> <td>references survive appends, but not insertions elsewhere; see the <code>capacity()</code> note above</td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>pmr</span><span class=o>::</span><span class=n>vector</span></code></td> <td>through an alias, as the allocator comes from <code>AllocatorType</code> instead</td> </tr> <tr> <td><code>boost::container::vector</code>, <code>deque</code>, <code>devector</code></td> <td></td> </tr> <tr> <td><code>boost::container::stable_vector</code></td> <td>the only one tried that keeps references valid across <em>every</em> insertion</td> </tr> <tr> <td><code>boost::container::small_vector</code>, <code>folly::small_vector</code></td> <td>through an alias that fixes the inline capacity</td> </tr> <tr> <td><code>boost::container::static_vector</code></td> <td>through the same kind of alias, for arrays that stay within the fixed capacity</td> </tr> <tr> <td><code>folly::fbvector</code></td> <td>requires C++20, see the note above</td> </tr> </tbody> </table> <h3 id=containers-that-cannot-be-used_1>Containers that cannot be used<a class=headerlink href=#containers-that-cannot-be-used_1 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Reason</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>list</span></code></td> <td>no <code>operator[]</code>, and no random-access iterators</td> </tr> <tr> <td><code>eastl::vector</code>, <code>QList</code>, <code>QVector</code></td> <td>no <code>max_size()</code>; they handle the incomplete value type fine</td> </tr> <tr> <td><code>absl::InlinedVector</code></td> <td>requires a complete value type, see the note above</td> </tr> <tr> <td><code>absl::FixedArray</code></td> <td>the size is fixed at construction, so <code>resize</code>, <code>push_back</code>, <code>insert</code> and <code>erase</code> are missing</td> </tr> </tbody> </table> <h2 id=stringtype><code>StringType</code><a class=headerlink href=#stringtype title="Permanent link">&para;</a></h2> <p><code>StringType</code> is used <strong>both</strong> for <abbr title="JavaScript Object Notation">JSON</abbr> string values and for the keys of <abbr title="JavaScript Object Notation">JSON</abbr> objects (<code>string_t</code> and <code>object_t::key_type</code>).</p> <h3 id=always-required_2>Always required<a class=headerlink href=#always-required_2 title="Permanent link">&para;</a></h3> <ul> <li>A member type <code>value_type</code> that is one byte wide and <code>char</code>-compatible. The library stores and processes <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded <code>char</code> data and passes <code>data()</code> to functions that take a <code class=highlight><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span></code>, such as <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>strtold</span></code> (only used to parse a <code class=highlight><span class=kt>long</span><span class=w> </span><span class=kt>double</span></code> that is not <abbr title="Institute of Electrical and Electronics Engineers">IEEE</abbr> 754 binary64, see <a href=#numberfloattype><code>NumberFloatType</code></a>). <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>wstring</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>u16string</span></code>, and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>u32string</span></code> are <strong>not</strong> valid choices; see the <abbr title="Frequently Asked Questions">FAQ</abbr> on <a href=../../../home/faq/#wide-string-handling>wide string handling</a>.</li> <li>Constructors: default, copy, move, from <code class=highlight><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span></code> (which must not be <code class=highlight><span class=k>explicit</span></code>), from <code class=highlight><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=kt>char</span><span class=o>*</span><span class=p>,</span><span class=w> </span><span class=n>size_type</span><span class=p>)</span></code>, and from <code class=highlight><span class=p>(</span><span class=n>size_type</span><span class=p>,</span><span class=w> </span><span class=kt>char</span><span Line truncated
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;cstddef&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;ostream&gt;</span>
@@ -548,7 +548,7 @@
<span class=w> </span><span class=nt>"pi"</span><span class=p>:</span><span class=w> </span><span class=mf>3.141</span>
<span class=p>}</span>
<span class=kc>true</span>
</code></pre></div> </details> <h2 id=booleantype><code>BooleanType</code><a class=headerlink href=#booleantype title="Permanent link">&para;</a></h2> <p><code>boolean_t</code> is stored <strong>directly</strong> inside <code>basic_json</code>, as a member of an anonymous union.</p> <h3 id=always-required_3>Always required<a class=headerlink href=#always-required_3 title="Permanent link">&para;</a></h3> <ul> <li>A literal type that is trivially default-constructible, trivially copyable, and trivially destructible; otherwise the union's special member functions are deleted.</li> <li><strong>Implicitly</strong> convertible from <code class=highlight><span class=kt>bool</span></code> -- an <code class=highlight><span class=k>explicit</span></code> constructor is not enough, because the <code>to_json</code> overload for a custom <code>BooleanType</code> is constrained on <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>is_convertible</span></code> -- and contextually convertible to <code class=highlight><span class=kt>bool</span></code> (here an <code class=highlight><span class=k>explicit</span><span class=w> </span><span class=k>operator</span><span class=w> </span><span class=kt>bool</span></code> is fine).</li> <li>Comparison operators <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code> (or <code>&lt;=&gt;</code>).</li> <li>Convertible from and to <code class=highlight><span class=kt>bool</span></code> through the serializer, because <a href=../../../api/basic_json/get/ ><code>get&lt;bool&gt;()</code></a> is used internally.</li> </ul> <p>There is little reason to use anything other than <code class=highlight><span class=kt>bool</span></code> here.</p> <h3 id=compatible-types_1>Compatible types<a class=headerlink href=#compatible-types_1 title="Permanent link">&para;</a></h3> <p><code class=highlight><span class=kt>bool</span></code> is the only usable choice. Another trivially copyable type that is implicitly convertible to and from <code class=highlight><span class=kt>bool</span></code> -- <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span></code>, say -- does compile, and <abbr title="JavaScript Object Notation">JSON</abbr> booleans still round-trip, but the type then serves as both <code>boolean_t</code> and an ordinary integer: <code>basic_json</code> can no longer be constructed or assigned from a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span></code> at all (the boolean and unsigned-integer <code>to_json</code> overloads become ambiguous), and <a href=../../../api/basic_json/get/ ><code>get&lt;std::uint8_t&gt;()</code></a> on a number throws <a href=../../../home/exceptions/#jsonexceptiontype_error302><code>type_error.302</code></a> instead of returning the value.</p> <h2 id=numberintegertype-and-numberunsignedtype><code>NumberIntegerType</code> and <code>NumberUnsignedType</code><a class=headerlink href=#numberintegertype-and-numberunsignedtype title="Permanent link">&para;</a></h2> <p>Both types are stored <strong>directly</strong> inside <code>basic_json</code>'s union.</p> <h3 id=always-required_4>Always required<a class=headerlink href=#always-required_4 title="Permanent link">&para;</a></h3> <ul> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>is_integral</span></code> must be satisfied: <code>NumberIntegerType</code> must be a <strong>signed</strong> integer type, <code>NumberUnsignedType</code> an <strong>unsigned</strong> integer type. Class types are not supported -- among others, the constructors taking integer values are constrained on <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>is_integral</span></code>.</li> <li>Trivially default-constructible, trivially copyable, and trivially destructible (union member).</li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>numeric_limits</span></code> must be specialized for both types.</li> <li><code>NumberUnsignedType</code> must be able to represent the absolute value of every <code>NumberIntegerType</code> value; serialization of negative numbers converts the value to <code>NumberUnsignedType</code>. A <code class=highlight><span class=k>static_assert</span></code> requires it to be at least as wide as <code>NumberIntegerType</code>, which is what that amounts to for the standard integer types.</li> <li>Both types must fit into the internal 64-character number buffer used by <a href=../../../api/basic_json/dump/ ><code>dump</code></a>, which is the case for all standard integer types.</li> <li><a href=../../../api/basic_json/std_hash/ ><code>std::hash&lt;basic_json&gt;</code></a> additionally requires <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>hash</span></code> specializations.</li> </ul> <h3 id=notes_1>Line truncated
</code></pre></div> </details> <h2 id=booleantype><code>BooleanType</code><a class=headerlink href=#booleantype title="Permanent link">&para;</a></h2> <p><code>boolean_t</code> is stored <strong>directly</strong> inside <code>basic_json</code>, as a member of an anonymous union.</p> <h3 id=always-required_3>Always required<a class=headerlink href=#always-required_3 title="Permanent link">&para;</a></h3> <ul> <li>A literal type that is trivially default-constructible, trivially copyable, and trivially destructible; otherwise the union's special member functions are deleted.</li> <li><strong>Implicitly</strong> convertible from <code class=highlight><span class=kt>bool</span></code> -- an <code class=highlight><span class=k>explicit</span></code> constructor is not enough, because the <code>to_json</code> overload for a custom <code>BooleanType</code> is constrained on <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>is_convertible</span></code> -- and contextually convertible to <code class=highlight><span class=kt>bool</span></code> (here an <code class=highlight><span class=k>explicit</span><span class=w> </span><span class=k>operator</span><span class=w> </span><span class=kt>bool</span></code> is fine).</li> <li>Comparison operators <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code> (or <code>&lt;=&gt;</code>).</li> <li>Convertible from and to <code class=highlight><span class=kt>bool</span></code> through the serializer, because <a href=../../../api/basic_json/get/ ><code>get&lt;bool&gt;()</code></a> is used internally.</li> </ul> <p>There is little reason to use anything other than <code class=highlight><span class=kt>bool</span></code> here.</p> <h3 id=compatible-types_1>Compatible types<a class=headerlink href=#compatible-types_1 title="Permanent link">&para;</a></h3> <p><code class=highlight><span class=kt>bool</span></code> is the only usable choice. Another trivially copyable type that is implicitly convertible to and from <code class=highlight><span class=kt>bool</span></code> -- <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span></code>, say -- does compile, and <abbr title="JavaScript Object Notation">JSON</abbr> booleans still round-trip, but the type then serves as both <code>boolean_t</code> and an ordinary integer: <code>basic_json</code> can no longer be constructed or assigned from a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span></code> at all (the boolean and unsigned-integer <code>to_json</code> overloads become ambiguous), and <a href=../../../api/basic_json/get/ ><code>get&lt;std::uint8_t&gt;()</code></a> on a number throws <a href=../../../home/exceptions/#jsonexceptiontype_error302><code>type_error.302</code></a> instead of returning the value.</p> <h2 id=numberintegertype-and-numberunsignedtype><code>NumberIntegerType</code> and <code>NumberUnsignedType</code><a class=headerlink href=#numberintegertype-and-numberunsignedtype title="Permanent link">&para;</a></h2> <p>Both types are stored <strong>directly</strong> inside <code>basic_json</code>'s union.</p> <h3 id=always-required_4>Always required<a class=headerlink href=#always-required_4 title="Permanent link">&para;</a></h3> <ul> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>is_integral</span></code> must be satisfied: <code>NumberIntegerType</code> must be a <strong>signed</strong> integer type, <code>NumberUnsignedType</code> an <strong>unsigned</strong> integer type. Class types are not supported -- among others, the constructors taking integer values are constrained on <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>is_integral</span></code>.</li> <li>Trivially default-constructible, trivially copyable, and trivially destructible (union member).</li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>numeric_limits</span></code> must be specialized for both types.</li> <li><code>NumberUnsignedType</code> must be able to represent the absolute value of every <code>NumberIntegerType</code> value; serialization of negative numbers converts the value to <code>NumberUnsignedType</code>. A <code class=highlight><span class=k>static_assert</span></code> requires it to be at least as wide as <code>NumberIntegerType</code>, which is what that amounts to for the standard integer types.</li> <li>Both types must fit into the internal 64-character number buffer used by <a href=../../../api/basic_json/dump/ ><code>dump</code></a>, which is the case for all standard integer types.</li> <li><a href=../../../api/basic_json/std_hash/ ><code>std::hash&lt;basic_json&gt;</code></a> additionally requires <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>hash</span></code> specializations.</li> </ul> <h3 id=notes_1>Line truncated
</code></pre></div> <h3 id=always-required_8>Always required<a class=headerlink href=#always-required_8 title="Permanent link">&para;</a></h3> <ul> <li>A non-<code>final</code> class type -- <a href=../../../api/byte_container_with_subtype/ ><code>byte_container_with_subtype</code></a> derives from it publicly.</li> <li>A member type <code>value_type</code> that is <strong>exactly one byte</strong> wide (e.g., <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span></code>, <code class=highlight><span class=kt>char</span></code>, or <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>byte</span></code>). Readers and writers reinterpret the container's storage as raw bytes, so a wider <code>value_type</code> is rejected with a <code class=highlight><span class=k>static_assert</span></code>.</li> <li>Contiguous storage: the binary readers <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>memcpy</span></code> into <code class=highlight><span class=o>&amp;</span><span class=n>binary</span><span class=p>[</span><span class=n>n</span><span class=p>]</span></code>, the writers <code>reinterpret_cast</code> <code>data()</code>. <code class=highlight><span class=n>data</span><span class=p>()</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=n>n</span></code> would do for the readers too, but they share one helper with <a href=#stringtype><code>StringType</code></a>, whose non-<code class=highlight><span class=k>const</span></code> <code>data()</code> is C++17 and later only.</li> <li>Default-constructible, copy-constructible, and move-constructible.</li> <li>Member functions <code>size()</code>, <code>empty()</code>, <code>data()</code>, <code>resize()</code>, <code>operator[]</code>, <code>back()</code>, <code>begin()</code>, <code>end()</code>, <code>cbegin()</code>, and <code>cend()</code> with random-access iterators, and <code>insert(pos, first, last)</code>, which the <abbr title="Concise Binary Object Representation">CBOR</abbr> reader uses to join the chunks of an indefinite-length byte string. <code>push_back()</code> is <strong>not</strong> required.</li> <li>Comparison operators: <code>==</code> is used by <a href=../../../api/byte_container_with_subtype/ ><code>byte_container_with_subtype</code></a>, the relational operators by <a href=../../../api/basic_json/operator_le/ ><code>basic_json</code>'s comparison operators</a>.</li> </ul> <h3 id=required-for-individual-functions_1>Required for individual functions<a class=headerlink href=#required-for-individual-functions_1 title="Permanent link">&para;</a></h3> <ul> <li><code>clear()</code>, for <a href=../../../api/basic_json/clear/ ><code>basic_json::clear()</code></a>.</li> </ul> <p><code>max_size()</code>, <code>at()</code>, <code>reserve()</code>, <code>erase()</code>, <code>pop_back()</code>, and <code>emplace_back()</code> are <strong>not</strong> used at all.</p> <p>See <a href=../../../api/basic_json/binary_t/ ><code>binary_t</code></a> for how a non-default <code>BinaryType</code> changes the meaning of assigning such a container to a <code>basic_json</code> value.</p> <div class="admonition tip"> <p class=admonition-title>Reference implementation</p> <p><code>docs/mkdocs/docs/examples/custom_binary_type.hpp</code> wraps a private <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=o>&gt;</span></code> and satisfies every requirement above -- a good starting point for a custom <code>BinaryType</code>.</p> <div class=highlight><pre><span></span><code><span class=cp>#pragma once</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;cstdint&gt;</span>
@@ -678,4 +678,4 @@
<span class=p>}</span>
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>{</span><span class=nt>"bytes"</span><span class=p>:[</span><span class=mi>1</span><span class=p>,</span><span class=mi>2</span><span class=p>,</span><span class=mi>3</span><span class=p>],</span><span class=nt>"subtype"</span><span class=p>:</span><span class=kc>null</span><span class=p>}</span>
<span class=kc>true</span>
</code></pre></div> </details> <h3 id=compatible-containers_2>Compatible containers<a class=headerlink href=#compatible-containers_2 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Notes</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=o>&gt;</span></code> (default)</td> <td></td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=kt>char</span><span class=o>&gt;</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>byte</span><span class=o>&gt;</span></code></td> <td><code>dump()</code> writes the bytes as 0..255 whichever is used</td> </tr> <tr> <td><code>boost::container::vector&lt;std::uint8_t&gt;</code>, <code>boost::container::small_vector&lt;std::uint8_t, N&gt;</code></td> <td></td> </tr> <tr> <td><code>absl::InlinedVector&lt;std::uint8_t, N&gt;</code></td> <td>usable here, unlike as an <code>ArrayType</code>, because the value type is complete</td> </tr> <tr> <td><code>eastl::vector&lt;std::uint8_t&gt;</code></td> <td>usable here, unlike as an <code>ArrayType</code>, because <code>max_size()</code> is not needed</td> </tr> <tr> <td><code>folly::fbvector&lt;std::uint8_t&gt;</code></td> <td>requires C++20, see the note above</td> </tr> </tbody> </table> <h3 id=containers-that-cannot-be-used_2>Containers that cannot be used<a class=headerlink href=#containers-that-cannot-be-used_2 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Reason</th> </tr> </thead> <tbody> <tr> <td><code>QByteArray</code></td> <td>no <code>empty()</code> (it spells that <code>isEmpty()</code>); its <code>insert</code> takes an index rather than an iterator; and it converts to <code>string_t</code>, which makes <code>to_json</code> ambiguous between a string and a binary value</td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code></td> <td><code>binary_t::container_type</code> and <code>string_t</code> would be the same type, so the two <a href=../../../api/basic_json/swap/ ><code>swap</code></a> overloads collide and <code>basic_json</code> cannot be instantiated at all</td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>deque</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=o>&gt;</span></code></td> <td>storage is not contiguous, so there is no <code>data()</code></td> </tr> <tr> <td>containers whose <code>value_type</code> is wider than one byte</td> <td>see above -- accepted by the compiler, wrong at runtime</td> </tr> </tbody> </table> <h2 id=custombaseclass><code>CustomBaseClass</code><a class=headerlink href=#custombaseclass title="Permanent link">&para;</a></h2> <p><code>CustomBaseClass</code> is an extension point: unless it is <code class=highlight><span class=kt>void</span></code> (the default, which selects the empty <code>nlohmann::json_default_base</code>), <code>basic_json</code> publicly derives from it.</p> <h3 id=always-required_9>Always required<a class=headerlink href=#always-required_9 title="Permanent link">&para;</a></h3> <ul> <li>A non-<code>final</code>, default-constructible class type.</li> <li><code>basic_json</code> is copy-/move-constructible and copy-/move-assignable only if <code>CustomBaseClass</code> is.</li> </ul> <h3 id=notes_2>Notes<a class=headerlink href=#notes_2 title="Permanent link">&para;</a></h3> <p><code>basic_json</code> is documented to be a <a href="https://en.cppreference.com/w/cpp/named_req/StandardLayoutType">StandardLayoutType</a>. Because <code>basic_json</code> has non-static data members of its own, a <code>CustomBaseClass</code> with non-static data members forfeits this guarantee.</p> <p>Note the namespace of <code>CustomBaseClass</code> becomes an associated namespace of <code>basic_json</code> for the purpose of argument-dependent lookup.</p> <p>See <a href=../../../api/basic_json/json_base_class_t/ ><code>json_base_class_t</code></a> for an example.</p> <h3 id=compatible-types_6>Compatible types<a class=headerlink href=#compatible-types_6 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Type</th> <th>Support</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=kt>void</span></code> (default)</td> <td>an empty base class is used; no effect on <code>basic_json</code></td> </tr> <tr> <td>any default-constructible, non-<code>final</code> class</td> <td>full; see <a href=../../../api/basic_json/json_base_class_t/ ><code>json_base_class_t</code></a></td> </tr>Line truncated
</code></pre></div> </details> <h3 id=compatible-containers_2>Compatible containers<a class=headerlink href=#compatible-containers_2 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Notes</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=o>&gt;</span></code> (default)</td> <td></td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=kt>char</span><span class=o>&gt;</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>byte</span><span class=o>&gt;</span></code></td> <td><code>dump()</code> writes the bytes as 0..255 whichever is used</td> </tr> <tr> <td><code>boost::container::vector&lt;std::uint8_t&gt;</code>, <code>boost::container::small_vector&lt;std::uint8_t, N&gt;</code></td> <td></td> </tr> <tr> <td><code>absl::InlinedVector&lt;std::uint8_t, N&gt;</code></td> <td>usable here, unlike as an <code>ArrayType</code>, because the value type is complete</td> </tr> <tr> <td><code>eastl::vector&lt;std::uint8_t&gt;</code></td> <td>usable here, unlike as an <code>ArrayType</code>, because <code>max_size()</code> is not needed</td> </tr> <tr> <td><code>folly::fbvector&lt;std::uint8_t&gt;</code></td> <td>requires C++20, see the note above</td> </tr> </tbody> </table> <h3 id=containers-that-cannot-be-used_2>Containers that cannot be used<a class=headerlink href=#containers-that-cannot-be-used_2 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Container</th> <th>Reason</th> </tr> </thead> <tbody> <tr> <td><code>QByteArray</code></td> <td>no <code>empty()</code> (it spells that <code>isEmpty()</code>); its <code>insert</code> takes an index rather than an iterator; and it converts to <code>string_t</code>, which makes <code>to_json</code> ambiguous between a string and a binary value</td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code></td> <td><code>binary_t::container_type</code> and <code>string_t</code> would be the same type, so the two <a href=../../../api/basic_json/swap/ ><code>swap</code></a> overloads collide and <code>basic_json</code> cannot be instantiated at all</td> </tr> <tr> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>deque</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=o>&gt;</span></code></td> <td>storage is not contiguous, so there is no <code>data()</code></td> </tr> <tr> <td>containers whose <code>value_type</code> is wider than one byte</td> <td>see above -- accepted by the compiler, wrong at runtime</td> </tr> </tbody> </table> <h2 id=custombaseclass><code>CustomBaseClass</code><a class=headerlink href=#custombaseclass title="Permanent link">&para;</a></h2> <p><code>CustomBaseClass</code> is an extension point: unless it is <code class=highlight><span class=kt>void</span></code> (the default, which selects the empty <code>nlohmann::json_default_base</code>), <code>basic_json</code> publicly derives from it.</p> <h3 id=always-required_9>Always required<a class=headerlink href=#always-required_9 title="Permanent link">&para;</a></h3> <ul> <li>A non-<code>final</code>, default-constructible class type.</li> <li><code>basic_json</code> is copy-/move-constructible and copy-/move-assignable only if <code>CustomBaseClass</code> is.</li> </ul> <h3 id=notes_2>Notes<a class=headerlink href=#notes_2 title="Permanent link">&para;</a></h3> <p><code>basic_json</code> is documented to be a <a href="https://en.cppreference.com/w/cpp/named_req/StandardLayoutType">StandardLayoutType</a>. Because <code>basic_json</code> has non-static data members of its own, a <code>CustomBaseClass</code> with non-static data members forfeits this guarantee.</p> <p>Note the namespace of <code>CustomBaseClass</code> becomes an associated namespace of <code>basic_json</code> for the purpose of argument-dependent lookup.</p> <p>See <a href=../../../api/basic_json/json_base_class_t/ ><code>json_base_class_t</code></a> for an example.</p> <h3 id=compatible-types_6>Compatible types<a class=headerlink href=#compatible-types_6 title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>Type</th> <th>Support</th> </tr> </thead> <tbody> <tr> <td><code class=highlight><span class=kt>void</span></code> (default)</td> <td>an empty base class is used; no effect on <code>basic_json</code></td> </tr> <tr> <td>any default-constructible, non-<code>final</code> class</td> <td>full; see <a href=../../../api/basic_json/json_base_class_t/ ><code>json_base_class_t</code></a></td> </tr>Line truncated
+6 -6
View File
@@ -15,7 +15,7 @@ Requirements are not checked
Three requirements are checked with a `static_assert`: the array iterator category, the width of [`BinaryType`](#binarytype)'s `value_type`, and [`NumberUnsignedType`](#numberintegertype-and-numberunsignedtype) being at least as wide as [`NumberIntegerType`](#numberintegertype-and-numberunsignedtype). The rest are not diagnosed with dedicated error messages, and violating most of them results in a compiler error somewhere inside the library. Four violations are not caught at compile time at all:
- A [`StringType`](#stringtype) whose `data()` is not null-terminated compiles and can silently misparse floating-point numbers, because the lexer may hand the buffer to `std::strtod`, which reads up to the terminating null character.
- A [`StringType`](#stringtype) whose `data()` is not null-terminated compiles and silently misparses numbers stored as a `long double` that is not IEEE 754 binary64 (e.g., the 80-bit x87 format), because the lexer hands the buffer to `std::strtold`.
- A stateful [`AllocatorType`](#allocatortype) compiles and silently ignores its state: allocation, deallocation, and [`get_allocator()`](https://json.nlohmann.me/api/basic_json/get_allocator/index.md) each use a different default-constructed instance.
- The two [cross-specialization conversions](#cross-specialization-conversions) below. These abort on an assertion in a normal build, and only fail silently under `NDEBUG`.
@@ -608,10 +608,10 @@ true
### Always required
- A member type `value_type` that is one byte wide and `char`-compatible. The library stores and processes UTF-8 encoded `char` data and passes `data()` to functions that take a `const char*`, such as `std::strtod`. `std::wstring`, `std::u16string`, and `std::u32string` are **not** valid choices; see the FAQ on [wide string handling](https://json.nlohmann.me/home/faq/#wide-string-handling).
- A member type `value_type` that is one byte wide and `char`-compatible. The library stores and processes UTF-8 encoded `char` data and passes `data()` to functions that take a `const char*`, such as `std::strtold` (only used to parse a `long double` that is not IEEE 754 binary64, see [`NumberFloatType`](#numberfloattype)). `std::wstring`, `std::u16string`, and `std::u32string` are **not** valid choices; see the FAQ on [wide string handling](https://json.nlohmann.me/home/faq/#wide-string-handling).
- Constructors: default, copy, move, from `const char*` (which must not be `explicit`), from `(const char*, size_type)`, and from `(size_type, char)`; and copy or move assignment.
- Member functions `size()`, `clear()`, `resize(n, c)`, `data()`, `push_back(char)`, and `operator[]` (const and non-const, returning references). `c_str()` and `back()` are **not** required.
- `data()` must return a pointer to a contiguous, **null-terminated** buffer -- the parser may hand it to `std::strtod`, which reads up to the null character. A type whose `data()` is not null-terminated does not fail to compile; it can silently misparse floating-point numbers.
- `data()` must return a pointer to a contiguous, **null-terminated** buffer. `float`, `double`, and a `long double` that is IEEE 754 binary64 are converted by the library itself and do not depend on this. For any other `NumberFloatType` (a `long double` of another format), the parser falls back to `std::strtold` when `std::from_chars` is not available or declines the token, and `std::strtold` reads up to the null character. A type whose `data()` is not null-terminated does not fail to compile; with such a `NumberFloatType` it can silently misparse floating-point numbers.
- `append(const char*, size_type)`, used by [`dump`](https://json.nlohmann.me/api/basic_json/dump/index.md), and `append(const StringType&)`, used by the CBOR reader for indefinite-length strings. The library's internal string concatenation additionally has to append a `char` and a `const char*`; for each it selects between `append(arg)`, `operator+=`, `append(first, last)`, and `append(data, size)`.
- The comparison operator `==` against another `StringType`, and `<` for use as a key of the chosen [`ObjectType`](#objecttype) (with the default comparator, `std::less<>` must be able to compare two `StringType` values, and a `StringType` with the key types used for lookup). `!=` is never applied to a `StringType`, and `==` against `const char*` is resolved by the implicit `const char*` constructor.
@@ -920,10 +920,10 @@ The number types influence what the parser accepts: an integer literal that does
`NumberFloatType` must be one of `float`, `double`, or `long double`:
- The [parser](https://json.nlohmann.me/features/parsing/index.md) converts number literals with `std::from_chars` or, as a fallback, with `std::strtof`, `std::strtod`, or `std::strtold`; the library provides overloads for exactly these three types.
- The [parser](https://json.nlohmann.me/features/parsing/index.md) converts number literals to `float`, `double`, and a `long double` that is IEEE 754 binary64 itself; other `long double` formats are converted with `std::from_chars` where available, or with `std::strtold`. The library provides overloads for exactly these three types.
- [`dump`](https://json.nlohmann.me/api/basic_json/dump/index.md) falls back to `std::snprintf` with the `%g` and `%Lg` conversion specifiers, for which the library likewise provides only `double` and `long double` overloads (`float` is promoted to `double`).
If `std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary32 or binary64 number, `dump` uses the Grisu2 algorithm, which produces the shortest representation that round-trips. Otherwise the `snprintf` fallback with `max_digits10` digits is used.
If `std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary64 number, `dump` uses the algorithm of Żmij, which produces the shortest representation that round-trips. For IEEE 754 binary32 numbers, it uses Grisu2, which produces a short representation that round-trips. Otherwise the `snprintf` fallback with `max_digits10` digits is used.
### Required for the binary formats
@@ -933,7 +933,7 @@ If `std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary32 or bina
| Type | Support |
| ------------------ | --------------------------------------------------------------------------------------------------------------------- |
| `double` (default) | full; short round-trip output through Grisu2 |
| `double` (default) | full; shortest round-trip output through Żmij |
| `float` | full; short round-trip output through Grisu2 |
| `long double` | `dump` and `parse` only; the binary format writers do not compile, as they only handle IEEE 754 binary32 and binary64 |
| any other type | not usable |