mirror of
https://github.com/nlohmann/json.git
synced 2026-10-07 06:57:14 +00:00
deploy: 8d7e0046f4
This commit is contained in:
1 parent
f2f73132b2
commit
4deadb2473
254 files changed
+678
-475
No files matched your search
@@ -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/element_access/unchecked_access/" rel="canonical"><link href=../ rel=prev><link href=../checked_access/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.6"><title>Unchecked access: operator[] - 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=#unchecked-access-operator 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> Unchecked access: operator[] </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 Line 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/element_access/unchecked_access/" rel="canonical"><link href=../ rel=prev><link href=../checked_access/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.6"><title>Unchecked access: operator[] - 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=#unchecked-access-operator 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> Unchecked access: operator[] </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 Line truncated
|
||||
<span class=w> </span><span class=nt>"name"</span><span class=p>:</span><span class=w> </span><span class=s2>"Mary Smith"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"age"</span><span class=p>:</span><span class=w> </span><span class=mi>42</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"hobbies"</span><span class=p>:</span><span class=w> </span><span class=p>[</span><span class=s2>"hiking"</span><span class=p>,</span><span class=w> </span><span class=s2>"reading"</span><span class=p>]</span>
|
||||
@@ -19,4 +19,4 @@
|
||||
<span class=w> </span><span class=nt>"age"</span><span class=p>:</span><span class=w> </span><span class=mi>42</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"hobbies"</span><span class=p>:</span><span class=w> </span><span class=p>[</span><span class=s2>"running"</span><span class=p>,</span><span class=w> </span><span class=s2>"reading"</span><span class=p>,</span><span class=w> </span><span class=kc>null</span><span class=p>,</span><span class=w> </span><span class=s2>"cooking"</span><span class=p>]</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> </details> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <div class="admonition info"> <p class=admonition-title>Design rationale</p> <p>The library behaves differently to <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span></code> and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span></code>:</p> <ul> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>::</span><span class=k>operator</span><span class=p>[]</span></code> never inserts a new element.</li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span><span class=o>::</span><span class=k>operator</span><span class=p>[]</span></code> is not available for const values.</li> </ul> <p>The type <code class=highlight><span class=n>json</span></code> wraps all <abbr title="JavaScript Object Notation">JSON</abbr> value types. It would be impossible to remove <a href=../../../api/basic_json/operator%5B%5D/ ><code>operator[]</code></a> for const objects. At the same time, inserting elements for non-const objects is really convenient as it avoids awkward <code>insert</code> calls. To this end, we decided to have an inserting non-const behavior for both arrays and objects.</p> </div> <div class="admonition info"> <p class=admonition-title>Info</p> <p>The access is unchecked. In case the passed object key does not exist or the passed array index is invalid, no exception is thrown.</p> </div> <div class="admonition danger"> <p class=admonition-title>Danger</p> <ul> <li>It is <strong>undefined behavior</strong> to access a const object with a non-existing key.</li> <li>It is <strong>undefined behavior</strong> to access a const array with an invalid index.</li> <li>In debug mode, an <strong>assertion</strong> will fire in both cases. You can disable assertions by defining the preprocessor symbol <code class=highlight><span class=n>NDEBUG</span></code> or redefine the macro <a href=../../macros/#json_assertx><code>JSON_ASSERT(x)</code></a>. See the documentation on <a href=../../assertions/ >runtime assertions</a> for more information.</li> </ul> </div> <div class="admonition failure"> <p class=admonition-title>Exceptions</p> <p><code>operator[]</code> can only be used with objects (with a string argument) or with arrays (with a numeric argument). For other types, a <a href=../../../home/exceptions/#jsonexceptiontype_error305><code>basic_json::type_error</code></a> is thrown.</p> </div> <h2 id=summary>Summary<a class=headerlink href=#summary title="Permanent link">¶</a></h2> <table> <thead> <tr> <th>scenario</th> <th>non-const value</th> <th>const value</th> </tr> </thead> <tbody> <tr> <td>access to existing object key</td> <td>reference to existing value is returned</td> <td>const reference to existing value is returned</td> </tr> <tr> <td>access to valid array index</td> <td>reference to existing value is returned</td> <td>const reference to existing value is returned</td> </tr> <tr> <td>access to non-existing object key</td> <td>reference to newly inserted <code class=highlight><span class=kc>null</span></code> value is returned</td> <td><strong>undefined behavior</strong>; <a href=../../assertions/ >runtime assertion</a> in debug mode</td> </tr> <tr> <td>access to invalid array index</td> <td>reference to newly inserted <code class=highlight><span class=kc>null</span></code> value is returned; any index between previous maximal index and passed index are filled with <code class=highlight><span class=kc>null</span></code></td> <td><strong>undefined behavior</strong>; <a href=../../assertions/ >runtime assertion</a> in debug mode</td> </tr> </tbody> </table> <!-- 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 2, 2026 04:35:35 UTC">July 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(locatLine truncated
|
||||
</code></pre></div> </details> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <div class="admonition info"> <p class=admonition-title>Design rationale</p> <p>The library behaves differently to <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span></code> and <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span></code>:</p> <ul> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>::</span><span class=k>operator</span><span class=p>[]</span></code> never inserts a new element.</li> <li><code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>map</span><span class=o>::</span><span class=k>operator</span><span class=p>[]</span></code> is not available for const values.</li> </ul> <p>The type <code class=highlight><span class=n>json</span></code> wraps all <abbr title="JavaScript Object Notation">JSON</abbr> value types. It would be impossible to remove <a href=../../../api/basic_json/operator%5B%5D/ ><code>operator[]</code></a> for const objects. At the same time, inserting elements for non-const objects is really convenient as it avoids awkward <code>insert</code> calls. To this end, we decided to have an inserting non-const behavior for both arrays and objects.</p> </div> <div class="admonition info"> <p class=admonition-title>Info</p> <p>The access is unchecked. In case the passed object key does not exist or the passed array index is invalid, no exception is thrown.</p> </div> <div class="admonition danger"> <p class=admonition-title>Danger</p> <ul> <li>It is <strong>undefined behavior</strong> to access a const object with a non-existing key.</li> <li>It is <strong>undefined behavior</strong> to access a const array with an invalid index.</li> <li>In debug mode, an <strong>assertion</strong> will fire in both cases. You can disable assertions by defining the preprocessor symbol <code class=highlight><span class=n>NDEBUG</span></code> or redefine the macro <a href=../../macros/#json_assertx><code>JSON_ASSERT(x)</code></a>. See the documentation on <a href=../../assertions/ >runtime assertions</a> for more information.</li> </ul> </div> <div class="admonition failure"> <p class=admonition-title>Exceptions</p> <p><code>operator[]</code> can only be used with objects (with a string argument) or with arrays (with a numeric argument). For other types, a <a href=../../../home/exceptions/#jsonexceptiontype_error305><code>basic_json::type_error</code></a> is thrown.</p> </div> <h2 id=summary>Summary<a class=headerlink href=#summary title="Permanent link">¶</a></h2> <table> <thead> <tr> <th>scenario</th> <th>non-const value</th> <th>const value</th> </tr> </thead> <tbody> <tr> <td>access to existing object key</td> <td>reference to existing value is returned</td> <td>const reference to existing value is returned</td> </tr> <tr> <td>access to valid array index</td> <td>reference to existing value is returned</td> <td>const reference to existing value is returned</td> </tr> <tr> <td>access to non-existing object key</td> <td>reference to newly inserted <code class=highlight><span class=kc>null</span></code> value is returned</td> <td><strong>undefined behavior</strong>; <a href=../../assertions/ >runtime assertion</a> in debug mode</td> </tr> <tr> <td>access to invalid array index</td> <td>reference to newly inserted <code class=highlight><span class=kc>null</span></code> value is returned; any index between previous maximal index and passed index are filled with <code class=highlight><span class=kc>null</span></code></td> <td><strong>undefined behavior</strong>; <a href=../../assertions/ >runtime assertion</a> in debug mode</td> </tr> </tbody> </table> <!-- 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 2, 2026 13:59:36 UTC">July 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(locatLine truncated
|
||||
Reference in new issue
Block a user