Skipping redundant JSON objects? #514
-
I'm dealing with a REST endpoint that wraps everything in a worthless Consider the following JSON:
Currently I'm doing this to get the data I want:
As you can see there's a superfluous Is it possible to tell Glaze to start parsing inside the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
There exists glz::skip{} to simply skip values that you don't care about. The README has some basic documentation on glz::skip, but you just put in in your meta like |
Beta Was this translation helpful? Give feedback.
-
@matttyson #include <glaze/core/common.hpp>
#include <glaze/core/meta.hpp>
#include <glaze/json/read.hpp>
#include <string_view>
struct Response {
std::string data{};
std::int64_t foo{};
bool bar{};
};
struct Reply {
Response response{};
};
template <> struct glz::meta<Response> {
using T = Response;
static constexpr auto value = object("data", &T::data, //
"foo", &T::foo, //
"bar", &T::bar //
);
};
template <> struct glz::meta<Reply> {
using T = Reply;
static constexpr auto value = object("response", &T::response);
};
int main() {
constexpr std::string_view rest_reply = R"({
"response": {
"data": "Hello, World!",
"foo": 123,
"bar": true
}
})";
// note the transform
const auto resp =
glz::read_json<Reply>(rest_reply).transform([](Reply&& reply) {
return std::move(reply.response);
});
assert(resp.has_value());
assert(resp->data == "Hello, World!");
assert(resp->foo == 123u);
assert(resp->bar == true);
}
|
Beta Was this translation helpful? Give feedback.
Thanks for your clarification. The support you want doesn't exist right now, but I think it would be worth adding. You could parse the response to a glz::raw_json and then parse it into your sub struct, but this would require double parsing and probably isn't what you want.