Closed mohabouje closed 1 year ago
The following should work https://jsonlink.godbolt.org/z/dbjPWea3M
However, I believe that std::monostate should be mapped in the library and will be adding that to the next revision. That will be a break for code that uses it, but it's an easy fix to remove the mapping too. The issue with a deduced mapping for sum types/variant is there is no correct way to do it but server. So there is no deduction for it. https://github.com/beached/daw_json_link/blob/release/docs/cookbook/variant.md Talks about the ways variants can be mapped though.
#include <daw/json/daw_json_link.h>
#include <variant>
namespace daw::json {
template<>
struct json_data_contract<std::monostate> {
using type = json_member_list<>;
static constexpr std::tuple<> to_json_data( std::monostate ) noexcept {
return {};
}
};
}
int main(int argc, char** argv) {
using type = daw::json::json_variant_null_no_name<
std::variant<std::monostate, double, std::string_view>>;
auto a = daw::json::from_json<type>(R"("hello")");
}
After #407 you should be able to use code like
#include <daw/json/daw_json_link.h>
#include <variant>
int main(int argc, char** argv) {
using type = daw::json::json_variant_null_no_name<
std::variant<std::monostate, double, std::string_view>>;
auto a = daw::json::from_json<type>(R"("hello")");
}
Thanks for the quick fix! Everything works as expected 😀
Reopening, forgot to handle the serialization part
Hi,
I have a stream of data that could hold different types. The JSON objects do not provide a member to identify what type they are holding. Each JSON object has a unique structure, so no conflicts are expected.
In C++, stuff is represented with a
std::variant
that holds each type. I want to be able to do something like this:But It looks like the contract for variant types is missing:
I've tried to implement the contract myself based on the work already done for
json_submember_tagged_variant
, but I cannot seem to figure out stuff correctly.A simple solution: the parser will try to parse each member individually and build the variant with the first one that successfully was parsed. If
std::monostate
is on the list and nothing was parsed correctly, return that case. Ifstd::monostate
is not there, throw an error.