This commit is contained in:
nlohmann committed 2026-10-06 15:51:51 +00:00
1 parent 41e21ac50a
commit 148be4b157
19 files changed
+73 -15

No files matched your search

+2 -2
View File
@@ -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>&amp;</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>&lt;</span><span class=kt>char</span><span class=o>&gt;</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">&para;</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">&para;</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">&para;</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">&para;</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">&para;</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">&para;</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>&lt;iostream&gt;</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">&para;</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">&para;</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">&para;</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">&para;</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">&para;</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">&para;</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>&lt;ioLine truncated
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iomanip&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</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">&para;</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">&para;</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 &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-Line truncated
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../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">&para;</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 &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com LiLine truncated
+2
View File
@@ -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.