mirror of
https://github.com/nlohmann/json.git
synced 2026-10-21 21:48:14 +00:00
deploy: 4155e2938f
This commit is contained in:
1 parent
be4d0a6f4b
commit
90f0c9d411
714 files changed
+22067
-622
No files matched your search
@@ -0,0 +1,64 @@
|
||||
<!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/api/basic_json_document/insert/" rel="canonical"><link href=../erase/ rel=prev><link href=../is_discarded/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>insert - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.ec1eaa64.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=#nlohmannbasic_json_documentinsert 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> insert </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 14 12 14 9.5 12 5 9.Line truncated
|
||||
<span class=n>view_type</span><span class=w> </span><span class=n>insert</span><span class=p>(</span><span class=n>view_type</span><span class=w> </span><span class=n>array</span><span class=p>,</span><span class=w> </span><span class=n>I</span><span class=w> </span><span class=n>idx</span><span class=p>,</span><span class=w> </span><span class=n>V</span><span class=o>&&</span><span class=w> </span><span class=n>value</span><span class=p>);</span>
|
||||
</code></pre></div> <p>Only an <strong>editable</strong> document (<code class=highlight><span class=n>Editable</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=nb>true</span></code>, e.g. <a href=../../json_editable_document/ ><code>json_editable_document</code></a>) has <code>insert</code>; calling it on a read-only <code>basic_json_document</code> fails to compile (<code class=highlight><span class=k>static_assert</span></code>).</p> <p>Inserts <code>value</code> into <code>array</code> as a new element before position <code>idx</code>, which must not be past the end (<code class=highlight><span class=n>idx</span><span class=w> </span><span class=o><=</span><span class=w> </span><span class=n>array</span><span class=p>.</span><span class=n>size</span><span class=p>()</span></code>; <code class=highlight><span class=n>idx</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>array</span><span class=p>.</span><span class=n>size</span><span class=p>()</span></code> appends, like <a href=../push_back/ ><code>push_back</code></a>). Unlike <a href=../push_back/ ><code>push_back</code></a>, a <a href=../../basic_json_view/is_null/ >null</a> <code>array</code> does <em>not</em> first become an empty array: <code>array</code> must already be an array.</p> <p><code>value</code> is accepted three ways: a <a href=../../basic_json_view/ ><code>basic_json_view</code></a> of <em>any</em> document -- read-only or editable, and it does not have to be <code>array</code>'s own document -- which is copied so that nothing is shared with the source document afterward; a <code>BasicJsonType</code> value; or anything <code>BasicJsonType</code> can be constructed from (numbers, strings, <code class=highlight><span class=kt>bool</span></code>, <code class=highlight><span class=nb>nullptr</span></code>, containers, ...).</p> <h2 id=template-parameters>Template parameters<a class=headerlink href=#template-parameters title="Permanent link">¶</a></h2> <dl> <dt><code>I</code></dt> <dd>an integral type other than <code class=highlight><span class=kt>bool</span></code>, deduced (overloads taking a <code class=highlight><span class=kt>bool</span></code> or a non-integral type for <code>idx</code> do not participate in overload resolution).</dd> <dt><code>V</code></dt> <dd>the type of <code>value</code>, deduced; see above for what is accepted.</dd> </dl> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>array</code> (in)</dt> <dd>the array to insert into</dd> <dt><code>idx</code> (in)</dt> <dd>the position to insert <code>value</code> before; a negative value throws (see <a href=#exceptions>Exceptions</a>)</dd> <dt><code>value</code> (in)</dt> <dd>the value to insert</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>a view of the new element, now holding <code>value</code></p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Basic exception safety: <code>value</code> is fully encoded -- including the checks below -- into storage owned by the document before anything already reachable from <a href=../root/ ><code>root()</code></a> is touched, so a failure while encoding <code>value</code> (an invalid argument, or <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>bad_alloc</span></code>) leaves the document completely unchanged, other than memory allocated for the encoding that is not reclaimed. A failure of a later allocation -- while <code>array</code> switches from its parsed layout to a growable block, or while that block grows, see <a href=#notes>Notes</a> -- can still leave <code>array</code> already switched to that layout even though <code>value</code> itself was not inserted.</p> <h2 id=exceptions>Exceptions<a class=headerlink href=#exceptions title="Permanent link">¶</a></h2> <p>Throws <a href=../../../home/exceptions/#jsonexceptiontype_error309><code>type_error.309</code></a> if <code>array</code> is not an array -- the same message <a href=../../basic_json/insert/ ><code>BasicJsonType::insert</code></a> throws for the same type; a null <code>array</code> throws this too (see above). Throws <a href=../../../home/exceptions/#jsonexceptionout_of_range401><code>out_of_range.401</code></a> if <code>idx</code> is negative, or if <code class=highlight><span class=n>idx</span><span class=w> </span><span class=o>></span><span class=w> </span><span class=n>array</span><span class=p>.</span><span class=n>size</span><span class=p>()</span></code>. Throws <a href=../../../home/exceptions/#jsonexceptioninvalid_iterator202><code>invalid_iterator.202</code></a> ("view does not belong to this document") if <code>array</code> is a <a href=../../basic_json_view/is_discarded/ >discarded</a> view or a view of a <em>dLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json_view.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_editable_document</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_editable_document</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json_editable_view</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json_editable_view</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// a deployment plan -- "budget" is written with a trailing zero that has</span>
|
||||
<span class=w> </span><span class=c1>// no effect on its value</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>text</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{</span>
|
||||
<span class=s> "release": "2026.09",</span>
|
||||
<span class=s> "steps": ["build", "test", "deploy"],</span>
|
||||
<span class=s> "budget": 19.90</span>
|
||||
<span class=s>}</span><span class=dl>)</span><span class=s>"</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=n>json_editable_document</span><span class=w> </span><span class=n>doc</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json_editable_document</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>text</span><span class=p>);</span>
|
||||
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=n>deploy_index</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>2</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>deploy</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>()[</span><span class=s>"steps"</span><span class=p>][</span><span class=n>deploy_index</span><span class=p>];</span><span class=w> </span><span class=c1>// held across the insert</span>
|
||||
|
||||
<span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>insert</span><span class=p>(</span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>()[</span><span class=s>"steps"</span><span class=p>],</span><span class=w> </span><span class=n>deploy_index</span><span class=p>,</span><span class=w> </span><span class=s>"smoke-test"</span><span class=p>);</span><span class=w> </span><span class=c1>// insert before "deploy"</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// the held view still refers to "deploy", even though its index moved</span>
|
||||
<span class=w> </span><span class=c1>// from 2 to 3, and nothing else in the document was touched</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>deploy</span><span class=p>.</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>doc</span><span class=p>.</span><span class=n>root</span><span class=p>().</span><span class=n>dump</span><span class=p>(</span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=sc>' '</span><span class=p>,</span><span class=w> </span><span class=nb>false</span><span class=p>,</span><span class=w> </span><span class=n>json_editable_view</span><span class=o>::</span><span class=n>number_format</span><span class=o>::</span><span class=n>source</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"</span><span class=se>\n\n</span><span class=s>"</span><span class=p>;</span>
|
||||
|
||||
<span class=w> </span><span class=c1>// the same edit on a plain json value: an index held from before the</span>
|
||||
<span class=w> </span><span class=c1>// insert now refers to whatever moved into that slot, and dump()</span>
|
||||
<span class=w> </span><span class=c1>// rewrites "budget" to its shortest form even though it was never</span>
|
||||
<span class=w> </span><span class=c1>// touched</span>
|
||||
<span class=w> </span><span class=n>json</span><span class=w> </span><span class=n>plain</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</span><span class=p>(</span><span class=n>text</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>plain</span><span class=p>[</span><span class=s>"steps"</span><span class=p>].</span><span class=n>insert</span><span class=p>(</span><span class=n>plain</span><span class=p>[</span><span class=s>"steps"</span><span class=p>].</span><span class=n>begin</span><span class=p>()</span><span class=w> </span><span class=o>+</span><span class=w> </span><span class=k>static_cast</span><span class=o><</span><span class=n>std</span><span class=o>::</span><span class=kt>ptrdiff_t</span><span class=o>></span><span class=p>(</span><span class=n>deploy_index</span><span class=p>),</span><span class=w> </span><span class=s>"smoke-test"</span><span class=p>);</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>plain</span><span class=p>[</span><span class=s>"steps"</span><span class=p>][</span><span class=n>deploy_index</span><span class=p>].</span><span class=n>dump</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>plain</span><span class=p>.</span><span class=n>dump</span><span class=p>(</span><span class=mi>2</span><span class=p>)</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=s2>"deploy"</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=nt>"release"</span><span class=p>:</span><span class=w> </span><span class=s2>"2026.09"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"steps"</span><span class=p>:</span><span class=w> </span><span class=p>[</span>
|
||||
<span class=w> </span><span class=s2>"build"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=s2>"test"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=s2>"smoke-test"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=s2>"deploy"</span>
|
||||
<span class=w> </span><span class=p>],</span>
|
||||
<span class=w> </span><span class=nt>"budget"</span><span class=p>:</span><span class=w> </span><span class=mf>19.90</span>
|
||||
<span class=p>}</span>
|
||||
|
||||
<span class=s2>"smoke-test"</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=nt>"budget"</span><span class=p>:</span><span class=w> </span><span class=mf>19.9</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"release"</span><span class=p>:</span><span class=w> </span><span class=s2>"2026.09"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=nt>"steps"</span><span class=p>:</span><span class=w> </span><span class=p>[</span>
|
||||
<span class=w> </span><span class=s2>"build"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=s2>"test"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=s2>"smoke-test"</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=s2>"deploy"</span>
|
||||
<span class=w> </span><span class=p>]</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> </details> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">¶</a></h2> <ul> <li><a href=../push_back/ >push_back</a> - append to an array</li> <li><a href=../erase/ >erase</a> - remove an object member, an array element, or the value a <abbr title="JavaScript Object Notation">JSON</abbr> pointer refers to</li> <li><a href=../set/ >set</a> - replace a value, or set an object member, an array element, or the value a <abbr title="JavaScript Object Notation">JSON</abbr> pointer refers to</li> <li><a href=../../basic_json/insert/ ><code>BasicJsonType::insert</code></a> - the corresponding function of <code>basic_json</code></li> <li><a href=../#edits>Edits</a> - what an edit guarantees, for every overload</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>.</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 21, 2026 08:21:29 UTC">October 21, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 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 4Line truncated
|
||||
@@ -0,0 +1,133 @@
|
||||
# nlohmann::basic_json_document::insert
|
||||
|
||||
```
|
||||
template<typename I, typename V>
|
||||
view_type insert(view_type array, I idx, V&& value);
|
||||
```
|
||||
|
||||
Only an **editable** document (`Editable == true`, e.g. [`json_editable_document`](https://json.nlohmann.me/api/json_editable_document/index.md)) has `insert`; calling it on a read-only `basic_json_document` fails to compile (`static_assert`).
|
||||
|
||||
Inserts `value` into `array` as a new element before position `idx`, which must not be past the end (`idx <= array.size()`; `idx == array.size()` appends, like [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md)). Unlike [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), a [null](https://json.nlohmann.me/api/basic_json_view/is_null/index.md) `array` does *not* first become an empty array: `array` must already be an array.
|
||||
|
||||
`value` is accepted three ways: a [`basic_json_view`](https://json.nlohmann.me/api/basic_json_view/index.md) of *any* document -- read-only or editable, and it does not have to be `array`'s own document -- which is copied so that nothing is shared with the source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers, strings, `bool`, `nullptr`, containers, ...).
|
||||
|
||||
## Template parameters
|
||||
|
||||
`I` : an integral type other than `bool`, deduced (overloads taking a `bool` or a non-integral type for `idx` do not participate in overload resolution).
|
||||
|
||||
`V` : the type of `value`, deduced; see above for what is accepted.
|
||||
|
||||
## Parameters
|
||||
|
||||
`array` (in) : the array to insert into
|
||||
|
||||
`idx` (in) : the position to insert `value` before; a negative value throws (see [Exceptions](#exceptions))
|
||||
|
||||
`value` (in) : the value to insert
|
||||
|
||||
## Return value
|
||||
|
||||
a view of the new element, now holding `value`
|
||||
|
||||
## Exception safety
|
||||
|
||||
Basic exception safety: `value` is fully encoded -- including the checks below -- into storage owned by the document before anything already reachable from [`root()`](https://json.nlohmann.me/api/basic_json_document/root/index.md) is touched, so a failure while encoding `value` (an invalid argument, or `std::bad_alloc`) leaves the document completely unchanged, other than memory allocated for the encoding that is not reclaimed. A failure of a later allocation -- while `array` switches from its parsed layout to a growable block, or while that block grows, see [Notes](#notes) -- can still leave `array` already switched to that layout even though `value` itself was not inserted.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`type_error.309`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error309) if `array` is not an array -- the same message [`BasicJsonType::insert`](https://json.nlohmann.me/api/basic_json/insert/index.md) throws for the same type; a null `array` throws this too (see above). Throws [`out_of_range.401`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range401) if `idx` is negative, or if `idx > array.size()`. Throws [`invalid_iterator.202`](https://json.nlohmann.me/home/exceptions/#jsonexceptioninvalid_iterator202) ("view does not belong to this document") if `array` is a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view or a view of a *different* document. Throws [`type_error.302`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error302) if `value` is a [discarded](https://json.nlohmann.me/api/basic_json_view/is_discarded/index.md) view or a [discarded](https://json.nlohmann.me/api/basic_json/is_discarded/index.md) `BasicJsonType` value, and [`type_error.319`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error319) if `value` is (or contains) a binary value -- `BasicJsonType` can hold one, but a `json_document` cannot. Throws [`type_error.316`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error316) if `value` is (or contains) a string that is not valid UTF-8, with the same message [`BasicJsonType::dump()`](https://json.nlohmann.me/api/basic_json/dump/index.md) gives for that string.
|
||||
|
||||
## Complexity
|
||||
|
||||
Linear in the number of elements of `array` at or after `idx` (they move one slot over), plus time linear in the size of `value` to encode it into the document's storage (constant for a scalar, linear in the number of nested values for an array or object): like [`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md), the elements of `array` move to a growable block of links the first time it is inserted into (or [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md)/[`push_back`](https://json.nlohmann.me/api/basic_json_document/push_back/index.md) on), and that block grows in amortized constant time; inserting before the end within that block still shifts every later element.
|
||||
|
||||
## Notes
|
||||
|
||||
Like [`set`](https://json.nlohmann.me/api/basic_json_document/set/index.md) on a member or an element, `insert` never moves an existing *element's value* -- only where `array`'s *links* to its elements live -- so a view of an existing element of `array` stays valid across an `insert`, and keeps referring to the same element even though its index shifts. Any iterator already taken over `array` is invalidated, since it was walking the old layout. See [Edits](https://json.nlohmann.me/api/basic_json_document/#edits) for what stays valid across an edit in general.
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example below inserts a step into the middle of a deployment plan, without touching the steps that come after it, and shows that a view taken before the insert keeps referring to the same element even though its index shifts -- something a plain `json`/`ordered_json` array, or its `std::vector`-based storage, has no equivalent for.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
using json_editable_document = nlohmann::json_editable_document;
|
||||
using json_editable_view = nlohmann::json_editable_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a deployment plan -- "budget" is written with a trailing zero that has
|
||||
// no effect on its value
|
||||
const std::string text = R"({
|
||||
"release": "2026.09",
|
||||
"steps": ["build", "test", "deploy"],
|
||||
"budget": 19.90
|
||||
})";
|
||||
|
||||
json_editable_document doc = json_editable_document::parse(text);
|
||||
|
||||
const std::size_t deploy_index = 2;
|
||||
const auto deploy = doc.root()["steps"][deploy_index]; // held across the insert
|
||||
|
||||
doc.insert(doc.root()["steps"], deploy_index, "smoke-test"); // insert before "deploy"
|
||||
|
||||
// the held view still refers to "deploy", even though its index moved
|
||||
// from 2 to 3, and nothing else in the document was touched
|
||||
std::cout << deploy.dump() << '\n';
|
||||
std::cout << doc.root().dump(2, ' ', false, json_editable_view::number_format::source) << "\n\n";
|
||||
|
||||
// the same edit on a plain json value: an index held from before the
|
||||
// insert now refers to whatever moved into that slot, and dump()
|
||||
// rewrites "budget" to its shortest form even though it was never
|
||||
// touched
|
||||
json plain = json::parse(text);
|
||||
plain["steps"].insert(plain["steps"].begin() + static_cast<std::ptrdiff_t>(deploy_index), "smoke-test");
|
||||
std::cout << plain["steps"][deploy_index].dump() << '\n';
|
||||
std::cout << plain.dump(2) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
"deploy"
|
||||
{
|
||||
"release": "2026.09",
|
||||
"steps": [
|
||||
"build",
|
||||
"test",
|
||||
"smoke-test",
|
||||
"deploy"
|
||||
],
|
||||
"budget": 19.90
|
||||
}
|
||||
|
||||
"smoke-test"
|
||||
{
|
||||
"budget": 19.9,
|
||||
"release": "2026.09",
|
||||
"steps": [
|
||||
"build",
|
||||
"test",
|
||||
"smoke-test",
|
||||
"deploy"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [push_back](https://json.nlohmann.me/api/basic_json_document/push_back/index.md) - append to an array
|
||||
- [erase](https://json.nlohmann.me/api/basic_json_document/erase/index.md) - remove an object member, an array element, or the value a JSON pointer refers to
|
||||
- [set](https://json.nlohmann.me/api/basic_json_document/set/index.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers to
|
||||
- [`BasicJsonType::insert`](https://json.nlohmann.me/api/basic_json/insert/index.md) - the corresponding function of `basic_json`
|
||||
- [Edits](https://json.nlohmann.me/api/basic_json_document/#edits) - what an edit guarantees, for every overload
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.13.0 unreleased.
|
||||
Reference in new issue
Block a user