boostorg / json

A C++11 library for parsing and serializing JSON to and from a DOM container in memory.
https://boost.org/libs/json
Boost Software License 1.0
434 stars 97 forks source link

JSON Pointer (was RFC6901) #480

Closed jano42 closed 2 years ago

jano42 commented 3 years ago

I'm wondering if accessing sub element is a feature that you could implement ?

{
     "foo1": "bar",
     "foo2": "bar2",
     "foo3": {
            "sfoo1" : 1,
            "sfoo2" : "42"
     }
}

The idea is to get a direct access to a sub-element like this:

boost::json::object data;
//parse data
auto &magicNumber = data["foo3.sfoo2"];

The path separator would be configurable ("." or "/",...)

This is RFC6901, as it's done by rapidjson for example

vinniefalco commented 3 years ago

Have a look at: https://github.com/boostorg/json/blob/develop/example/path.cpp and https://github.com/boostorg/json/blob/develop/example/proxy.cpp ?

hadrielk commented 3 years ago

If this was added natively, how would it distinguish this scenario?:

{
     "foo1": "bar",
     "foo2": "bar2",
     "foo3": {
            "sfoo1" : 1,
            "sfoo2" : "42"
     },
    "foo3.sfoo2" : "can't touch me!"
}

Would you expect the user to escape the "." before passing it to Boost.Json? That would be annoying for using string literals and other inputs, not to mention require Boost.Json to do more work to unescape it it for matching.

Normally for Json-Pointer support, libraries have differently named functions/methods so that they know the argument is a JSON Pointer, or have a lightweight class-type to denote it.

(Also, minor point but that isn't the JSON Pointer syntax - "/foo3/sfoo2" is, I believe)

vinniefalco commented 3 years ago

JSON Pointer support is a planned feature and it will follow the RFC (avoiding the problems that @hadrielk mentioned).

grisumbras commented 2 years ago

Implemented in #575