Skip to content

Latest commit

 

History

History
91 lines (71 loc) · 1.64 KB

File metadata and controls

91 lines (71 loc) · 1.64 KB

jsoncons::jsonpointer::replace

Replace a json element or member.

Header

#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>

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

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

Return value

On success, a value-initialized jsonpointer_errc.

On error, a jsonpointer_errc error code

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"
        }
    )");

    auto ec = jsonpointer::replace(target, "/baz", json("boo"));
    if (ec == jsonpointer::jsonpointer_errc())
    {
        std::cout << target << std::endl;
    }
    else
    {
        std::cout << make_error_code(ec).message() << 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" ] }
    )");

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

Output:

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