stephenberry/glaze

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

Opened this issue · 1 comments

qudix commented

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?

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.