Comments (4)
Reopened as @stephenberry decided to add the feature originally requested.
from glaze.
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.
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.
@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)
- Using an enum class as a template parameter will silently break reflection HOT 2
- Unable to read variant when default initialized to different variant index HOT 1
- How do you read [] JSON with your lib HOT 3
- Glaze will not link under arm 32 bit gcc. HOT 3
- Raw binary support HOT 10
- Build failure when including Windows.h header HOT 1
- Column-wise csv doesn't produce proper output when one of the fields is empty HOT 2
- Fail to generate perfect hash for reading when there are one or more unicode keys in struct HOT 7
- Support for file_include and hostname_include when using reflection HOT 1
- `write_json` seems to be broken in release v2.0.0 HOT 6
- Add validation mechanisms for writing JSON. Validate against a specification. HOT 1
- Reflection broken for write_json with const objects since 2.0.0 HOT 2
- Handle carriage return '\r' in CSV new lines
- Thoughts on untagged binary format HOT 4
- Compression helpers for BEVE and parallel compression
- Compilation error when setting read options HOT 2
- `write` fails for a specfic case using `std::variant` HOT 1
- Add end of buffer checks for invalid binary HOT 1
- call to consteval function 'glz::get_name<&ProjectSettings::identity>' is not a constant expression HOT 3
- cmake doesn't look for eigen HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from glaze.