Coder Social home page Coder Social logo

Comments (4)

mwalcott3 avatar mwalcott3 commented on May 22, 2024 1

Reopened as @stephenberry decided to add the feature originally requested.

from glaze.

mwalcott3 avatar mwalcott3 commented on May 22, 2024

We could add a way of parsing a single json value quicker by skipping portions of the document we are not interested in but I see this as more of a type selection problem. There is limited support for handling this right now https://github.com/stephenberry/glaze/wiki/Variant-Handling. We really need to have a better way of generically handling variants and type selection but don't want to rush the solution and end up stuck with a bad one.

As a workaround, you could do a 2 stage parse using a generic parse and then a typed parse. This is very slow since it involves an extra unnecessary parse and quite a bit of dynamic memory allocations.

std::string buffer = R"( { "action": "GET", "data": { "x": 10, "y": 200 }})";

glz::json_t json{};
glz::read_json(json, buffer);
const &action = json["action"].get<std::string>();
if (action == "DELETE") {
    // Now we know the message type
    auto bomb = glz::read_json<bomb_t>(buffer);
}

from glaze.

sweihub avatar sweihub commented on May 22, 2024

Thanks for the thoughtful reply, BTW I never know there's a glz::json_t variant.

Since I am reluctant to integrate extra JSON libraries, So I will go with string pattern matching. Different JSON messages are so common in a websocket stream, so we have to decide the message types and deserialize them.

{"action":"subscribe", "symbols": ["BTCUSDT", "ETHUSDT"]}
{"action":"trade", "data": {"symbol": "ETHUSDT", "price": "123.4","volume":100}}

Substring matching

auto buffer = "{...}";
if (buffer.starts_with("{\"action\":\"subscribe\"")) {
    auto subscribe = glz::read_json<subscribe_t>(buffer);
}

Thanks anyway, I will stay tune to the project.

from glaze.

stephenberry avatar stephenberry commented on May 22, 2024

@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")") {
   auto bomb = glz::read_json<bomb_t>(buffer);
   expect(bomb.data.x == 10);
   expect(bomb.data.y == 200);
}

Notice that "action" is registered with a skip type, which tells the parser to just skip this value because you've already handled it. You could add action as a std::string to your bomb_t, and it would populate the action on parse, but this skip approach is just a bit faster if you've already handled the action.

glz::get_sv_json gets the targeted value as a std::string_view, so notice the extra quotes needed when checking the value. This is to get the best performance possible. If you wanted to check against "DELETE" as a string without quotes then you could call: auto action = glz::get_as_json<std::string, "/action">(buffer);
glz::get_as_json will actually parse into the desired type, which then would allow you to check the action as follows:

if (action == "DELETE")

I hope all this makes sense, feel free to ask questions. We'll be adding documentation for these functions in the future.

from glaze.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.