Replies: 1 comment 2 replies
-
@GJanos I've written these in the form of unit tests: For std::map<std::string, glz::raw_json> map;
glz::json_t obj{{ "arr", { 1, 2, 3 }}, { "hello", "world" }};
auto& o = obj.get<glz::json_t::object_t>();
for (auto&[key, value] : o) {
map[key] = glz::write_json(value);
}
auto s = glz::write_json(map);
expect(s == R"({"arr":[1,2,3],"hello":"world"})") << s; For std::map<std::string, glz::raw_json> map;
glz::obj obj{ "arr", glz::arr{ 1, 2, 3 }, "hello", "world" };
using T = std::decay_t<decltype(obj.value)>;
glz::for_each<std::tuple_size_v<T>>([&](auto I) {
if constexpr (I % 2 == 0) {
map[std::string(glz::tuplet::get<I>(obj.value))] = glz::write_json(glz::tuplet::get<I + 1>(obj.value));
}
});
auto s = glz::write_json(map);
expect(s == R"({"arr":[1,2,3],"hello":"world"})") << s; |
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 checked and have not found a way to iterate over the key and value fields of json objects implemented inside glz yet.
One good use case would be, when you have a string containing a serialized json, (contents of the json are not known), and would like to convert it to a glz::obj or glz::json_t (depending on the use case) instance. Then you would have to make operations (save / send / modify) on the individual fields.
Something simmilar to nlohmann's approach could be cool.
But for this first glz would need a way to directly serialoze strings to glz::obj, just like how it currently does with glz::json_t.
Thank you for your time! If this feature is already supported and I was careless, please let me know.
Beta Was this translation helpful? Give feedback.
All reactions