mirror of
https://github.com/nlohmann/json.git
synced 2026-10-07 06:57:14 +00:00
deploy: 5379e04ce4
This commit is contained in:
1 parent
41e21ac50a
commit
148be4b157
19 files changed
+73
-15
No files matched your search
@@ -68,6 +68,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
||||
- Throws [type_error.321](../../home/exceptions.md#jsonexceptiontype_error321) if `j` or a value nested in it is
|
||||
discarded; example: `"cannot serialize discarded value to BJData"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -119,4 +121,6 @@ Linear in the size of the JSON value `j`.
|
||||
- BJData version parameter (for draft3 binary encoding) added in version 3.12.0.
|
||||
- Added `error_handler` parameter in version 3.13.0. Its default, `keep`, writes the bytes of a string or object key
|
||||
that is not valid UTF-8 unchanged, as before; `strict` (the default if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`.
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an
|
||||
array or object was silently skipped, producing invalid BJData.
|
||||
@@ -14,7 +14,7 @@
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>use_size</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>,</span><span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>use_type</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>bjdata_version_t</span><span class=w> </span><span class=n>version</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>bjdata_version_t</span><span class=o>::</span><span class=n>draft2</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the <abbr title="Binary JData">BJData</abbr> (Binary JData) serialization format. <abbr title="Binary JData">BJData</abbr> aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the <abbr title="Binary JData">BJData</abbr> serialization.</li> <li>Writes the <abbr title="Binary JData">BJData</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/bjdata/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>use_size</code> (in)</dt> <dd>whether to add size annotations to container types; optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>use_type</code> (in)</dt> <dd>whether to add type annotations to container types (must be combined with <code class=highlight><span class=n>use_size</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</span></code>); optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>version</code> (in)</dt> <dd>which version of <abbr title="Binary JData">BJData</abbr> to use (see note on "Binary values" on <a href=../../../features/binary_formats/bjdata/ ><abbr title="Binary JData">BJData</abbr></a>); optional, <code class=highlight><span class=n>bjdata_version_t</span><span class=o>::</span><span class=n>draft2</span></code> by default.</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_bjdata</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Binary JData">BJData</abbr> serialization as byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionother_error502><code>other_error.502</code></a> if <code>use_type</code> is true and <code>use_size</code> is false, and <code>j</code> contains a non-empty array, object, or binary value.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to <abbr title="Binary JData">BJData</abbr></summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Binary JData">BJData</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the <abbr title="Binary JData">BJData</abbr> (Binary JData) serialization format. <abbr title="Binary JData">BJData</abbr> aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the <abbr title="Binary JData">BJData</abbr> serialization.</li> <li>Writes the <abbr title="Binary JData">BJData</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/bjdata/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>use_size</code> (in)</dt> <dd>whether to add size annotations to container types; optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>use_type</code> (in)</dt> <dd>whether to add type annotations to container types (must be combined with <code class=highlight><span class=n>use_size</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</span></code>); optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>version</code> (in)</dt> <dd>which version of <abbr title="Binary JData">BJData</abbr> to use (see note on "Binary values" on <a href=../../../features/binary_formats/bjdata/ ><abbr title="Binary JData">BJData</abbr></a>); optional, <code class=highlight><span class=n>bjdata_version_t</span><span class=o>::</span><span class=n>draft2</span></code> by default.</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_bjdata</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Binary JData">BJData</abbr> serialization as byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionother_error502><code>other_error.502</code></a> if <code>use_type</code> is true and <code>use_size</code> is false, and <code>j</code> contains a non-empty array, object, or binary value.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error321>type_error.321</a> if <code>j</code> or a value nested in it is discarded; example: <code>"cannot serialize discarded value to BJData"</code></li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to <abbr title="Binary JData">BJData</abbr></summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Binary JData">BJData</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iomanip></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
@@ -103,4 +103,4 @@
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.o</span><span class=kc>t</span><span class=err>her_error.</span><span class=mi>502</span><span class=p>]</span><span class=w> </span><span class=err>use_</span><span class=kc>t</span><span class=err>ype</span><span class=w> </span><span class=err>requires</span><span class=w> </span><span class=err>use_size</span><span class=w> </span><span class=err>=</span><span class=w> </span><span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_bjdata/ >from_bjdata</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Binary JData">BJData</abbr> format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.11.0.</li> <li><abbr title="Binary JData">BJData</abbr> version parameter (for draft3 binary encoding) added in version 3.12.0.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code>.</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 4, 2026 10:13:49 UTC">October 4, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 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-.3Line truncated
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_bjdata/ >from_bjdata</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Binary JData">BJData</abbr> format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.11.0.</li> <li><abbr title="Binary JData">BJData</abbr> version parameter (for draft3 binary encoding) added in version 3.12.0.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code>.</li> <li>Throws <code>type_error.321</code> for a discarded value since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>; previously, a discarded value nested in an array or object was silently skipped, producing invalid <abbr title="Binary JData">BJData</abbr>.</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 6, 2026 05:33:34 UTC">October 6, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8Line truncated
|
||||
@@ -53,6 +53,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
|
||||
- Throws [`other_error.502`](https://json.nlohmann.me/home/exceptions/#jsonexceptionother_error502) if `use_type` is true and `use_size` is false, and `j` contains a non-empty array, object, or binary value.
|
||||
- Throws [type_error.316](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) if a string or object key in `j` is not valid UTF-8 and `error_handler` is `strict` (the default only if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled)
|
||||
- Throws [type_error.321](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error321) if `j` or a value nested in it is discarded; example: `"cannot serialize discarded value to BJData"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -187,3 +188,4 @@ Output:
|
||||
- Added in version 3.11.0.
|
||||
- BJData version parameter (for draft3 binary encoding) added in version 3.12.0.
|
||||
- Added `error_handler` parameter in version 3.13.0 unreleased. Its default, `keep`, writes the bytes of a string or object key that is not valid UTF-8 unchanged, as before; `strict` (the default if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled) throws `type_error.316`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0 unreleased; previously, a discarded value nested in an array or object was silently skipped, producing invalid BJData.
|
||||
@@ -58,6 +58,9 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key is
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
||||
- Throws [type_error.321](../../home/exceptions.md#jsonexceptiontype_error321) if a value nested in `j` is discarded
|
||||
(the top-level value itself is covered by `type_error.317` above, since it must be an object); example:
|
||||
`"cannot serialize discarded value to BSON"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -110,6 +113,8 @@ pass before anything is written.
|
||||
- Throws `out_of_range.412` and `out_of_range.415` since version 3.13.0.
|
||||
- Linear in the size of `j`, and no longer limited by the call stack for deeply nested values, since version 3.13.0.
|
||||
- `out_of_range.415` is now detected before anything is written, like the other exceptions above, since version 3.13.0.
|
||||
- Throws `type_error.321` for a discarded value nested in `j` since version 3.13.0; previously, it was silently
|
||||
skipped, producing a document whose declared size did not match what was actually written.
|
||||
- Added `error_handler` parameter in version 3.13.0. Its default, `keep`, writes the bytes of a string or object key
|
||||
that is not valid UTF-8 unchanged, as before; `strict` (the default if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316` before anything
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
<span class=k>static</span><span class=w> </span><span class=kt>void</span><span class=w> </span><span class=nf>to_bson</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>basic_json</span><span class=o>&</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=n>detail</span><span class=o>::</span><span class=n>output_adapter</span><span class=o><</span><span class=kt>char</span><span class=o>></span><span class=w> </span><span class=n>o</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
</code></pre></div> <p><abbr title="Binary JSON">BSON</abbr> (Binary <abbr title="JavaScript Object Notation">JSON</abbr>) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a so-called document).</p> <ol> <li>Returns a byte vector containing the <abbr title="Binary JSON">BSON</abbr> serialization.</li> <li>Writes the <abbr title="Binary JSON">BSON</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/bson/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_bson</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Binary JSON">BSON</abbr> serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error317><code>type_error.317</code></a> if the top-level type of the <abbr title="JavaScript Object Notation">JSON</abbr> value is not an object; example: <code>"to serialize to BSON, top-level type must be object, but is string"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range409><code>out_of_range.409</code></a> if a key in the <abbr title="JavaScript Object Notation">JSON</abbr> object contains a null byte (code point U+0000); example: <code>"BSON key cannot contain code point U+0000 (at byte 2)"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range412><code>out_of_range.412</code></a> if the length of a document, array, string, or binary value exceeds the range of the 32-bit <abbr title="Binary JSON">BSON</abbr> length field; example: <code>"BSON length 2147483661 exceeds maximum of 2147483647"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range415><code>out_of_range.415</code></a> if the subtype of a binary value exceeds 255, the maximum of the <abbr title="Binary JSON">BSON</abbr> binary subtype; example: <code>"subtype 70000 is too large for the BSON binary subtype (max 255)"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>. The length prefixes of all nested documents and arrays are computed in one pass before anything is written.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to <abbr title="Binary JSON">BSON</abbr></summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Binary JSON">BSON</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
</code></pre></div> <p><abbr title="Binary JSON">BSON</abbr> (Binary <abbr title="JavaScript Object Notation">JSON</abbr>) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a so-called document).</p> <ol> <li>Returns a byte vector containing the <abbr title="Binary JSON">BSON</abbr> serialization.</li> <li>Writes the <abbr title="Binary JSON">BSON</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/bson/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_bson</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Binary JSON">BSON</abbr> serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error317><code>type_error.317</code></a> if the top-level type of the <abbr title="JavaScript Object Notation">JSON</abbr> value is not an object; example: <code>"to serialize to BSON, top-level type must be object, but is string"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range409><code>out_of_range.409</code></a> if a key in the <abbr title="JavaScript Object Notation">JSON</abbr> object contains a null byte (code point U+0000); example: <code>"BSON key cannot contain code point U+0000 (at byte 2)"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range412><code>out_of_range.412</code></a> if the length of a document, array, string, or binary value exceeds the range of the 32-bit <abbr title="Binary JSON">BSON</abbr> length field; example: <code>"BSON length 2147483661 exceeds maximum of 2147483647"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range415><code>out_of_range.415</code></a> if the subtype of a binary value exceeds 255, the maximum of the <abbr title="Binary JSON">BSON</abbr> binary subtype; example: <code>"subtype 70000 is too large for the BSON binary subtype (max 255)"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error321>type_error.321</a> if a value nested in <code>j</code> is discarded (the top-level value itself is covered by <code>type_error.317</code> above, since it must be an object); example: <code>"cannot serialize discarded value to BSON"</code></li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>. The length prefixes of all nested documents and arrays are computed in one pass before anything is written.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to <abbr title="Binary JSON">BSON</abbr></summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Binary JSON">BSON</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><ioLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iomanip></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
@@ -54,4 +54,4 @@
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.ou</span><span class=kc>t</span><span class=err>_o</span><span class=kc>f</span><span class=err>_ra</span><span class=kc>n</span><span class=err>ge.</span><span class=mi>409</span><span class=p>]</span><span class=w> </span><span class=err>BSON</span><span class=w> </span><span class=err>key</span><span class=w> </span><span class=err>ca</span><span class=kc>nn</span><span class=err>o</span><span class=kc>t</span><span class=w> </span><span class=err>co</span><span class=kc>nta</span><span class=err>i</span><span class=kc>n</span><span class=w> </span><span class=err>code</span><span class=w> </span><span class=err>poi</span><span class=kc>nt</span><span class=w> </span><span class=err>U+</span><span class=mi>0000</span><span class=w> </span><span class=err>(a</span><span class=kc>t</span><span class=w> </span><span class=err>by</span><span class=kc>te</span><span class=w> </span><span class=mi>2</span><span class=err>)</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_bson/ >from_bson</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Binary JSON">BSON</abbr> format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.4.0.</li> <li>Throws <code>out_of_range.412</code> and <code>out_of_range.415</code> since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li>Linear in the size of <code>j</code>, and no longer limited by the call stack for deeply nested values, since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li><code>out_of_range.415</code> is now detected before anything is written, like the other exceptions above, since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code> before anything is written.</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 4, 2026 10:13:49 UTC">October 4, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 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-Line truncated
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_bson/ >from_bson</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Binary JSON">BSON</abbr> format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.4.0.</li> <li>Throws <code>out_of_range.412</code> and <code>out_of_range.415</code> since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li>Linear in the size of <code>j</code>, and no longer limited by the call stack for deeply nested values, since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li><code>out_of_range.415</code> is now detected before anything is written, like the other exceptions above, since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li>Throws <code>type_error.321</code> for a discarded value nested in <code>j</code> since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>; previously, it was silently skipped, producing a document whose declared size did not match what was actually written.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code> before anything is written.</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 6, 2026 05:33:34 UTC">October 6, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com LiLine truncated
|
||||
@@ -43,6 +43,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [`out_of_range.412`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range412) if the length of a document, array, string, or binary value exceeds the range of the 32-bit BSON length field; example: `"BSON length 2147483661 exceeds maximum of 2147483647"`
|
||||
- Throws [`out_of_range.415`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range415) if the subtype of a binary value exceeds 255, the maximum of the BSON binary subtype; example: `"subtype 70000 is too large for the BSON binary subtype (max 255)"`
|
||||
- Throws [type_error.316](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) if a string or object key is not valid UTF-8 and `error_handler` is `strict` (the default only if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled)
|
||||
- Throws [type_error.321](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error321) if a value nested in `j` is discarded (the top-level value itself is covered by `type_error.317` above, since it must be an object); example: `"cannot serialize discarded value to BSON"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -136,4 +137,5 @@ Output:
|
||||
- Throws `out_of_range.412` and `out_of_range.415` since version 3.13.0 unreleased.
|
||||
- Linear in the size of `j`, and no longer limited by the call stack for deeply nested values, since version 3.13.0 unreleased.
|
||||
- `out_of_range.415` is now detected before anything is written, like the other exceptions above, since version 3.13.0 unreleased.
|
||||
- Throws `type_error.321` for a discarded value nested in `j` since version 3.13.0 unreleased; previously, it was silently skipped, producing a document whose declared size did not match what was actually written.
|
||||
- Added `error_handler` parameter in version 3.13.0 unreleased. Its default, `keep`, writes the bytes of a string or object key that is not valid UTF-8 unchanged, as before; `strict` (the default if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled) throws `type_error.316` before anything is written.
|
||||
@@ -49,6 +49,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
||||
- Throws [type_error.321](../../home/exceptions.md#jsonexceptiontype_error321) if `j` or a value nested in it is
|
||||
discarded; example: `"cannot serialize discarded value to CBOR"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -86,3 +88,5 @@ Linear in the size of the JSON value `j`.
|
||||
- Added `error_handler` parameter in version 3.13.0. Its default, `keep`, writes the bytes of a string or object key
|
||||
that is not valid UTF-8 unchanged, as before; `strict` (the default if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an
|
||||
array or object was silently skipped, producing invalid CBOR.
|
||||
@@ -7,7 +7,7 @@
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
<span class=k>static</span><span class=w> </span><span class=kt>void</span><span class=w> </span><span class=nf>to_cbor</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>basic_json</span><span class=o>&</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=n>detail</span><span class=o>::</span><span class=n>output_adapter</span><span class=o><</span><span class=kt>char</span><span class=o>></span><span class=w> </span><span class=n>o</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the <abbr title="Concise Binary Object Representation">CBOR</abbr> (Concise Binary Object Representation) serialization format. <abbr title="Concise Binary Object Representation">CBOR</abbr> is a binary serialization format that aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization.</li> <li>Writes the <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/cbor/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_cbor</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Concise Binary Object Representation">CBOR</abbr> serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Concise Binary Object Representation">CBOR</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the <abbr title="Concise Binary Object Representation">CBOR</abbr> (Concise Binary Object Representation) serialization format. <abbr title="Concise Binary Object Representation">CBOR</abbr> is a binary serialization format that aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization.</li> <li>Writes the <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/cbor/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_cbor</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Concise Binary Object Representation">CBOR</abbr> serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error321>type_error.321</a> if <code>j</code> or a value nested in it is discarded; example: <code>"cannot serialize discarded value to CBOR"</code></li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example</summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Concise Binary Object Representation">CBOR</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iomanip></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
@@ -30,4 +30,4 @@
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=mi>0</span><span class=err>xa</span><span class=mi>2</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>67</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>63</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>6</span><span class=kc>f</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>6</span><span class=err>d</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>70</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>61</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>63</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>74</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=kc>f</span><span class=mi>5</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>66</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>73</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>63</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>68</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>65</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>6</span><span class=err>d</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>61</span><span class=w> </span><span class=mi>0</span><span class=err>x</span><span class=mi>00</span><span class=w> </span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_cbor/ >from_cbor</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Concise Binary Object Representation">CBOR</abbr> format</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 2.0.9.</li> <li>Compact representation of floating-point numbers added in version 3.8.0.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code>.</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 4, 2026 10:13:49 UTC">October 4, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 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.1C43Line truncated
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_cbor/ >from_cbor</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Concise Binary Object Representation">CBOR</abbr> format</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 2.0.9.</li> <li>Compact representation of floating-point numbers added in version 3.8.0.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code>.</li> <li>Throws <code>type_error.321</code> for a discarded value since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>; previously, a discarded value nested in an array or object was silently skipped, producing invalid <abbr title="Concise Binary Object Representation">CBOR</abbr>.</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 6, 2026 05:33:34 UTC">October 6, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.Line truncated
|
||||
@@ -39,6 +39,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
## Exceptions
|
||||
|
||||
- Throws [type_error.316](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) if a string or object key in `j` is not valid UTF-8 and `error_handler` is `strict` (the default only if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled)
|
||||
- Throws [type_error.321](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error321) if `j` or a value nested in it is discarded; example: `"cannot serialize discarded value to CBOR"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -95,3 +96,4 @@ Output:
|
||||
- Added in version 2.0.9.
|
||||
- Compact representation of floating-point numbers added in version 3.8.0.
|
||||
- Added `error_handler` parameter in version 3.13.0 unreleased. Its default, `keep`, writes the bytes of a string or object key that is not valid UTF-8 unchanged, as before; `strict` (the default if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled) throws `type_error.316`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0 unreleased; previously, a discarded value nested in an array or object was silently skipped, producing invalid CBOR.
|
||||
@@ -54,6 +54,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
`"subtype 70000 is too large for the MessagePack ext type (max 255)"`
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8 and `error_handler` is `strict`
|
||||
- Throws [type_error.321](../../home/exceptions.md#jsonexceptiontype_error321) if `j` or a value nested in it is
|
||||
discarded; example: `"cannot serialize discarded value to MessagePack"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -108,3 +110,5 @@ Linear in the size of the JSON value `j`.
|
||||
- Fixed in version 3.13.0 to serialize `number_integer_t`/`number_unsigned_t` pairs of different width correctly;
|
||||
before, integers could be serialized with the wrong value if `number_integer_t` was narrower than
|
||||
`number_unsigned_t`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an
|
||||
array or object was silently skipped, producing invalid MessagePack.
|
||||
@@ -7,7 +7,7 @@
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
<span class=k>static</span><span class=w> </span><span class=kt>void</span><span class=w> </span><span class=nf>to_msgpack</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>basic_json</span><span class=o>&</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=n>detail</span><span class=o>::</span><span class=n>output_adapter</span><span class=o><</span><span class=kt>char</span><span class=o>></span><span class=w> </span><span class=n>o</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the MessagePack serialization format. MessagePack is a binary serialization format that aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the MessagePack serialization.</li> <li>Writes the MessagePack serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/messagepack/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_msgpack</code> did before this parameter was added and as the MessagePack specification allows; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. Unlike the other binary writers, the default stays <code>keep</code> even if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li>MessagePack serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range412><code>out_of_range.412</code></a> if the length of a string, binary value, array, or object exceeds 4294967295, the maximum MessagePack can store; example: <code>"MessagePack length 4294967296 exceeds maximum of 4294967295"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range415><code>out_of_range.415</code></a> if the subtype of a binary value exceeds 255, the maximum of the MessagePack ext type; example: <code>"subtype 70000 is too large for the MessagePack ext type (max 255)"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code></li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to MessagePack</summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in MessagePack format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the MessagePack serialization format. MessagePack is a binary serialization format that aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the MessagePack serialization.</li> <li>Writes the MessagePack serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/messagepack/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_msgpack</code> did before this parameter was added and as the MessagePack specification allows; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. Unlike the other binary writers, the default stays <code>keep</code> even if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li>MessagePack serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range412><code>out_of_range.412</code></a> if the length of a string, binary value, array, or object exceeds 4294967295, the maximum MessagePack can store; example: <code>"MessagePack length 4294967296 exceeds maximum of 4294967295"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range415><code>out_of_range.415</code></a> if the subtype of a binary value exceeds 255, the maximum of the MessagePack ext type; example: <code>"subtype 70000 is too large for the MessagePack ext type (max 255)"</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code></li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error321>type_error.321</a> if <code>j</code> or a value nested in it is discarded; example: <code>"cannot serialize discarded value to MessagePack"</code></li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to MessagePack</summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in MessagePack format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iomanip></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
@@ -51,4 +51,4 @@
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.ou</span><span class=kc>t</span><span class=err>_o</span><span class=kc>f</span><span class=err>_ra</span><span class=kc>n</span><span class=err>ge.</span><span class=mi>415</span><span class=p>]</span><span class=w> </span><span class=err>sub</span><span class=kc>t</span><span class=err>ype</span><span class=w> </span><span class=mi>300</span><span class=w> </span><span class=err>is</span><span class=w> </span><span class=kc>t</span><span class=err>oo</span><span class=w> </span><span class=err>large</span><span class=w> </span><span class=kc>f</span><span class=err>or</span><span class=w> </span><span class=kc>t</span><span class=err>he</span><span class=w> </span><span class=err>MessagePack</span><span class=w> </span><span class=err>ex</span><span class=kc>t</span><span class=w> </span><span class=kc>t</span><span class=err>ype</span><span class=w> </span><span class=err>(max</span><span class=w> </span><span class=mi>255</span><span class=err>)</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_msgpack/ >from_msgpack</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in MessagePack format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 2.0.9.</li> <li>Throws <code>out_of_range.412</code> and <code>out_of_range.415</code> since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before.</li> <li>Fixed in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span> to serialize <code>number_integer_t</code>/<code>number_unsigned_t</code> pairs of different width correctly; before, integers could be serialized with the wrong value if <code>number_integer_t</code> was narrower than <code>number_unsigned_t</code>.</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 4, 2026 10:13:49 UTC">October 4, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 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.Line truncated
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_msgpack/ >from_msgpack</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in MessagePack format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_ubjson/ >to_ubjson</a> create a <abbr title="Universal Binary JSON">UBJSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 2.0.9.</li> <li>Throws <code>out_of_range.412</code> and <code>out_of_range.415</code> since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before.</li> <li>Fixed in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span> to serialize <code>number_integer_t</code>/<code>number_unsigned_t</code> pairs of different width correctly; before, integers could be serialized with the wrong value if <code>number_integer_t</code> was narrower than <code>number_unsigned_t</code>.</li> <li>Throws <code>type_error.321</code> for a discarded value since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>; previously, a discarded value nested in an array or object was silently skipped, producing invalid MessagePack.</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 6, 2026 05:33:34 UTC">October 6, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2Line truncated
|
||||
@@ -41,6 +41,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [`out_of_range.412`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range412) if the length of a string, binary value, array, or object exceeds 4294967295, the maximum MessagePack can store; example: `"MessagePack length 4294967296 exceeds maximum of 4294967295"`
|
||||
- Throws [`out_of_range.415`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range415) if the subtype of a binary value exceeds 255, the maximum of the MessagePack ext type; example: `"subtype 70000 is too large for the MessagePack ext type (max 255)"`
|
||||
- Throws [type_error.316](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) if a string or object key in `j` is not valid UTF-8 and `error_handler` is `strict`
|
||||
- Throws [type_error.321](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error321) if `j` or a value nested in it is discarded; example: `"cannot serialize discarded value to MessagePack"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -131,3 +132,4 @@ Output:
|
||||
- Throws `out_of_range.412` and `out_of_range.415` since version 3.13.0 unreleased.
|
||||
- Added `error_handler` parameter in version 3.13.0 unreleased. Its default, `keep`, writes the bytes of a string or object key that is not valid UTF-8 unchanged, as before.
|
||||
- Fixed in version 3.13.0 unreleased to serialize `number_integer_t`/`number_unsigned_t` pairs of different width correctly; before, integers could be serialized with the wrong value if `number_integer_t` was narrower than `number_unsigned_t`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0 unreleased; previously, a discarded value nested in an array or object was silently skipped, producing invalid MessagePack.
|
||||
@@ -61,6 +61,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
||||
- Throws [type_error.321](../../home/exceptions.md#jsonexceptiontype_error321) if `j` or a value nested in it is
|
||||
discarded; example: `"cannot serialize discarded value to UBJSON"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -112,3 +114,5 @@ Linear in the size of the JSON value `j`.
|
||||
- Added `error_handler` parameter in version 3.13.0. Its default, `keep`, writes the bytes of a string or object key
|
||||
that is not valid UTF-8 unchanged, as before; `strict` (the default if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an
|
||||
array or object was silently skipped, producing invalid UBJSON.
|
||||
@@ -11,7 +11,7 @@
|
||||
<span class=k>static</span><span class=w> </span><span class=kt>void</span><span class=w> </span><span class=nf>to_ubjson</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>basic_json</span><span class=o>&</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=n>detail</span><span class=o>::</span><span class=n>output_adapter</span><span class=o><</span><span class=kt>char</span><span class=o>></span><span class=w> </span><span class=n>o</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>use_size</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>,</span><span class=w> </span><span class=k>const</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=n>use_type</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>error_handler_t</span><span class=w> </span><span class=n>error_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>error_handler_t</span><span class=o>::</span><span class=n>keep</span><span class=p>);</span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the <abbr title="Universal Binary JSON">UBJSON</abbr> (Universal Binary <abbr title="JavaScript Object Notation">JSON</abbr>) serialization format. <abbr title="Universal Binary JSON">UBJSON</abbr> aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the <abbr title="Universal Binary JSON">UBJSON</abbr> serialization.</li> <li>Writes the <abbr title="Universal Binary JSON">UBJSON</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/ubjson/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>use_size</code> (in)</dt> <dd>whether to add size annotations to container types; optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>use_type</code> (in)</dt> <dd>whether to add type annotations to container types (must be combined with <code class=highlight><span class=n>use_size</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</span></code>); optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_ubjson</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Universal Binary JSON">UBJSON</abbr> serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionother_error502><code>other_error.502</code></a> if <code>use_type</code> is true and <code>use_size</code> is false, and <code>j</code> contains a non-empty array, object, or binary value.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to <abbr title="Universal Binary JSON">UBJSON</abbr></summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Universal Binary JSON">UBJSON</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
</code></pre></div> <p>Serializes a given <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code> to a byte vector using the <abbr title="Universal Binary JSON">UBJSON</abbr> (Universal Binary <abbr title="JavaScript Object Notation">JSON</abbr>) serialization format. <abbr title="Universal Binary JSON">UBJSON</abbr> aims to be more compact than <abbr title="JavaScript Object Notation">JSON</abbr> itself, yet more efficient to parse.</p> <ol> <li>Returns a byte vector containing the <abbr title="Universal Binary JSON">UBJSON</abbr> serialization.</li> <li>Writes the <abbr title="Universal Binary JSON">UBJSON</abbr> serialization to an output adapter.</li> </ol> <p>The exact mapping and its limitations are described on a <a href=../../../features/binary_formats/ubjson/ >dedicated page</a>.</p> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>j</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to serialize</dd> <dt><code>o</code> (in)</dt> <dd>output adapter to write serialization to</dd> <dt><code>use_size</code> (in)</dt> <dd>whether to add size annotations to container types; optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>use_type</code> (in)</dt> <dd>whether to add type annotations to container types (must be combined with <code class=highlight><span class=n>use_size</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</span></code>); optional, <code class=highlight><span class=nb>false</span></code> by default.</dd> <dt><code>error_handler</code> (in)</dt> <dd>how to treat a string or object key in <code>j</code> that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8; see <a href=../error_handler_t/ ><code>error_handler_t</code></a>. The default, <code>keep</code>, writes the ill-formed bytes to the output as is, as every version of <code>to_ubjson</code> did before this parameter was added; <code>strict</code> throws; <code>replace</code>/<code>ignore</code> sanitize it the same way <a href=../dump/ ><code>dump</code></a> would. If <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled, the default is <code>strict</code> instead.</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <ol> <li><abbr title="Universal Binary JSON">UBJSON</abbr> serialization as a byte vector</li> <li>(none)</li> </ol> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: if an exception is thrown, there are no changes in the <abbr title="JavaScript Object Notation">JSON</abbr> value.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <ul> <li>Throws <a href=../../../home/exceptions/#jsonexceptionother_error502><code>other_error.502</code></a> if <code>use_type</code> is true and <code>use_size</code> is false, and <code>j</code> contains a non-empty array, object, or binary value.</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error316>type_error.316</a> if a string or object key in <code>j</code> is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and <code>error_handler</code> is <code>strict</code> (the default only if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled)</li> <li>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error321>type_error.321</a> if <code>j</code> or a value nested in it is discarded; example: <code>"cannot serialize discarded value to UBJSON"</code></li> </ul> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the size of the <abbr title="JavaScript Object Notation">JSON</abbr> value <code>j</code>.</p> <h2 id=examples>Examples<a class=headerlink href=#examples title="Permanent link">¶</a></h2> <details class=example> <summary>Example: serialize a <abbr title="JavaScript Object Notation">JSON</abbr> value to <abbr title="Universal Binary JSON">UBJSON</abbr></summary> <p>The example shows the serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value to a byte vector in <abbr title="Universal Binary JSON">UBJSON</abbr> format.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iomanip></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
@@ -100,4 +100,4 @@
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>[</span><span class=err>jso</span><span class=kc>n</span><span class=err>.excep</span><span class=kc>t</span><span class=err>io</span><span class=kc>n</span><span class=err>.o</span><span class=kc>t</span><span class=err>her_error.</span><span class=mi>502</span><span class=p>]</span><span class=w> </span><span class=err>use_</span><span class=kc>t</span><span class=err>ype</span><span class=w> </span><span class=err>requires</span><span class=w> </span><span class=err>use_size</span><span class=w> </span><span class=err>=</span><span class=w> </span><span class=kc>true</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_ubjson/ >from_ubjson</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Universal Binary JSON">UBJSON</abbr> format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.1.0.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code>.</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 4, 2026 10:13:49 UTC">October 4, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 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 5Line truncated
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../from_ubjson/ >from_ubjson</a> create a <abbr title="JavaScript Object Notation">JSON</abbr> value from an input in <abbr title="Universal Binary JSON">UBJSON</abbr> format</li> <li><a href=../to_cbor/ >to_cbor</a> create a <abbr title="Concise Binary Object Representation">CBOR</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_msgpack/ >to_msgpack</a> create a MessagePack serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bson/ >to_bson</a> create a <abbr title="Binary JSON">BSON</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bjdata/ >to_bjdata</a> create a <abbr title="Binary JData">BJData</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> <li><a href=../to_bon8/ >to_bon8</a> create a <abbr title="Binary Object Notation 8">BON8</abbr> serialization of a <abbr title="JavaScript Object Notation">JSON</abbr> value</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.1.0.</li> <li>Added <code>error_handler</code> parameter in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Its default, <code>keep</code>, writes the bytes of a string or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 unchanged, as before; <code>strict</code> (the default if <a href=../../macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled) throws <code>type_error.316</code>.</li> <li>Throws <code>type_error.321</code> for a discarded value since version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>; previously, a discarded value nested in an array or object was silently skipped, producing invalid <abbr title="Universal Binary JSON">UBJSON</abbr>.</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 6, 2026 05:33:34 UTC">October 6, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2Line truncated
|
||||
@@ -48,6 +48,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
|
||||
- Throws [`other_error.502`](https://json.nlohmann.me/home/exceptions/#jsonexceptionother_error502) if `use_type` is true and `use_size` is false, and `j` contains a non-empty array, object, or binary value.
|
||||
- Throws [type_error.316](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) if a string or object key in `j` is not valid UTF-8 and `error_handler` is `strict` (the default only if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled)
|
||||
- Throws [type_error.321](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error321) if `j` or a value nested in it is discarded; example: `"cannot serialize discarded value to UBJSON"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -181,3 +182,4 @@ Output:
|
||||
|
||||
- Added in version 3.1.0.
|
||||
- Added `error_handler` parameter in version 3.13.0 unreleased. Its default, `keep`, writes the bytes of a string or object key that is not valid UTF-8 unchanged, as before; `strict` (the default if [`JSON_STRICT_BINARY_UTF8`](https://json.nlohmann.me/api/macros/json_strict_binary_utf8/index.md) is enabled) throws `type_error.316`.
|
||||
- Throws `type_error.321` for a discarded value since version 3.13.0 unreleased; previously, a discarded value nested in an array or object was silently skipped, producing invalid UBJSON.
|
||||
Reference in new issue
Block a user