From 08139ff65713f01d3d5241aab9f719fe388d2ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 10 Nov 2016 10:38:18 +0100 Subject: [PATCH] Add serialization support for std.typecons.Typedef!T. --- data/vibe/data/serialization.d | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/data/vibe/data/serialization.d b/data/vibe/data/serialization.d index b7e1a73633..ea85dfea49 100644 --- a/data/vibe/data/serialization.d +++ b/data/vibe/data/serialization.d @@ -23,6 +23,7 @@ and will always be serialized as a string.) $(LI Any `Nullable!T` will be serialized as either `null`, or as the contained value (subject to these rules again).) + $(LI Any `Typedef!T` will be serialized as if it were just `T`.) $(LI Any `BitFlags!T` value will be serialized as `T[]`) $(LI Types satisfying the `isPolicySerializable` trait for the supplied `Policy` will be serialized as the value returned @@ -339,7 +340,7 @@ private template serializeValueImpl(Serializer, alias Policy) { private void serializeValue(T, ATTRIBUTES...)(ref Serializer ser, T value) { - import std.typecons : BitFlags, Nullable, Tuple, tuple; + import std.typecons : BitFlags, Nullable, Tuple, Typedef, TypedefType, tuple; alias TU = Unqual!T; @@ -413,6 +414,8 @@ private template serializeValueImpl(Serializer, alias Policy) { } else static if (/*isInstanceOf!(Nullable, TU)*/is(T == Nullable!TPS, TPS...)) { if (value.isNull()) ser.serializeValue!(typeof(null))(null); else ser.serializeValue!(typeof(value.get()), ATTRIBUTES)(value.get()); + } else static if (isInstanceOf!(Typedef, TU)) { + ser.serializeValue!(TypedefType!TU, ATTRIBUTES)(cast(TypedefType!TU)value); } else static if (is(TU == BitFlags!E, E)) { alias STraits = SubTraits!(Traits, E); @@ -525,7 +528,7 @@ private template deserializeValueImpl(Serializer, alias Policy) { T deserializeValue(T, ATTRIBUTES...)(ref Serializer ser) if(isMutable!T) { - import std.typecons : BitFlags, Nullable; + import std.typecons : BitFlags, Nullable, Typedef, TypedefType; static struct Traits { alias Type = T; @@ -589,6 +592,8 @@ private template deserializeValueImpl(Serializer, alias Policy) { } else static if (isInstanceOf!(Nullable, T)) { if (ser.tryReadNull!Traits()) return T.init; return T(ser.deserializeValue!(typeof(T.init.get()), ATTRIBUTES)); + } else static if (isInstanceOf!(Typedef, T)) { + return T(ser.deserializeValue!(TypedefType!T, ATTRIBUTES)); } else static if (is(T == BitFlags!E, E)) { alias STraits = SubTraits!(Traits, E); T ret; @@ -1579,3 +1584,12 @@ unittest { auto deser = deserialize!(TestSerializer, Custom)(ser); assert(deser.x == 42); } + +unittest { + import std.typecons : Typedef; + alias T = Typedef!int; + auto ser = serialize!TestSerializer(T(42)); + assert(ser == "V(i)(42)", ser); + auto deser = deserialize!(TestSerializer, T)(ser); + assert(deser == 42); +}