deserializing a json array of different objects. #298
-
Hi how does glaze deal with the following problem? [ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
tldr use std::tuple or a meta with a glz::array value for this example case and std::vector<std::variant<ObjectTypes...>> generally. It should just work if all the objects have metas. If the types always have the same order and length you can parse to a std::tuple of those types and it should work as long as they have metas for all the individual objects. If the array has variable length and differing types you can create a std::vector of std::variants of the different object types https://github.com/stephenberry/glaze/blob/main/docs/variant-handling.md#auto-deduction-of-object-types and as long as the objects all have metas and they all have unique key combinations it should work automatically. Worst case scenario you can parse to a There are also some situations (rare) where you may want to utilize a two stage parse where you parse to an array of glz::raw_json_view and then do typed parses later when you are able to deduce the type. If you need more details or a full example I can provide them. |
Beta Was this translation helpful? Give feedback.
tldr use std::tuple or a meta with a glz::array value for this example case and std::vector<std::variant<ObjectTypes...>> generally. It should just work if all the objects have metas.
If the types always have the same order and length you can parse to a std::tuple of those types and it should work as long as they have metas for all the individual objects.
std::tuple<Object1, Object2, std::string>
. Alternatively, you can also create an array based meta instead of an object oneglz::array(&T::object1, &T::object2, &T::date_time)
If the array has variable length and differing types you can create a std::vector of std::variants of the different object types https://github.com/stephenberry/gla…