This commit is contained in:
nlohmann committed 2026-07-10 14:08:26 +00:00
1 parent e86d443881
commit 7bc7ca0e06
301 files changed
+1230 -510

No files matched your search

+24 -3
View File
@@ -1,4 +1,4 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/parsing/parser_callbacks/" rel="canonical"><link href=../parse_exceptions/ rel=prev><link href=../sax_interface/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.6"><title>Parser Callbacks - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.484c7ddc.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#parser-callbacks class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Parser Callbacks </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14Line truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/parsing/parser_callbacks/" rel="canonical"><link href=../parse_exceptions/ rel=prev><link href=../sax_interface/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.6"><title>Parser Callbacks - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.484c7ddc.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../../css/custom.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#parser-callbacks class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Parser Callbacks </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14Line truncated
<span class=k>using</span><span class=w> </span><span class=n>parser_callback_t</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>function</span><span class=o>&lt;</span><span class=kt>bool</span><span class=p>(</span><span class=kt>int</span><span class=w> </span><span class=n>depth</span><span class=p>,</span><span class=w> </span><span class=n>parse_event_t</span><span class=w> </span><span class=n>event</span><span class=p>,</span><span class=w> </span><span class=n>BasicJsonType</span><span class=o>&amp;</span><span class=w> </span><span class=n>parsed</span><span class=p>)</span><span class=o>&gt;</span><span class=p>;</span>
</code></pre></div> <h2 id=callback-event-types>Callback event types<a class=headerlink href=#callback-event-types title="Permanent link">&para;</a></h2> <p>We distinguish six scenarios (determined by the event type) in which the callback function can be called. The following table describes the values of the parameters <code>depth</code>, <code>event</code>, and <code>parsed</code>.</p> <table> <thead> <tr> <th>parameter <code>event</code></th> <th>description</th> <th>parameter <code>depth</code></th> <th>parameter <code>parsed</code></th> </tr> </thead> <tbody> <tr> <td><code>parse_event_t::object_start</code></td> <td>the parser read <code>{</code> and started to process a <abbr title="JavaScript Object Notation">JSON</abbr> object</td> <td>depth of the parent of the <abbr title="JavaScript Object Notation">JSON</abbr> object</td> <td>a <abbr title="JavaScript Object Notation">JSON</abbr> value with type discarded</td> </tr> <tr> <td><code>parse_event_t::key</code></td> <td>the parser read a key of a value in an object</td> <td>depth of the currently parsed <abbr title="JavaScript Object Notation">JSON</abbr> object</td> <td>a <abbr title="JavaScript Object Notation">JSON</abbr> string containing the key</td> </tr> <tr> <td><code>parse_event_t::object_end</code></td> <td>the parser read <code>}</code> and finished processing a <abbr title="JavaScript Object Notation">JSON</abbr> object</td> <td>depth of the parent of the <abbr title="JavaScript Object Notation">JSON</abbr> object</td> <td>the parsed <abbr title="JavaScript Object Notation">JSON</abbr> object</td> </tr> <tr> <td><code>parse_event_t::array_start</code></td> <td>the parser read <code>[</code> and started to process a <abbr title="JavaScript Object Notation">JSON</abbr> array</td> <td>depth of the parent of the <abbr title="JavaScript Object Notation">JSON</abbr> array</td> <td>a <abbr title="JavaScript Object Notation">JSON</abbr> value with type discarded</td> </tr> <tr> <td><code>parse_event_t::array_end</code></td> <td>the parser read <code>]</code> and finished processing a <abbr title="JavaScript Object Notation">JSON</abbr> array</td> <td>depth of the parent of the <abbr title="JavaScript Object Notation">JSON</abbr> array</td> <td>the parsed <abbr title="JavaScript Object Notation">JSON</abbr> array</td> </tr> <tr> <td><code>parse_event_t::value</code></td> <td>the parser finished reading a <abbr title="JavaScript Object Notation">JSON</abbr> value</td> <td>depth of the value</td> <td>the parsed <abbr title="JavaScript Object Notation">JSON</abbr> value</td> </tr> </tbody> </table> <details class=example> <summary>Example</summary> <p>When parsing the following <abbr title="JavaScript Object Notation">JSON</abbr> text,</p> <div class=highlight><pre><span></span><code><span class=p>{</span>
@@ -8,7 +8,7 @@
<span class=w> </span><span class=mf>13.406667</span>
<span class=w> </span><span class=p>]</span>
<span class=p>}</span>
</code></pre></div> <p>these calls are made to the callback function:</p> <table> <thead> <tr> <th>event</th> <th>depth</th> <th>parsed</th> </tr> </thead> <tbody> <tr> <td><code>object_start</code></td> <td>0</td> <td><em>discarded</em></td> </tr> <tr> <td><code>key</code></td> <td>1</td> <td><code class=highlight><span class=s2>&quot;name&quot;</span></code></td> </tr> <tr> <td><code>value</code></td> <td>1</td> <td><code class=highlight><span class=s2>&quot;Berlin&quot;</span></code></td> </tr> <tr> <td><code>key</code></td> <td>1</td> <td><code class=highlight><span class=s2>&quot;location&quot;</span></code></td> </tr> <tr> <td><code>array_start</code></td> <td>1</td> <td><em>discarded</em></td> </tr> <tr> <td><code>value</code></td> <td>2</td> <td><code class=highlight><span class=mf>52.519444</span></code></td> </tr> <tr> <td><code>value</code></td> <td>2</td> <td><code class=highlight><span class=mf>13.406667</span></code></td> </tr> <tr> <td><code>array_end</code></td> <td>1</td> <td><code class=highlight><span class=p>[</span><span class=mf>52.519444</span><span class=p>,</span><span class=mf>13.406667</span><span class=p>]</span></code></td> </tr> <tr> <td><code>object_end</code></td> <td>0</td> <td><code class=highlight><span class=p>{</span><span class=nt>&quot;location&quot;</span><span class=p>:[</span><span class=mf>52.519444</span><span class=p>,</span><span class=mf>13.406667</span><span class=p>],</span><span class=nt>&quot;name&quot;</span><span class=p>:</span><span class=s2>&quot;Berlin&quot;</span><span class=p>}</span></code></td> </tr> </tbody> </table> </details> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">&para;</a></h2> <p>Discarding a value (i.e., returning <code class=highlight><span class=nb>false</span></code>) has different effects depending on the context in which the function was called:</p> <ul> <li>Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never read.</li> <li>In case a value outside a structured type is skipped, it is replaced with <code class=highlight><span class=kc>null</span></code>. This case happens if the top-level element is skipped.</li> </ul> <details class=example> <summary>Example</summary> <p>The example below demonstrates the <code>parse()</code> function with and without callback function.</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>these calls are made to the callback function:</p> <table> <thead> <tr> <th>event</th> <th>depth</th> <th>parsed</th> </tr> </thead> <tbody> <tr> <td><code>object_start</code></td> <td>0</td> <td><em>discarded</em></td> </tr> <tr> <td><code>key</code></td> <td>1</td> <td><code class=highlight><span class=s2>&quot;name&quot;</span></code></td> </tr> <tr> <td><code>value</code></td> <td>1</td> <td><code class=highlight><span class=s2>&quot;Berlin&quot;</span></code></td> </tr> <tr> <td><code>key</code></td> <td>1</td> <td><code class=highlight><span class=s2>&quot;location&quot;</span></code></td> </tr> <tr> <td><code>array_start</code></td> <td>1</td> <td><em>discarded</em></td> </tr> <tr> <td><code>value</code></td> <td>2</td> <td><code class=highlight><span class=mf>52.519444</span></code></td> </tr> <tr> <td><code>value</code></td> <td>2</td> <td><code class=highlight><span class=mf>13.406667</span></code></td> </tr> <tr> <td><code>array_end</code></td> <td>1</td> <td><code class=highlight><span class=p>[</span><span class=mf>52.519444</span><span class=p>,</span><span class=mf>13.406667</span><span class=p>]</span></code></td> </tr> <tr> <td><code>object_end</code></td> <td>0</td> <td><code class=highlight><span class=p>{</span><span class=nt>&quot;location&quot;</span><span class=p>:[</span><span class=mf>52.519444</span><span class=p>,</span><span class=mf>13.406667</span><span class=p>],</span><span class=nt>&quot;name&quot;</span><span class=p>:</span><span class=s2>&quot;Berlin&quot;</span><span class=p>}</span></code></td> </tr> </tbody> </table> </details> <div class="admonition note"> <p class=admonition-title>No built-in nesting depth limit</p> <p>The library has no built-in limit on recursion/nesting depth while parsing. A parser callback can only <em>discard</em> content it has already parsed (by returning <code class=highlight><span class=nb>false</span></code>); it cannot make parsing fail once a nesting limit is exceeded partway through reading a deeply nested value. If you need to reject over-deep untrusted input outright, track <code>depth</code> in a callback and <code>throw</code> from it once your limit is exceeded (a thrown exception propagates out of <code>parse()</code> as usual).</p> </div> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">&para;</a></h2> <p>Discarding a value (i.e., returning <code class=highlight><span class=nb>false</span></code>) has different effects depending on the context in which the function was called:</p> <ul> <li>Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never read.</li> <li>In case a value outside a structured type is skipped, it is replaced with <code class=highlight><span class=kc>null</span></code>. This case happens if the top-level element is skipped.</li> </ul> <details class=example> <summary>Example</summary> <p>The example below demonstrates the <code>parse()</code> function with and without callback function.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iostream&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iomanip&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
@@ -153,4 +153,25 @@
<span class=p>}</span>
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>{</span><span class=nt>&quot;one&quot;</span><span class=p>:</span><span class=mi>1</span><span class=p>,</span><span class=nt>&quot;two&quot;</span><span class=p>:</span><span class=mi>2</span><span class=p>}</span>
<span class=err>duplica</span><span class=kc>te</span><span class=w> </span><span class=err>JSON</span><span class=w> </span><span class=err>objec</span><span class=kc>t</span><span class=w> </span><span class=err>key</span><span class=p>:</span><span class=w> </span><span class=err>o</span><span class=kc>ne</span>
</code></pre></div> </details> <p>This approach has two limitations:</p> <ul> <li>The depth-indexed bookkeeping must account for the fact that <code>object_start</code> reports the depth of the <em>parent</em> of the object, while the <code>key</code> events inside that object are reported one depth deeper (see the event table above); it is easy to get this off by one for nested objects.</li> <li>The thrown exception cannot carry a <code>parse_error</code>-style byte offset, because position tracking only exists inside the parser and lexer, not at the callback layer.</li> </ul> <p>For strict validation with precise error positions, implementing a <a href=../sax_interface/ ><abbr title="Simple API for XML">SAX</abbr> interface</a> instead gives access to the parser's position information directly.</p> <!-- 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="July 10, 2026 11:56:06 UTC">July 10, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9Line truncated
</code></pre></div> </details> <p>This approach has two limitations:</p> <ul> <li>The depth-indexed bookkeeping must account for the fact that <code>object_start</code> reports the depth of the <em>parent</em> of the object, while the <code>key</code> events inside that object are reported one depth deeper (see the event table above); it is easy to get this off by one for nested objects.</li> <li>The thrown exception cannot carry a <code>parse_error</code>-style byte offset, because position tracking only exists inside the parser and lexer, not at the callback layer.</li> </ul> <p>For strict validation with precise error positions, implementing a <a href=../sax_interface/ ><abbr title="Simple API for XML">SAX</abbr> interface</a> instead gives access to the parser's position information directly.</p> <h2 id=recipe-streaming-a-large-homogeneous-array>Recipe: streaming a large homogeneous array<a class=headerlink href=#recipe-streaming-a-large-homogeneous-array title="Permanent link">&para;</a></h2> <p>A common use case is a huge top-level array of many similarly-shaped objects, too large to hold entirely in memory as a <code class=highlight><span class=n>json</span></code> value. A parser callback can hand off each completed element to a user function and then discard it, so memory usage stays bounded by a single element (plus the not-yet-parsed tail of the input) rather than the whole document. Since the top-level array's <code>array_start</code>/<code>array_end</code> are reported at <code>depth == 0</code> (its parent is the document root), the object elements it contains are reported at <code>depth == 1</code>:</p> <details class=example> <summary>Example</summary> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>ifstream</span><span class=w> </span><span class=n>input</span><span class=p>(</span><span class=s>&quot;large_array.json&quot;</span><span class=p>);</span>
<span class=k>auto</span><span class=w> </span><span class=n>callback</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>[](</span><span class=kt>int</span><span class=w> </span><span class=n>depth</span><span class=p>,</span><span class=w> </span><span class=n>json</span><span class=o>::</span><span class=n>parse_event_t</span><span class=w> </span><span class=n>event</span><span class=p>,</span><span class=w> </span><span class=n>json</span><span class=o>&amp;</span><span class=w> </span><span class=n>parsed</span><span class=p>)</span><span class=w> </span><span class=o>-&gt;</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>depth</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=mi>1</span><span class=w> </span><span class=o>&amp;&amp;</span><span class=w> </span><span class=n>event</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>json</span><span class=o>::</span><span class=n>parse_event_t</span><span class=o>::</span><span class=n>object_end</span><span class=p>)</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=n>handle_element</span><span class=p>(</span><span class=n>parsed</span><span class=p>);</span><span class=w> </span><span class=c1>// process the element, e.g. write it elsewhere</span>
<span class=w> </span><span class=k>return</span><span class=w> </span><span class=nb>false</span><span class=p>;</span><span class=w> </span><span class=c1>// discard it -- frees its memory before the next one is parsed</span>
<span class=w> </span><span class=p>}</span>
<span class=w> </span><span class=k>return</span><span class=w> </span><span class=nb>true</span><span class=p>;</span><span class=w> </span><span class=c1>// keep everything else, including the (by then empty) top-level array</span>
<span class=p>};</span>
<span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>input</span><span class=p>,</span><span class=w> </span><span class=n>callback</span><span class=p>);</span>
</code></pre></div> </details> <p>If the array's elements are scalars or nested arrays instead of objects, check for <code>parse_event_t::value</code> or <code>parse_event_t::array_end</code> at <code>depth == 1</code> instead. The same approach works for a top-level <em>object</em> of many homogeneous values by checking <code>object_end</code>/<code>value</code> events at <code>depth == 1</code> there too.</p> <h2 id=recipe-max-nesting-depth-via-a-callback>Recipe: max nesting depth via a callback<a class=headerlink href=#recipe-max-nesting-depth-via-a-callback title="Permanent link">&para;</a></h2> <p>Since there is no built-in nesting-depth limit (see the note above), a callback can enforce one manually by tracking the maximum <code>depth</code> seen and throwing once it is exceeded:</p> <details class=example> <summary>Example</summary> <div class=highlight><pre><span></span><code><span class=k>constexpr</span><span class=w> </span><span class=kt>int</span><span class=w> </span><span class=n>max_depth</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>32</span><span class=p>;</span>
<span class=k>auto</span><span class=w> </span><span class=n>callback</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>[](</span><span class=kt>int</span><span class=w> </span><span class=n>depth</span><span class=p>,</span><span class=w> </span><span class=n>json</span><span class=o>::</span><span class=n>parse_event_t</span><span class=w> </span><span class=cm>/*event*/</span><span class=p>,</span><span class=w> </span><span class=n>json</span><span class=o>&amp;</span><span class=w> </span><span class=cm>/*parsed*/</span><span class=p>)</span><span class=w> </span><span class=o>-&gt;</span><span class=w> </span><span class=kt>bool</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>depth</span><span class=w> </span><span class=o>&gt;</span><span class=w> </span><span class=n>max_depth</span><span class=p>)</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=k>throw</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>runtime_error</span><span class=p>(</span><span class=s>&quot;maximum nesting depth exceeded&quot;</span><span class=p>);</span>
<span class=w> </span><span class=p>}</span>
<span class=w> </span><span class=k>return</span><span class=w> </span><span class=nb>true</span><span class=p>;</span>
<span class=p>};</span>
<span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>input</span><span class=p>,</span><span class=w> </span><span class=n>callback</span><span class=p>);</span>
</code></pre></div> </details> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="July 10, 2026 14:01:08 UTC">July 10, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M162.7 210c-1.8 3.3-25.2 44.4-70.1 123.5-4.9 8.3-10.8 12.5-17.7 12.5H9.8c-7.7 0-12.1-7.5-8.5-14.4l69-121.3q.3 0 0-.3l-43.9-75.6c-4.3-7.8.3-14.1 8.5-14.1H100c7.3 0 13.3 4.1 18 12.2zM382.6 46.1l-144 253v.3L330.2 466c3.9 7.1.2 14.1-8.5 14.1h-65.2c-7.6 0-13.6-4-18-12.2l-92.4-168.5c3.3-5.8 51.5-90.8 144.8-25Line truncated
@@ -53,6 +53,10 @@ these calls are made to the callback function:
| `array_end` | 1 | `[52.519444,13.406667]` |
| `object_end` | 0 | `{"location":[52.519444,13.406667],"name":"Berlin"}` |
No built-in nesting depth limit
The library has no built-in limit on recursion/nesting depth while parsing. A parser callback can only *discard* content it has already parsed (by returning `false`); it cannot make parsing fail once a nesting limit is exceeded partway through reading a deeply nested value. If you need to reject over-deep untrusted input outright, track `depth` in a callback and `throw` from it once your limit is exceeded (a thrown exception propagates out of `parse()` as usual).
## Return value
Discarding a value (i.e., returning `false`) has different effects depending on the context in which the function was called:
@@ -237,3 +241,44 @@ This approach has two limitations:
- The thrown exception cannot carry a `parse_error`-style byte offset, because position tracking only exists inside the parser and lexer, not at the callback layer.
For strict validation with precise error positions, implementing a [SAX interface](https://json.nlohmann.me/features/parsing/sax_interface/index.md) instead gives access to the parser's position information directly.
## Recipe: streaming a large homogeneous array
A common use case is a huge top-level array of many similarly-shaped objects, too large to hold entirely in memory as a `json` value. A parser callback can hand off each completed element to a user function and then discard it, so memory usage stays bounded by a single element (plus the not-yet-parsed tail of the input) rather than the whole document. Since the top-level array's `array_start`/`array_end` are reported at `depth == 0` (its parent is the document root), the object elements it contains are reported at `depth == 1`:
Example
```
std::ifstream input("large_array.json");
auto callback = [](int depth, json::parse_event_t event, json& parsed) -> bool {
if (depth == 1 && event == json::parse_event_t::object_end) {
handle_element(parsed); // process the element, e.g. write it elsewhere
return false; // discard it -- frees its memory before the next one is parsed
}
return true; // keep everything else, including the (by then empty) top-level array
};
json::parse(input, callback);
```
If the array's elements are scalars or nested arrays instead of objects, check for `parse_event_t::value` or `parse_event_t::array_end` at `depth == 1` instead. The same approach works for a top-level *object* of many homogeneous values by checking `object_end`/`value` events at `depth == 1` there too.
## Recipe: max nesting depth via a callback
Since there is no built-in nesting-depth limit (see the note above), a callback can enforce one manually by tracking the maximum `depth` seen and throwing once it is exceeded:
Example
```
constexpr int max_depth = 32;
auto callback = [](int depth, json::parse_event_t /*event*/, json& /*parsed*/) -> bool {
if (depth > max_depth) {
throw std::runtime_error("maximum nesting depth exceeded");
}
return true;
};
json::parse(input, callback);
```