This commit is contained in:
nlohmann committed 2026-10-07 06:52:50 +00:00
1 parent 42b1d75fff
commit 6cba066a3f
9 files changed
+307 -293

No files matched your search

+6 -4
View File
@@ -5,7 +5,8 @@
<span class=w> </span><span class=n>input_format_t</span><span class=w> </span><span class=n>format</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>input_format_t</span><span class=o>::</span><span class=n>json</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>strict</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</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>ignore_comments</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>ignore_trailing_commas</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>ignore_trailing_commas</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>cbor_tag_handler_t</span><span class=w> </span><span class=n>tag_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>cbor_tag_handler_t</span><span class=o>::</span><span class=n>error</span><span class=p>);</span>
<span class=c1>// (2)</span>
<span class=k>template</span><span class=o>&lt;</span><span class=k>class</span><span class=w> </span><span class=nc>IteratorType</span><span class=p>,</span><span class=w> </span><span class=k>class</span><span class=w> </span><span class=nc>SAX</span><span class=p>,</span><span class=w> </span><span class=k>class</span><span class=w> </span><span class=nc>SentinelType</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>IteratorType</span><span class=o>&gt;</span>
@@ -14,8 +15,9 @@
<span class=w> </span><span class=n>input_format_t</span><span class=w> </span><span class=n>format</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>input_format_t</span><span class=o>::</span><span class=n>json</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>strict</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>true</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>ignore_comments</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>ignore_trailing_commas</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=nb>false</span><span class=p>);</span>
</code></pre></div> <p>Read from input and generate <abbr title="Simple API for XML">SAX</abbr> events</p> <ol> <li>Read from a compatible input.</li> <li> <p>Read from a pair of character iterators, or an iterator and a sentinel of a different type (C++20 ranges support)</p> <p>The value_type of the iterator must be an integral type with a size of 1, 2, or 4 bytes, which will be interpreted respectively as <abbr title="Unicode Transformation Format">UTF</abbr>-8, <abbr title="Unicode Transformation Format">UTF</abbr>-16, and <abbr title="Unicode Transformation Format">UTF</abbr>-32. If <code>SentinelType</code> differs from <code>IteratorType</code>, it must be comparable to the iterator type with <code>operator!=</code>.</p> </li> </ol> <p>The <abbr title="Simple API for XML">SAX</abbr> event lister must follow the interface of <a href=../../json_sax/ ><code>json_sax</code></a>.</p> <h2 id=template-parameters>Template parameters<a class=headerlink href=#template-parameters title="Permanent link">&para;</a></h2> <dl> <dt><code>InputType</code></dt> <dd> <p>A compatible input, for instance:</p> <ul> <li>an <code>std::istream</code> object</li> <li>a <code>FILE</code> pointer</li> <li>a C-style array of characters</li> <li>a pointer to a null-terminated string of single byte characters</li> <li>a container <code>obj</code> for which <code>begin(obj)</code> and <code>end(obj)</code> produce a valid pair of iterators (as found via <abbr title="Argument-dependent lookup">ADL</abbr> or member functions, with semantics compatible to <code>std::begin</code> and <code>std::end</code>)</li> </ul> </dd> <dt><code>IteratorType</code></dt> <dd>a compatible iterator type for overload (2); a pair of character iterators whose <code>value_type</code> is an integral type with a size of 1, 2, or 4 bytes (interpreted respectively as <abbr title="Unicode Transformation Format">UTF</abbr>-8, <abbr title="Unicode Transformation Format">UTF</abbr>-16, and <abbr title="Unicode Transformation Format">UTF</abbr>-32)</dd> <dt><code>SentinelType</code></dt> <dd> <p>defaults to <code>IteratorType</code>; may be a different type comparable to <code>IteratorType</code> via <code>operator!=</code>, for overload (2), for instance.</p> <ul> <li>a custom sentinel type for C++20 ranges</li> <li><code>std::default_sentinel_t</code>, when <code>IteratorType</code> is <code>std::counted_iterator</code></li> </ul> </dd> <dt><code>SAX</code></dt> <dd>a class fulfilling the <abbr title="Simple API for XML">SAX</abbr> event listener interface; see <a href=../../json_sax/ ><code>json_sax</code></a></dd> </dl> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">&para;</a></h2> <dl> <dt><code>i</code> (in)</dt> <dd>Input to parse from</dd> <dt><code>sax</code> (in)</dt> <dd><abbr title="Simple API for XML">SAX</abbr> event listener (must not be null)</dd> <dt><code>format</code> (in)</dt> <dd>the format to parse (<abbr title="JavaScript Object Notation">JSON</abbr>, <abbr title="Binary JData">BJData</abbr>, <abbr title="Binary Object Notation 8">BON8</abbr>, <abbr title="Binary JSON">BSON</abbr>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, or <abbr title="Universal Binary JSON">UBJSON</abbr>) (optional, <code>input_format_t::json</code> by default), see <a href=../input_format_t/ ><code>input_format_t</code></a> for more information</dd> <dt><code>strict</code> (in)</dt> <dd>whether the input has to be consumed completely (optional, <code class=highlight><span class=nb>true</span></code> by default); when <code class=highlight><span class=nb>false</span></code> and the input is a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>istream</span></code>, the character that terminates a number is consumed unless <a href=../../macros/json_precise_stream_position/ ><code>JSON_PRECISE_STREAM_POSITION</code></a> is defined to <code>1</code>; see <a href=../../operator_gtgt/#notes><code>operator&gt;&gt;</code></a></dd> <dt><code>ignore_comments</code> (in)</dt> <dd>whether comments should be ignored and treated like whitespace (<code class=highlight><span class=nb>true</span></code>) or yield a parse error (<code class=highlight><span class=nb>false</span></code>); (optional, <code class=highlight><span class=nb>false</span></code> by default)</dd> <dt><code>ignore_trailing_commas</code> (in)</dt> <dd>whether trailing commas in arrays or objects should be ignored and treated like whitespace (<code class=highlight><span class=nb>true</span></code>) or yield a parse error (<code class=highlight><span class=nb>false</span></code>); (optional, <code class=highlight><span class=nb>false</span></code> by default)</dd> <dt><code>first</code> (in)</dt> <dd>iterator to the start of a character range</dd> <dt><code>last</code> (in)</dt> <dd>iterator to the end of a character range, or a sentinel value that compares equal to the end iterator with <code>operator!=</code><Line truncated
<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>ignore_trailing_commas</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>cbor_tag_handler_t</span><span class=w> </span><span class=n>tag_handler</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>cbor_tag_handler_t</span><span class=o>::</span><span class=n>error</span><span class=p>);</span>
</code></pre></div> <p>Read from input and generate <abbr title="Simple API for XML">SAX</abbr> events</p> <ol> <li>Read from a compatible input.</li> <li> <p>Read from a pair of character iterators, or an iterator and a sentinel of a different type (C++20 ranges support).</p> <p>The value_type of the iterator must be an integral type with a size of 1, 2, or 4 bytes, which will be interpreted respectively as <abbr title="Unicode Transformation Format">UTF</abbr>-8, <abbr title="Unicode Transformation Format">UTF</abbr>-16, and <abbr title="Unicode Transformation Format">UTF</abbr>-32. If <code>SentinelType</code> differs from <code>IteratorType</code>, it must be comparable to the iterator type with <code>operator!=</code>.</p> </li> </ol> <p>The <abbr title="Simple API for XML">SAX</abbr> event lister must follow the interface of <a href=../../json_sax/ ><code>json_sax</code></a>.</p> <h2 id=template-parameters>Template parameters<a class=headerlink href=#template-parameters title="Permanent link">&para;</a></h2> <dl> <dt><code>InputType</code></dt> <dd> <p>A compatible input, for instance:</p> <ul> <li>an <code>std::istream</code> object</li> <li>a <code>FILE</code> pointer</li> <li>a C-style array of characters</li> <li>a pointer to a null-terminated string of single byte characters</li> <li>a container <code>obj</code> for which <code>begin(obj)</code> and <code>end(obj)</code> produce a valid pair of iterators (as found via <abbr title="Argument-dependent lookup">ADL</abbr> or member functions, with semantics compatible to <code>std::begin</code> and <code>std::end</code>)</li> </ul> </dd> <dt><code>IteratorType</code></dt> <dd>a compatible iterator type for overload (2); a pair of character iterators whose <code>value_type</code> is an integral type with a size of 1, 2, or 4 bytes (interpreted respectively as <abbr title="Unicode Transformation Format">UTF</abbr>-8, <abbr title="Unicode Transformation Format">UTF</abbr>-16, and <abbr title="Unicode Transformation Format">UTF</abbr>-32)</dd> <dt><code>SentinelType</code></dt> <dd> <p>defaults to <code>IteratorType</code>; may be a different type comparable to <code>IteratorType</code> via <code>operator!=</code>, for overload (2), for instance.</p> <ul> <li>a custom sentinel type for C++20 ranges</li> <li><code>std::default_sentinel_t</code>, when <code>IteratorType</code> is <code>std::counted_iterator</code></li> </ul> </dd> <dt><code>SAX</code></dt> <dd>a class fulfilling the <abbr title="Simple API for XML">SAX</abbr> event listener interface; see <a href=../../json_sax/ ><code>json_sax</code></a></dd> </dl> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">&para;</a></h2> <dl> <dt><code>i</code> (in)</dt> <dd>Input to parse from</dd> <dt><code>sax</code> (in)</dt> <dd><abbr title="Simple API for XML">SAX</abbr> event listener (must not be null)</dd> <dt><code>format</code> (in)</dt> <dd>the format to parse (<abbr title="JavaScript Object Notation">JSON</abbr>, <abbr title="Binary JData">BJData</abbr>, <abbr title="Binary Object Notation 8">BON8</abbr>, <abbr title="Binary JSON">BSON</abbr>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, or <abbr title="Universal Binary JSON">UBJSON</abbr>) (optional, <code>input_format_t::json</code> by default), see <a href=../input_format_t/ ><code>input_format_t</code></a> for more information</dd> <dt><code>strict</code> (in)</dt> <dd>whether the input has to be consumed completely (optional, <code class=highlight><span class=nb>true</span></code> by default); when <code class=highlight><span class=nb>false</span></code> and the input is a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>istream</span></code>, the character that terminates a number is consumed unless <a href=../../macros/json_precise_stream_position/ ><code>JSON_PRECISE_STREAM_POSITION</code></a> is defined to <code>1</code>; see <a href=../../operator_gtgt/#notes><code>operator&gt;&gt;</code></a></dd> <dt><code>ignore_comments</code> (in)</dt> <dd>whether comments should be ignored and treated like whitespace (<code class=highlight><span class=nb>true</span></code>) or yield a parse error (<code class=highlight><span class=nb>false</span></code>); (optional, <code class=highlight><span class=nb>false</span></code> by default)</dd> <dt><code>ignore_trailing_commas</code> (in)</dt> <dd>whether trailing commas in arrays or objects should be ignored and treated like whitespace (<code class=highlight><span class=nb>true</span></code>) or yield a parse error (<code class=highlight><span class=nb>false</span></code>); (optional, <code class=highlight><span class=nb>false</span></code> by default)</dd> <dt><code>tag_handler</code> (in)</dt> <dd>how to handle <abbr title="Concise Binary Object Representation">CBOR</abbr> tags; see <a href=../cbor_tag_handler_t/ ><code>cbor_tag_handler_t</code></a>. Ignored for formats other than <abbr title="Concise Line 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;sstream&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
@@ -183,4 +185,4 @@
<span class=w> </span><span class=err>ex=</span><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>.parse_error.</span><span class=mi>101</span><span class=p>]</span><span class=w> </span><span class=err>parse</span><span class=w> </span><span class=err>error</span><span class=w> </span><span class=err>a</span><span class=kc>t</span><span class=w> </span><span class=err>li</span><span class=kc>ne</span><span class=w> </span><span class=mi>17</span><span class=p>,</span><span class=w> </span><span class=err>colum</span><span class=kc>n</span><span class=w> </span><span class=mi>6</span><span class=p>:</span><span class=w> </span><span class=err>sy</span><span class=kc>nta</span><span class=err>x</span><span class=w> </span><span class=err>error</span><span class=w> </span><span class=err>while</span><span class=w> </span><span class=err>parsi</span><span class=kc>n</span><span class=err>g</span><span class=w> </span><span class=err>value</span><span class=w> </span><span class=mi>-</span><span class=w> </span><span class=err>u</span><span class=kc>ne</span><span class=err>xpec</span><span class=kc>te</span><span class=err>d</span><span class=w> </span><span class=err>'</span><span class=p>]</span><span class=err>';</span><span class=w> </span><span class=err>expec</span><span class=kc>te</span><span class=err>d</span><span class=w> </span><span class=err>e</span><span class=kc>n</span><span class=err>d</span><span class=w> </span><span class=err>o</span><span class=kc>f</span><span class=w> </span><span class=err>i</span><span class=kc>n</span><span class=err>pu</span><span class=kc>t</span><span class=err>)</span>
<span class=err>resul</span><span class=kc>t</span><span class=p>:</span><span class=w> </span><span class=kc>false</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=../parse/ >parse</a> - deserialize from a compatible input</li> <li><a href=../accept/ >accept</a> - check if the input is valid <abbr title="JavaScript Object Notation">JSON</abbr></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.2.0.</li> <li>Ignoring comments via <code>ignore_comments</code> added in version 3.9.0.</li> <li>Added <code>ignore_trailing_commas</code> 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>.</li> <li>Extended container support (1) to include types with lvalue-only <abbr title="Argument-dependent lookup">ADL</abbr> <code>begin</code>/<code>end</code> (matching <code>std::begin</code>/<code>std::end</code> semantics) 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>.</li> <li>Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) 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>.</li> <li><code>JSON_PRECISE_STREAM_POSITION</code> added 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 optionally leave a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>istream</span></code> positioned right after the parsed value when <code>strict</code> is <code class=highlight><span class=nb>false</span></code>.</li> </ul> <div class="admonition warning"> <p class=admonition-title>Deprecation</p> <p>Overload (2) replaces calls to <code>sax_parse</code> with a pair of iterators as their first parameter which has been deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like <code class=highlight><span class=n>sax_parse</span><span class=p>({</span><span class=n>ptr</span><span class=p>,</span><span class=w> </span><span class=n>ptr</span><span class=o>+</span><span class=n>len</span><span class=p>});</span></code> with <code class=highlight><span class=n>sax_parse</span><span class=p>(</span><span class=n>ptr</span><span class=p>,</span><span class=w> </span><span class=n>ptr</span><span class=o>+</span><span class=n>len</span><span class=p>);</span></code>.</p> <p>See the <a href=../../../integration/migration_guide/#parsing>migration guide</a> for how to update existing code.</p> </div> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 2, 2026 09:32:15 UTC">October 2, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.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=../parse/ >parse</a> - deserialize from a compatible input</li> <li><a href=../accept/ >accept</a> - check if the input is valid <abbr title="JavaScript Object Notation">JSON</abbr></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.2.0.</li> <li>Ignoring comments via <code>ignore_comments</code> added in version 3.9.0.</li> <li>Added <code>ignore_trailing_commas</code> 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>.</li> <li>Added <code>tag_handler</code> 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>.</li> <li>Extended container support (1) to include types with lvalue-only <abbr title="Argument-dependent lookup">ADL</abbr> <code>begin</code>/<code>end</code> (matching <code>std::begin</code>/<code>std::end</code> semantics) 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>.</li> <li>Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) 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>.</li> <li><code>JSON_PRECISE_STREAM_POSITION</code> added 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 optionally leave a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>istream</span></code> positioned right after the parsed value when <code>strict</code> is <code class=highlight><span class=nb>false</span></code>.</li> </ul> <div class="admonition warning"> <p class=admonition-title>Deprecation</p> <p>Overload (2) replaces calls to <code>sax_parse</code> with a pair of iterators as their first parameter which has been deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like <code class=highlight><span class=n>sax_parse</span><span class=p>({</span><span class=n>ptr</span><span class=p>,</span><span class=w> </span><span class=n>ptr</span><span class=o>+</span><span class=n>len</span><span class=p>});</span></code> with <code class=highlight><span class=n>sax_parse</span><span class=p>(</span><span class=n>ptr</span><span class=p>,</span><span class=w> </span><span class=n>ptr</span><span class=o>+</span><span class=n>len</span><span class=p>);</span></code>.</p> <p>See the <a href=../../../integration/migration_guide/#parsing>migration guide</a> for how to update existing code.</p> </div> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 7, 2026 05:38:24 UTC">October 7, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0Line truncated
+8 -3
View File
@@ -8,7 +8,8 @@ static bool sax_parse(InputType&& i,
input_format_t format = input_format_t::json,
const bool strict = true,
const bool ignore_comments = false,
const bool ignore_trailing_commas = false);
const bool ignore_trailing_commas = false,
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error);
// (2)
template<class IteratorType, class SAX, class SentinelType = IteratorType>
@@ -17,14 +18,15 @@ static bool sax_parse(IteratorType first, SentinelType last,
input_format_t format = input_format_t::json,
const bool strict = true,
const bool ignore_comments = false,
const bool ignore_trailing_commas = false);
const bool ignore_trailing_commas = false,
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error);
```
Read from input and generate SAX events
1. Read from a compatible input.
1. Read from a pair of character iterators, or an iterator and a sentinel of a different type (C++20 ranges support)
1. Read from a pair of character iterators, or an iterator and a sentinel of a different type (C++20 ranges support).
The value_type of the iterator must be an integral type with a size of 1, 2, or 4 bytes, which will be interpreted respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to the iterator type with `operator!=`.
@@ -68,6 +70,8 @@ The SAX event lister must follow the interface of [`json_sax`](https://json.nloh
`ignore_trailing_commas` (in) : whether trailing commas in arrays or objects should be ignored and treated like whitespace (`true`) or yield a parse error (`false`); (optional, `false` by default)
`tag_handler` (in) : how to handle CBOR tags; see [`cbor_tag_handler_t`](https://json.nlohmann.me/api/basic_json/cbor_tag_handler_t/index.md). Ignored for formats other than CBOR (optional, `cbor_tag_handler_t::error` by default).
`first` (in) : iterator to the start of a character range
`last` (in) : iterator to the end of a character range, or a sentinel value that compares equal to the end iterator with `operator!=`
@@ -284,6 +288,7 @@ result: false
- Added in version 3.2.0.
- Ignoring comments via `ignore_comments` added in version 3.9.0.
- Added `ignore_trailing_commas` in version 3.13.0 unreleased.
- Added `tag_handler` in version 3.13.0 unreleased.
- Extended container support (1) to include types with lvalue-only ADL `begin`/`end` (matching `std::begin`/`std::end` semantics) in version 3.13.0 unreleased.
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0 unreleased.
- `JSON_PRECISE_STREAM_POSITION` added in version 3.13.0 unreleased to optionally leave a `std::istream` positioned right after the parsed value when `strict` is `false`.