On Wed, 07 Oct 2015 12:03:04 GMT, Martin Drasar wrote:

Hi,

I was wondering if it is possible to have vibe.d (de)serialize custom types that cannot be reasonable extended by providing toRepresentation/fromRepresentation, e.g. UUID struct from std.uuid.

After checking the isCustomSerializable trait, I tried to use this code:

string toRepresentation(UUID id) { return id.toString(); }
static UUID fromRepresentation (string id) { return parseUUID(id); }

But it is apparently not working. So, my question is - what should I do?

Thanks,
Martin

Two possibilities, either use a wrapper struct:

struct UUIDWrapper {
    UUID value;
    alias value this;
    string toString() const { return value.toString(); }
    static UUID fromString(string str) { return UUID(str); }
}

assert(uuid.serializeToJson() == Json(uuid.toString()));

or use the serialization policy feature:

template Policy(T) if (is(T == UUID)) {
    static string toRepresentation(UUID value) { return value.toString(); }
    static UUID fromRepresentation(string str) { return UUID(str); }
}

assert(uuid.serializeWithPolicy!(JsonSerializer, Policy) == Json(uuid.toString());