Skip to content

How to read portion object from unknown JSON string? #151

Closed Answered by stephenberry
sweihub asked this question in Q&A
Discussion options

You must be logged in to vote

@sweihub
#150 has been merged with main. This adds the ability to generally parse a targeted value.

Your example problem can now be solved as follows:

struct xy_t {
   int x{};
   int y{};
};

template <>
struct glz::meta<xy_t> {
   using T = xy_t;
   static constexpr auto value = object("x", &T::x, "y", &T::y);
};

struct bomb_t {
   xy_t data{};
};

template <>
struct glz::meta<bomb_t> {
   using T = bomb_t;
   static constexpr auto value = object("action", skip{}, "data", &T::data);
};

In use:

std::string buffer = R"( { "action": "DELETE", "data": { "x": 10, "y": 200 }})";
      
auto action = glz::get_sv_json<"/action">(buffer);

expect(action == R"("DELETE")");
if (action == R"("DELETE"

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by stephenberry
Comment options

You must be logged in to vote
1 reply
@stephenberry
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #147 on February 01, 2023 20:39.