2023-03-08 15:43:29 +03:00
<!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/patch_inplace/ rel = canonical > < link rel = icon href = ../../../assets/images/favicon.png > < meta name = generator content = "mkdocs-1.4.2, mkdocs-material-8.5.10" > < title > patch_inplace - JSON for Modern C++< / title > < link rel = stylesheet href = ../../../assets/stylesheets/main.975780f9.min.css > < link rel = stylesheet href = ../../../assets/stylesheets/palette.2505c338.min.css > < link rel = preconnect href = https://fonts.gstatic.com crossorigin > < link rel = stylesheet href = "https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback" > < 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 > < script > var palette = _ _md _get ( "__palette" ) ; if ( palette && "object" == typeof palette . color ) for ( var key of Object . keys ( palette . color ) ) document . body . setAttribute ( "data-md-color-" + key , palette . color [ key ] ) < / script > < 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_jsonpatch_inplace 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.54Z" / > < / 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 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z" / > < / 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 > patch_inplace < / 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_1 > < label class = "md-header__button md-icon" title = "Switch to dark mode" for = __palette_2 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 12 20 8.69Z" / > < / 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_2 > < label class = "md-header__button md-icon" title = "Switch to light mode" for = __palette_1 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 12c0-2 . 42-1 . 44-4 . 5-3 . 5-5 . 45C10 . 26 6 . 2 11
< / code > < / pre > < / div > < p > < a href = http://jsonpatch.com > JSON Patch< / a > defines a JSON document structure for expressing a sequence of operations to apply to a JSON document. With this function, a JSON Patch is applied to the current JSON value by executing all operations from the patch. This function applies a JSON patch in place and returns void.< / p > < h2 id = parameters > Parameters< a class = headerlink href = #parameters title = "Permanent link" > ¶ < / a > < / h2 > < dl > < dt > < code > json_patch< / code > (in)< / dt > < dd > JSON patch document< / dd > < / dl > < h2 id = exception-safety > Exception safety< a class = headerlink href = #exception-safety title = "Permanent link" > ¶ < / a > < / h2 > < p > No guarantees, value may be corrupted by an unsuccessful patch operation.< / p > < h2 id = exceptions > Exceptions< a class = headerlink href = #exceptions title = "Permanent link" > ¶ < / a > < / h2 > < ul > < li > Throws < a href = ../../../home/exceptions/#jsonexceptionparse_error104 > < code > parse_error.104< / code > < / a > if the JSON patch does not consist of an array of objects.< / li > < li > Throws < a href = ../../../home/exceptions/#jsonexceptionparse_error105 > < code > parse_error.105< / code > < / a > if the JSON patch is malformed (e.g., mandatory attributes are missing); example: < code > "operation add must have member path"< / code > .< / li > < li > Throws < a href = ../../../home/exceptions/#jsonexceptionout_of_range401 > < code > out_of_range.401< / code > < / a > if an array index is out of range.< / li > < li > Throws < a href = ../../../home/exceptions/#jsonexceptionout_of_range403 > < code > out_of_range.403< / code > < / a > if a JSON pointer inside the patch could not be resolved successfully in the current JSON value; example: < code > "key baz not found"< / code > .< / li > < li > Throws < a href = ../../../home/exceptions/#jsonexceptionout_of_range405 > < code > out_of_range.405< / code > < / a > if JSON pointer has no parent ("add", "remove", "move")< / li > < li > Throws < a href = ../../../home/exceptions/#jsonexceptionother_error501 > < code > out_of_range.501< / code > < / a > if "test" operation was unsuccessful.< / li > < / ul > < h2 id = complexity > Complexity< a class = headerlink href = #complexity title = "Permanent link" > ¶ < / a > < / h2 > < p > Linear in the size of the JSON value and the length of the JSON patch. As usually only a fraction of the JSON value is affected by the patch, the complexity can usually be neglected.< / p > < h2 id = notes > Notes< a class = headerlink href = #notes title = "Permanent link" > ¶ < / a > < / h2 > < p > Unlike < a href = ../patch/ > < code > patch< / code > < / a > , < code > patch_inplace< / code > applies the operation "in place" and no copy of the JSON value is created. That makes it faster for large documents by avoiding the copy. However, the JSON value might be corrupted if the function throws an exception.< / p > < h2 id = examples > Examples< a class = headerlink href = #examples title = "Permanent link" > ¶ < / a > < / h2 > < details class = example > < summary > Example< / summary > < p > The following code shows how a JSON patch is applied to a value.< / p > < div class = highlight > < pre > < span > < / span > < code > < span class = cp > #include< / span > < span class = w > < / span > < span class = cpf > < iostream> < / span > < span class = cp > < / span >
< span class = cp > #include< / span > < span class = w > < / span > < span class = cpf > < iomanip> < / span > < span class = cp > < / span >
< span class = cp > #include< / span > < span class = w > < / span > < span class = cpf > < nlohmann/json.hpp> < / span > < span class = cp > < / 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 = w > < / span >
< span class = k > using< / span > < span class = w > < / span > < span class = k > namespace< / span > < span class = w > < / span > < span class = nn > nlohmann< / span > < span class = o > ::< / span > < span class = nn > literals< / span > < span class = p > ;< / span > < span class = w > < / span >
< span class = kt > int< / span > < span class = w > < / span > < span class = nf > main< / span > < span class = p > ()< / span > < span class = w > < / span >
< span class = p > {< / span > < span class = w > < / span >
< span class = w > < / span > < span class = c1 > // the original document< / span >
< span class = w > < / span > < span class = n > json< / span > < span class = w > < / span > < span class = n > doc< / 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 > {< / span >
< span class = s > " baz" : " qux" ,< / span >
< span class = s > " foo" : " bar" < / span >
< span class = s > }< / span >
< span class = s > < / span > < span class = dl > )< / span > < span class = s > " < / span > < span class = n > _json< / span > < span class = p > ;< / span > < span class = w > < / span >
< span class = w > < / span > < span class = c1 > // the patch< / span >
< span class = w > < / span > < span class = n > json< / span > < span class = w > < / span > < span class = n > patch< / 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 > [< / span >
< span class = s > { " op" : " replace" , " path" : " /baz" , " value" : " boo" },< / span >
< span class = s > { " op" : " add" , " path" : " /hello" , " value" : [" world" ] },< / span >
< span class = s > { " op" : " remove" , " path" : " /foo" }< / span >
< span class = s > ]< / span >
< span class = s > < / span > < span class = dl > )< / span > < span class = s > " < / span > < span class = n > _json< / span > < span class = p > ;< / span > < span class = w > < / span >
< span class = w > < / span > < span class = c1 > // output original document< / 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 = s > " Before< / span > < span class = se > \n< / span > < span class = s > " < / 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 > setw< / span > < span class = p > (< / span > < span class = mi > 4< / span > < span class = p > )< / span > < span class = w > < / span > < span class = o > < < < / 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 > std< / span > < span class = o > ::< / span > < span class = n > endl< / span > < span class = p > ;< / span > < span class = w > < / span >
< span class = w > < / span > < span class = c1 > // apply the patch< / span >
< span class = w > < / span > < span class = n > doc< / span > < span class = p > .< / span > < span class = n > patch_inplace< / span > < span class = p > (< / span > < span class = n > patch< / span > < span class = p > );< / span > < span class = w > < / span >
< span class = w > < / span > < span class = c1 > // output patched document< / 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 = s > " < / span > < span class = se > \n< / span > < span class = s > After< / span > < span class = se > \n< / span > < span class = s > " < / 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 > setw< / span > < span class = p > (< / span > < span class = mi > 4< / span > < span class = p > )< / span > < span class = w > < / span > < span class = o > < < < / 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 > std< / span > < span class = o > ::< / span > < span class = n > endl< / span > < span class = p > ;< / span > < span class = w > < / span >
< span class = p > }< / span > < span class = w > < / span >
< / code > < / pre > < / div > < p > Output:< / p > < div class = highlight > < pre > < span > < / span > < code > < span class = err > Be< / span > < span class = kc > f< / span > < span class = err > ore< / span > < span class = w > < / span >
< span class = p > {< / span > < span class = w > < / span >
< span class = w > < / span > < span class = nt > " baz" < / span > < span class = p > :< / span > < span class = w > < / span > < span class = s2 > " qux" < / span > < span class = p > ,< / span > < span class = w > < / span >
< span class = w > < / span > < span class = nt > " foo" < / span > < span class = p > :< / span > < span class = w > < / span > < span class = s2 > " bar" < / span > < span class = w > < / span >
< span class = p > }< / span > < span class = w > < / span >
< span class = err > A< / span > < span class = kc > fter< / span > < span class = w > < / span >
< span class = p > {< / span > < span class = w > < / span >
< span class = w > < / span > < span class = nt > " baz" < / span > < span class = p > :< / span > < span class = w > < / span > < span class = s2 > " boo" < / span > < span class = p > ,< / span > < span class = w > < / span >
< span class = w > < / span > < span class = nt > " hello" < / span > < span class = p > :< / span > < span class = w > < / span > < span class = p > [< / span > < span class = w > < / span >
< span class = w > < / span > < span class = s2 > " world" < / span > < span class = w > < / span >
< span class = w > < / span > < span class = p > ]< / span > < span class = w > < / span >
< span class = p > }< / span > < span class = w > < / span >
2023-09-23 18:28:29 +03:00
< / 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 > RFC 6902 (JSON Patch)< / a > < / li > < li > < a href = https://tools.ietf.org/html/rfc6901 > RFC 6901 (JSON Pointer)< / a > < / li > < li > < a href = ../patch/ > patch< / a > applies a JSON Merge Patch< / li > < li > < a href = ../merge_patch/ > merge_patch< / a > applies a JSON Merge 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 3.11.0.< / li > < / ul > < hr > < div class = md-source-file > < small > Last update: < span class = "git-revision-date-localized-plugin git-revision-date-localized-plugin-date" > July 22, 2022< / span > < / small > < / div > < / article > < / div > < script > var tabs = _ _md _get ( "__tabs" ) ; if ( Array . isArray ( tabs ) ) e : for ( var set of document . querySelectorAll ( ".tabbed-set" ) ) { var tab , labels = set . querySelector ( ".tabbed-labels" ) ; for ( 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 > < / div > < a href = # 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 8v12Z" / > < / svg > Back to top < / a > < / main > < footer class = md-footer > < nav class = "md-footer__inner md-grid" aria-label = Footer > < a href = ../patch/ class = "md-footer__link md-footer__link--prev" aria-label = "Previous: patch" rel = prev > < 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 11h12Z" / > < / svg > < / div > < div class = md-footer__title > < div class = md-ellipsis > < span class = md-footer__direction > Previous < / span > patch < / div > < / div > < / a > < a href = ../push_back/ class = "md-footer__link md-footer__link--next" aria-label = "Next: push_back" rel = next > < div class = md-footer__title > < div class = md-ellipsis > < span class = md-footer__direction > Next < / span > push_back < / 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 11H4Z" / > < / 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 - 2022 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 496 512" > <!-- Font Awesome Free 6.2.1 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 2022 Fonticons, Inc. --> < path d = "M165.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 . 6zm-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 . 3zm44 . 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 . 9zM244 . 8 8C106 . 1 8 0 113 . 3 0 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 . 1C428 . 2 457 . 8 496 362 . 9 496 252 496 113 . 3 383 . 5 8 244 . 8 8zM97 . 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-1zm-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 . 7zm32 . 4 35 . 6c-1 . 6 1 . 3-1 4 . 3 1 . 3 6 . 2 2 . 3 2 . 3 5 . 2 2