mirror of
https://github.com/nlohmann/json.git
synced 2026-11-09 22:12:32 +00:00
deploy: 6f01030c4e
This commit is contained in:
1 parent
cf8df97ba7
commit
eeea067a72
566 files changed
+15370
-2688
No files matched your search
@@ -0,0 +1,10 @@
|
||||
<!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=description content="JSON for Modern C++ is a header-only C++11 library that makes JSON a first-class data type."><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/json_view/differences/" rel="canonical"><link href=../lifetime/ rel=prev><link href=../reading/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.8.0"><title>Differences from basic_json - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.57a0d79d.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.fb2fffe2.min.css><link rel=stylesheet href=../../../css/custom.css><link rel=stylesheet href=../../../css/brand.css><link rel=stylesheet href=../../../css/fig-sax.css><link rel=stylesheet href=../../../css/fig-jsonview.css><link rel=stylesheet href=../../../css/fig-exceptions.css><link rel=stylesheet href=../../../css/fig-pointer.css><link rel=stylesheet href=../../../css/fig-charts.css><link rel=stylesheet href=../../../css/fig-integration.css><link rel=stylesheet href=../../../css/fig-macros.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=custom data-md-color-accent=custom> <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=#differences-from-basic_json 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> <img src=../../../images/wordmark-header.svg alt=logo> </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> Differences from basic_json </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=custom data-md-color-accent=custom 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=custom data-md-color-accent=custom 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 Line truncated
|
||||
<span class=n>json_view</span><span class=w> </span><span class=n>last</span><span class=p>;</span>
|
||||
<span class=k>for</span><span class=w> </span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>item</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>view</span><span class=p>.</span><span class=n>items</span><span class=p>())</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>item</span><span class=p>.</span><span class=n>key</span><span class=p>()</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=s>"a"</span><span class=p>)</span>
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=n>last</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>item</span><span class=p>.</span><span class=n>value</span><span class=p>();</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>If you cannot trust the source text to have unique keys, check <a href=../../../api/basic_json_view/size/ ><code>size()</code></a> against the number of distinct keys, or reject duplicates when iterating.</p> <h2 id=conversions>Conversions<a class=headerlink href=#conversions title="Permanent link">¶</a></h2> <ul> <li><strong>No implicit conversions.</strong> A view has no <code class=highlight><span class=k>operator</span><span class=w> </span><span class=n>ValueType</span><span class=p>()</span></code>, no <code class=highlight><span class=n>get_ptr</span><span class=p>()</span></code>, and no <code class=highlight><span class=n>get_ref</span><span class=p>()</span></code>; use <a href=../../../api/basic_json_view/get/ ><code>get<T>()</code></a>, or <a href=../../../api/basic_json_view/get_string/ ><code>get_string()</code></a> for a string without a copy.</li> <li><strong><code>from_json()</code> overloads.</strong> <a href=../../../api/basic_json_view/get/ ><code>get<T>()</code></a> calls <code>T</code>'s <code>from_json()</code> with the view only if that <code>from_json()</code> accepts views, and with <a href=../../../api/basic_json_view/materialize/ ><code>materialize()</code></a> otherwise; see <a href=../generic_code/#a-from_json-that-reads-views>A <code>from_json()</code> that reads views</a>. Both give the same result except for <a href=#duplicate-keys>repeated keys</a>.</li> <li><strong><code>string_view</code> members.</strong> A struct with a <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string_view</span></code> member decoded from a view points into the document, see <a href=../reading/#string_view-members><code>string_view</code> members</a>. Decoding the same struct from a <code>json</code> value compiles, too, but dangles at once if the value is a temporary.</li> </ul> <h2 id=serialization>Serialization<a class=headerlink href=#serialization title="Permanent link">¶</a></h2> <p><a href=../../../api/basic_json_view/dump/ ><code>dump()</code></a> writes numbers the way <a href=../../../api/basic_json/dump/ ><code>basic_json::dump()</code></a> would by default, but writes object members in <a href=#object-member-order>document order</a> and writes <a href=#duplicate-keys>every occurrence of a repeated key</a>.</p> <p>A view can also do what a <code>basic_json</code> value cannot: <a href=../../../api/basic_json_view/number_token/ ><code>number_token()</code></a> and <a href=../../../api/basic_json_view/number_format/ ><code>number_format::source</code></a> give every number exactly as it was spelled in the source text, while parsing into <code>basic_json</code> already reduces a number to its <code class=highlight><span class=kt>double</span></code>/<code class=highlight><span class=kt>int64_t</span></code> value. See <a href=../reading/#writing-a-view-back>Writing a view back</a>.</p> <h2 id=comparison>Comparison<a class=headerlink href=#comparison title="Permanent link">¶</a></h2> <p><strong>Ordering comparisons are not provided</strong> by <code>basic_json_view</code> — there is no <code class=highlight><span class=k>operator</span><span class=o><</span></code>. <a href=../../../api/basic_json_view/operator_eq/ ><code>operator==</code></a> and <a href=../../../api/basic_json_view/operator_ne/ ><code>operator!=</code></a> are provided, though: two views, or a view and a <code>BasicJsonType</code> value, compare equal exactly when <a href=../../../api/basic_json_view/materialize/ ><code>materialize()</code></a> or <a href=../../../api/basic_json/parse/ ><code>parse()</code></a> would produce equal values for them, without ever building a tree to do it. For ordering, <a href=../../../api/basic_json_view/materialize/ ><code>materialize()</code></a> is the way to get a value you can compare.</p> <h2 id=exception-context>Exception context<a class=headerlink href=#exception-context title="Permanent link">¶</a></h2> <p>With <a href=../../../api/macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a>, an exception of a view names the <abbr title="JavaScript Object Notation">JSON</abbr> Pointer path of the value within the document, and with <a href=../../../api/macros/json_diagnostic_positions/ ><code>JSON_DIAGNOSTIC_POSITIONS</code></a>, it names the bytes of the value in the source text, as they do for the exceptions of <code class=highlight><span class=n>basic_json</span></code>.</p> <p>A view has neither parent pointers nor stored positions, so both are found from the document's index when the exception is thrown. This takes time linear in the size of the document. One exception has neither: <code>key()</code> of an iterator over an array or a primitive value (<a href=../../../home/exceptions/#jsonexceptioninvalid_iterator207><code>invalid_iterator.207</code></a>), because a view's iterator does not know the value it iterates over.</p> <p><strong>Fewer source positions after edits.</strong> WLine truncated
|
||||
Reference in new issue
Block a user