RejectedSoftware Forums

Sign up

how to serialize Variant to JSON

I have a data structure with one field that changes depending on context, i.e.

struct Foo {
string var0;
int var1;
Variant var2;
};

How can I make the JSON serialization in Vibe.d treat var2 properly, and generate 10 if it is an int 10, but "xxyz" if it has a string value?

Re: how to serialize Variant to JSON

On Mon, 30 May 2016 18:53:27 GMT, Øivind Loe wrote:

I have a data structure with one field that changes depending on context, i.e.

struct Foo {
string var0;
int var1;
Variant var2;
};

How can I make the JSON serialization in Vibe.d treat var2 properly, and generate 10 if it is an int 10, but "xxyz" if it has a string value?

I solved this by using Json as a type for var2 instead of Variant. Not totally happy about it, but it works.

Re: how to serialize Variant to JSON

Am 31.05.2016 um 01:20 schrieb Øivind Loe:

On Mon, 30 May 2016 18:53:27 GMT, Øivind Loe wrote:

I have a data structure with one field that changes depending on context, i.e.

struct Foo {
string var0;
int var1;
Variant var2;
};

How can I make the JSON serialization in Vibe.d treat var2 properly, and generate 10 if it is an int 10, but "xxyz" if it has a string value?

I solved this by using Json as a type for var2 instead of Variant. Not totally happy about it, but it works.

Serializing a Variant requires some additional work, because there is
no way for the serialization framework to know which types might be/are
contained in it. One possibility would be to wrap it into a custom type
and add toRepresentation/fromRepresentation methods:

struct MyVariant {
     Variant value;
     alias value this;

     Json toRepresentation() const { ... }
     static MyVariant fromRepresentation(Json j) { ... }
}

If you want to avoid the custom wrapper type, you could also use a
serialization policy:

template VariantPolicy(T) if (is(T == Variant)) {
     Json toRepresentation(Variant v) { ... }
     Variant fromRepresentation(Json j) { ... }
}

Variant v = ...;
auto json = serializeWithPolicy!(JsonSerializer, VariantPolicy)(v);

Re: how to serialize Variant to JSON

On Tue, 31 May 2016 08:13:42 +0200, Sönke Ludwig wrote:

Am 31.05.2016 um 01:20 schrieb Øivind Loe:

On Mon, 30 May 2016 18:53:27 GMT, Øivind Loe wrote:

I have a data structure with one field that changes depending on context, i.e.

struct Foo {
string var0;
int var1;
Variant var2;
};

How can I make the JSON serialization in Vibe.d treat var2 properly, and generate 10 if it is an int 10, but "xxyz" if it has a string value?

I solved this by using Json as a type for var2 instead of Variant. Not totally happy about it, but it works.

Serializing a Variant requires some additional work, because there is
no way for the serialization framework to know which types might be/are
contained in it. One possibility would be to wrap it into a custom type
and add toRepresentation/fromRepresentation methods:

struct MyVariant {
     Variant value;
     alias value this;

     Json toRepresentation() const { ... }
     static MyVariant fromRepresentation(Json j) { ... }
}

If you want to avoid the custom wrapper type, you could also use a
serialization policy:

template VariantPolicy(T) if (is(T == Variant)) {
     Json toRepresentation(Variant v) { ... }
     Variant fromRepresentation(Json j) { ... }
}

Variant v = ...;
auto json = serializeWithPolicy!(JsonSerializer, VariantPolicy)(v);

Aha. I have used fromRep.. and toRep.. in other places as well, but I did not think of using Json there. I thought you could only used fixed types. This solves it. Thank you!