glaze icon indicating copy to clipboard operation
glaze copied to clipboard

Add JSON pointer support (`glz::get<T>`) for `glz::json_t`

Open qudix opened this issue 1 year ago • 1 comments

glaze v4.0.1

Using the following test:

{
    "test": true
}
#include <print>
#include <glaze/glaze.hpp>

int main()
{
    glz::json_t json;
    if (!glz::read_file_json(json, "test.json", std::string{}))
        std::println("found: {}", glz::get<bool>(json, "/test").has_value());
}

Expected: found: true Actual: found: false

However using a custom struct works:

#include <print>
#include <glaze/glaze.hpp>

struct test_t
{
    bool test;
};

int main()
{
    test_t test{};
    if (!glz::read_file_json(test, "test.json", std::string{}))
        std::println("found: {}", glz::get<bool>(test, "/test").has_value());
}

To get around needing a struct, I found the following to also work:

#include <print>
#include <glaze/glaze.hpp>

int main()
{
    glz::json_t json;
    std::string json_buf;
    if (!glz::read_file_json(json, "test.json", json_buf))
        std::println("found: {}", glz::get_as_json<bool, "/test">(json_buf).has_value());
}

But that's unintuitive (and potentially an incorrect use). Is this an intended limitation?

qudix avatar Nov 14 '24 02:11 qudix

The JSON pointer functions in Glaze were not developed for use with glz::json_t. However, I see how this could be very helpful and would make the library more cohesive. Thanks for bringing this up and I'll keep this issue alive until support has been added.

stephenberry avatar Nov 14 '24 14:11 stephenberry

@qudix, support has been merged into main with #1805

stephenberry avatar Jun 13 '25 22:06 stephenberry