Skip to content

Latest commit

 

History

History
100 lines (77 loc) · 1.74 KB

File metadata and controls

100 lines (77 loc) · 1.74 KB

jsoncons::jsonpointer::replace

Replace a json element or member.

Header

#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>

template<class J>
void replace(J& target, const typename J::string_view_type& path, const J& value); 

template<class J>
void replace(J& target, const typename J::string_view_type& path, const J& value, std::error_code& ec); 

Replaces the value at the location specified by path with a new value.

Return value

None

Exceptions

(1) Throws a jsonpointer_error if replace fails.

(2) Sets the std::error_code& to the jsonpointer_error_category if replace fails.

Examples

Replace an object value

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>

using namespace jsoncons;

int main()
{
    json target = json::parse(R"(
        {
          "baz": "qux",
          "foo": "bar"
        }
    )");

    std::error_code ec;
    jsonpointer::replace(target, "/baz", json("boo"), ec);
    if (ec)
    {
        std::cout << ec.message() << std::endl;
    }
    else
    {
        std::cout << target << std::endl;
    }
}

Output:

{
    "baz": "boo",
    "foo": "bar"
}

Replace an array value

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>

using namespace jsoncons;

int main()
{
    json target = json::parse(R"(
        { "foo": [ "bar", "baz" ] }
    )");

    std::error_code ec;
    jsonpointer::replace(target, "/foo/1", json("qux"), ec);
    if (ec)
    {
        std::cout << ec.message() << std::endl;
    }
    else
    {
        std::cout << pretty_print(target) << std::endl;
    }
}

Output:

{
    "foo": ["bar","qux"]
}