Boost Variant: How to model JSON?
You have to use a recursive wrapper (and you shouldn't be deriving from boost::variant
):
struct JsonValue;typedef boost::variant</*types*/, boost::recursive_wrapper<JsonValue> > JsonDataValue;struct JsonValue{ JsonDataValue value;};
To make Boost.Spirit take a JsonValue, you will need to write one of those Fusion adaptor things to adapt the raw variant type into a struct.
Moreover, how to safely cast JsonValue to JsonObject? When I try doing:
That's not how variants work. If you want to set them to a value, just set them like any other value:
JsonValue val;val.value = JsonValue();