Skip to content

How to declare recursive self referencing data structure? #421

@andreasdamm-shure

Description

@andreasdamm-shure

Trying to declare the data contract for a variant type that can have itself as one of the alternatives.

struct Variant
{
   std::variant<int, bool, std::shared_ptr<Variant>> value;
};

A std::shared_ptr has to be used as the compiler complains about incomplete type otherwise.

Calling

auto payload = daw::json::to_json(Variant {1}, std::vector<std::uint8_t> {});

With the following contract all call to to_json does not compile (parse_to_t missing).

namespace daw::json {

template<>
struct json_data_contract<Variant>
{
   using type = json_type_alias<
        json_variant_no_name<std::variant<int, bool, std::shared_ptr<Variant>>,
        json_variant_type_list<
            json_number_no_name<int>, 
            json_bool_no_name<bool>, 
            json_class_null_no_name<Variant, JsonNullable::Nullable, std::shared_ptr<Variant>>>>>;

   static auto to_json_data(const Variant &value) { return value.value; }
};

}

Are these type of data structures supported?

(Using MSVC 19.37.32825)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions