Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support std::variant<...,std::variant<...> > #1092

Open
shiretu opened this issue Jun 13, 2024 · 2 comments
Open

Support std::variant<...,std::variant<...> > #1092

shiretu opened this issue Jun 13, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@shiretu
Copy link

shiretu commented Jun 13, 2024

Hi all,

Here is a patch that allows us to support variant of variant, each having tagged keys:

diff --git a/include/glaze/json/read.hpp b/include/glaze/json/read.hpp
index d13ef99..d8b91b8 100644
--- a/include/glaze/json/read.hpp
+++ b/include/glaze/json/read.hpp
@@ -2239,9 +2239,14 @@ namespace glz
                                        [&](auto&& v) {
                                           using V = std::decay_t<decltype(v)>;
                                           constexpr bool is_object = glaze_object_t<V> || reflectable<V>;
-                                          if constexpr (is_object) {
-                                             from_json<V>::template op<opening_handled<Opts>(), tag_literal>(v, ctx, it,
-                                                                                                             end);
+                                          if constexpr (is_variant<V>) {
+                                             from_json<V>::template op<Options>(v, ctx, std::prev(it), end);
+                                          }
+                                          else {
+                                             if constexpr (is_object) {
+                                                from_json<V>::template op<opening_handled<Opts>(), tag_literal>(
+                                                   v, ctx, it, end);
+                                             }
                                           }
                                        },
                                        value);
@shiretu
Copy link
Author

shiretu commented Jun 13, 2024

More info:

using subscribed = std::variant<subscribed_v4_orderbook, subscribed_v4_subaccounts>;
template <> struct glz::meta<subscribed> {
  static constexpr std::string_view tag = "channel";
  static constexpr auto ids = std::array{"v4_orderbook", "v4_subaccounts"};
};

using ws_message_type = std::variant<connected, subscribed, channel_data>;
template <> struct glz::meta<ws_message_type> {
  static constexpr std::string_view tag = "type";
  static constexpr auto ids = std::array{"connected", "subscribed", "channel_data"};
};

that work properly now: it switches on type and then on channel

@shiretu
Copy link
Author

shiretu commented Jun 13, 2024

created this PR: #1098 which also includes fixes for issue #1096

@stephenberry stephenberry added the bug Something isn't working label Jun 13, 2024
@stephenberry stephenberry added enhancement New feature or request and removed bug Something isn't working labels Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants