Flatten JSon
#1067
Replies: 1 comment 2 replies
-
It is best to define the inner struct with Glaze because it is telling the compiler the actual layout of the JSON, which makes parsing more efficient. However, you can use pointers and references in C++ to remap this inner value to a value that is higher up in your struct to make your interface simpler. struct inner
{
double p{};
}; struct outer
{
std::string e{};
uint64_t T{};
uint64_t E{};
inner o{};
double* p{&o.p}; // a reference or pointer here remaps the value, but a reference cannot use pure reflection and will need a glz::meta
}; Is this the kind of remapping you want? If you could provide the C++ struct you want, that would help with explaining a solution. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to efficiently parse similar JSon strings from the Binance Api into the same struct. It seems that I can map multiple names from JSon strings to the same member variable with meta objects like:
static constexpr auto value = object("p", glz::quoted_num<&BinanceTradeGlaze::p>, "price", glz::quoted_num<&BinanceTradeGlaze::p> ...
Is this correct?
However, even so I have a problem, because in one type of JSon the relevant price field is in an inner object of the Json like this:
{"e":"ORDER_TRADE_UPDATE","T":1684156948198,"E":1684156948203,"o":{"p":"27363.90" ...
Is there a way to map
o:p
in the JSon to the member variablep
in my struct? I addedo:p
to the meta object, but this doesn't seem to work.I understand that I could just define an inner struct and it would parse it correctly, but I would like to only have one type of struct for a simpler interface to the methods that use the Binance trades and also do as little parsing and copying as possible.
Beta Was this translation helpful? Give feedback.
All reactions