deploy: 54f6b3ef06
This commit is contained in:
1 parent
11a863958e
commit
40c9f42917
38 files changed
+564
-458
No files matched your search
@@ -2,7 +2,7 @@
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>basic_json</span><span class=o>&</span><span class=w> </span><span class=n>target</span><span class=p>,</span>
|
||||
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>string_t</span><span class=o>&</span><span class=w> </span><span class=n>path</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=s>""</span><span class=p>);</span>
|
||||
</code></pre></div> <p>Creates a <a href="http://jsonpatch.com"><abbr title="JavaScript Object Notation">JSON</abbr> Patch</a> so that value <code>source</code> can be changed into the value <code>target</code> by calling <a href=../patch/ ><code>patch</code></a> function.</p> <p>For two <abbr title="JavaScript Object Notation">JSON</abbr> values <code>source</code> and <code>target</code>, the following code yields <code class=highlight><span class=nb>true</span></code>: <div class=highlight><pre><span></span><code><span class=n>source</span><span class=p>.</span><span class=n>patch</span><span class=p>(</span><span class=n>diff</span><span class=p>(</span><span class=n>source</span><span class=p>,</span><span class=w> </span><span class=n>target</span><span class=p>))</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>target</span><span class=p>;</span>
|
||||
</code></pre></div></p> <p>The equality holds up to <a href=../operator_eq/ ><code>operator==</code></a>, so it has the following exceptions:</p> <ul> <li>A value that is not equal to itself cannot round-trip. This applies to <code>NaN</code> (also nested in an array or object) and to <a href=../is_discarded/ >discarded</a> values: <code>diff</code> emits a <code>replace</code> operation for them. A <code>NaN</code> in the patch is serialized as <code class=highlight><span class=kc>null</span></code>, so a patch that was dumped and parsed again would set <code class=highlight><span class=kc>null</span></code>.</li> <li>Numbers that compare equal produce no operation, even if their types or signs differ: <code>diff(1, 1.0)</code>, <code>diff(1u, 1)</code> (unsigned and signed integer) and <code>diff(0.0, -0.0)</code> all return <code class=highlight><span class=p>[]</span></code>. The patched value is then the <code>source</code> and keeps its number type.</li> </ul> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>source</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to compare from</dd> <dt><code>target</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to compare against</dd> <dt><code>path</code> (in)</dt> <dd>a prefix that is put verbatim in front of the <code>path</code> member of every operation (default: <code>""</code>, which addresses the root of the value). The prefix is neither escaped nor validated: <code class=highlight><span class=n>diff</span><span class=p>(</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=s>"abc"</span><span class=p>)</span></code> yields an operation with the <code>path</code> <code>abc</code>, which is not a valid <abbr title="JavaScript Object Notation">JSON</abbr> Pointer. Use it with a valid <abbr title="JavaScript Object Notation">JSON</abbr> Pointer such as <code>/root</code> and escape <code>~</code> and <code>/</code> in its reference tokens (see <a href=../../../features/json_pointer/ ><abbr title="JavaScript Object Notation">JSON</abbr> Pointer</a>).</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>a <abbr title="JavaScript Object Notation">JSON</abbr> patch to convert the <code>source</code> to <code>target</code></p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: <code>source</code> and <code>target</code> are never modified.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the total number of values (including nested ones) in <code>source</code> and <code>target</code>, times the cost of a key-based object operation (looking up a key in an object): logarithmic for <a href=../../json/ ><code>json</code></a>, and linear for <a href=../../ordered_json/ ><code>ordered_json</code></a> (see <a href=../../ordered_map/#complexity><code>ordered_map</code> complexity</a>). For <code>ordered_json</code>, diffing two objects with <code>n</code> and <code>m</code> members therefore takes O((n+m)²) when their keys do not line up (e.g., a key was added or the keys are in a different order). The size of the patch is proportional to the number of operations times the length of their paths, so a small change deep inside a value yields a long <code>path</code>.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <ul> <li>Currently, only <code>remove</code>, <code>add</code>, and <code>replace</code> operations are generated.</li> <li>The patch is correct, but not guaranteed to be minimal. For instance, <code>diff([1,2,3], [0,1,2,3])</code> yields three <code>replace</code> operations and one <code>add</code> operation instead of a single insertion.</li> <li>Differences between two arrays are reported index by index. Elements that were added at the end of the array are added with <code>add</code> operations on the path <code>/-</code> (appended in order), and elements that were removed are removed with <code>remove</code> operations from the highest index down to the lowest, so that the remaining indices stay valid.</li> <li>For objects, the operations for the members of <code>source</code> come first (<code>replace</code>, <code>remove</code>, or the operations for nested values, in the iteration order of <code>source</code>), followed by <code>add</code> operations for the members that only <code>target</code> has.</li> <li>For <a href=../../ordered_json/ ><code>ordered_json</code></a>, objects with the same keys in a different order are different. As a patch cannot reorder members, <code>diff</code> removes all keys of the <code>source</code> object and adds them agaLine truncated
|
||||
</code></pre></div></p> <p>The equality holds up to <a href=../operator_eq/ ><code>operator==</code></a>, so it has the following exceptions:</p> <ul> <li>A value that is not equal to itself cannot round-trip. This applies to <code>NaN</code> (also nested in an array or object) and to <a href=../is_discarded/ >discarded</a> values: <code>diff</code> emits a <code>replace</code> operation for them. A <code>NaN</code> in the patch is serialized as <code class=highlight><span class=kc>null</span></code>, so a patch that was dumped and parsed again would set <code class=highlight><span class=kc>null</span></code>.</li> <li>Numbers that compare equal produce no operation, even if their types differ or if one is zero and the other is negative zero: <code>diff(1, 1.0)</code> (integer and floating-point), <code>diff(1u, 1)</code> (unsigned and signed integer) and <code>diff(0.0, -0.0)</code> (positive and negative zero) all return <code class=highlight><span class=p>[]</span></code>. The patched value is then the <code>source</code> and keeps its number type and sign.</li> </ul> <h2 id=parameters>Parameters<a class=headerlink href=#parameters title="Permanent link">¶</a></h2> <dl> <dt><code>source</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to compare from</dd> <dt><code>target</code> (in)</dt> <dd><abbr title="JavaScript Object Notation">JSON</abbr> value to compare against</dd> <dt><code>path</code> (in)</dt> <dd>a prefix that is put verbatim in front of the <code>path</code> member of every operation (default: <code>""</code>, which addresses the root of the value). The prefix is neither escaped nor validated: <code class=highlight><span class=n>diff</span><span class=p>(</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=s>"abc"</span><span class=p>)</span></code> yields an operation with the <code>path</code> <code>abc</code>, which is not a valid <abbr title="JavaScript Object Notation">JSON</abbr> Pointer. Use it with a valid <abbr title="JavaScript Object Notation">JSON</abbr> Pointer such as <code>/root</code> and escape <code>~</code> and <code>/</code> in its reference tokens (see <a href=../../../features/json_pointer/ ><abbr title="JavaScript Object Notation">JSON</abbr> Pointer</a>).</dd> </dl> <h2 id=return-value>Return value<a class=headerlink href=#return-value title="Permanent link">¶</a></h2> <p>a <abbr title="JavaScript Object Notation">JSON</abbr> patch to convert the <code>source</code> to <code>target</code></p> <h2 id=exception-safety>Exception safety<a class=headerlink href=#exception-safety title="Permanent link">¶</a></h2> <p>Strong guarantee: <code>source</code> and <code>target</code> are never modified.</p> <h2 id=complexity>Complexity<a class=headerlink href=#complexity title="Permanent link">¶</a></h2> <p>Linear in the total number of values (including nested ones) in <code>source</code> and <code>target</code>, times the cost of a key-based object operation (looking up a key in an object): logarithmic for <a href=../../json/ ><code>json</code></a>, and linear for <a href=../../ordered_json/ ><code>ordered_json</code></a> (see <a href=../../ordered_map/#complexity><code>ordered_map</code> complexity</a>). For <code>ordered_json</code>, diffing two objects with <code>n</code> and <code>m</code> members therefore takes O((n+m)²) when their keys do not line up (e.g., a key was added or the keys are in a different order). The size of the patch is proportional to the number of operations times the length of their paths, so a small change deep inside a value yields a long <code>path</code>.</p> <h2 id=notes>Notes<a class=headerlink href=#notes title="Permanent link">¶</a></h2> <ul> <li>Currently, only <code>remove</code>, <code>add</code>, and <code>replace</code> operations are generated.</li> <li>The patch is correct, but not guaranteed to be minimal. For instance, <code>diff([1,2,3], [0,1,2,3])</code> yields three <code>replace</code> operations and one <code>add</code> operation instead of a single insertion.</li> <li>Differences between two arrays are reported index by index. Elements that were added at the end of the array are added with <code>add</code> operations on the path <code>/-</code> (appended in order), and elements that were removed are removed with <code>remove</code> operations from the highest index down to the lowest, so that the remaining indices stay valid.</li> <li>For objects, the operations for the members of <code>source</code> come first (<code>replace</code>, <code>remove</code>, or the operations for nested values, in the iteration order of <code>source</code>), followed by <code>add</code> operations for the members that only <code>target</code> has.</li> <li>For <a href=../../ordered_json/ ><code>ordered_json</code></a>, objects with the same keys in a different order are different. As a patch canLine truncated
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><iomanip></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
@@ -64,4 +64,4 @@
|
||||
<span class=w> </span><span class=s2>"world"</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="https://tools.ietf.org/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902 (<abbr title="JavaScript Object Notation">JSON</abbr> Patch)</a> - specification of <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> <li><a href=../patch/ ><code>patch</code></a> - applies a <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> <li><a href=../patch_inplace/ ><code>patch_inplace</code></a> - applies a <abbr title="JavaScript Object Notation">JSON</abbr> Patch in place</li> <li><a href=../merge_patch/ ><code>merge_patch</code></a> - applies a <abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</li> <li><a href=../merge_diff/ ><code>merge_diff</code></a> - creates a diff as a <abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</li> <li><a href=../../../features/json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch and Diff</a> - the article on <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 2.0.0.</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 27, 2026 06:41:42 UTC">October 27, 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> <nav class="md-footer__inner md-grid" aria-label=Footer> <a href=../default_object_comparator_t/ class="md-footer__link md-footer__link--prev" aria-label="Previous: default_object_comparator_t"> <div class="md-footer__button md-icon"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg> </div> <div class=md-footer__title> <span class=md-footer__direction> Previous </span> <div class=md-ellipsis> default_object_comparator_t </div> </div> </a> <a href=../dump/ class="md-footer__link md-footer__link--next" aria-label="Next: dump"> <div class=md-footer__title> <span class=md-footer__direction> Next </span> <div class=md-ellipsis> dump </div> </div> <div class="md-footer__button md-icon"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11z"/></svg> </div> </a> </nav> <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 2Line truncated
|
||||
</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="https://tools.ietf.org/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902 (<abbr title="JavaScript Object Notation">JSON</abbr> Patch)</a> - specification of <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> <li><a href=../patch/ ><code>patch</code></a> - applies a <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> <li><a href=../patch_inplace/ ><code>patch_inplace</code></a> - applies a <abbr title="JavaScript Object Notation">JSON</abbr> Patch in place</li> <li><a href=../merge_patch/ ><code>merge_patch</code></a> - applies a <abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</li> <li><a href=../merge_diff/ ><code>merge_diff</code></a> - creates a diff as a <abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</li> <li><a href=../../../features/json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch and Diff</a> - the article on <abbr title="JavaScript Object Notation">JSON</abbr> Patch</li> </ul> <h2 id=version-history>Version history<a class=headerlink href=#version-history title="Permanent link">¶</a></h2> <ul> <li>Added in version 2.0.0.</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 27, 2026 16:16:08 UTC">October 27, 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> <nav class="md-footer__inner md-grid" aria-label=Footer> <a href=../default_object_comparator_t/ class="md-footer__link md-footer__link--prev" aria-label="Previous: default_object_comparator_t"> <div class="md-footer__button md-icon"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg> </div> <div class=md-footer__title> <span class=md-footer__direction> Previous </span> <div class=md-ellipsis> default_object_comparator_t </div> </div> </a> <a href=../dump/ class="md-footer__link md-footer__link--next" aria-label="Next: dump"> <div class=md-footer__title> <span class=md-footer__direction> Next </span> <div class=md-ellipsis> dump </div> </div> <div class="md-footer__button md-icon"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11z"/></svg> </div> </a> </nav> <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 2Line truncated
|
||||
Reference in new issue
Block a user