stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.23k stars 123 forks source link

JSON pointer (`glz::get<T>`) failing on `json_t` object #1441

Open qudix opened 1 week ago

qudix commented 1 week ago

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?

stephenberry commented 1 week ago

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.