diff --git a/UnitTests/Tests.cs b/UnitTests/Tests.cs index 006477b..e03d5fb 100644 --- a/UnitTests/Tests.cs +++ b/UnitTests/Tests.cs @@ -307,6 +307,38 @@ public static void ParseTest() Assert.IsNotNull(o); } + public struct NullableEnumStruct + { + public enumt? P1; + public enumt? P2; + public enumt P3; + + public override string ToString() + { + return String.Format("{{P1={{{0}}}, P2={{{1}}}, P3={{{2}}}}}", P1, P2, P3); + } + + public NullableEnumStruct(enumt? p1, enumt? p2, enumt p3) + { + P1 = p1; + P2 = p2; + P3 = p3; + } + } + + [Test] + public static void NullableEnumTest() + { + var r = new NullableEnumStruct(enumt.B, null, enumt.C); + + var s = BJSON.ToBJSON(r); + var o = BJSON.ToObject(s); + + Assert.IsNotNull(o); + Assert.IsInstanceOf(r.GetType(), o); + Assert.AreEqual(r, o); + } + [Test] public static void StringListTest() { diff --git a/fastBinaryJSON/BJSON.cs b/fastBinaryJSON/BJSON.cs index f8a5216..3ccb02d 100644 --- a/fastBinaryJSON/BJSON.cs +++ b/fastBinaryJSON/BJSON.cs @@ -677,6 +677,8 @@ private NameValueCollection CreateNV(Dictionary d) private object CreateEnum(Type pt, object v) { // FEATURE : optimize create enum + if (pt.IsGenericType && Reflection.Instance.GetGenericTypeDefinition(pt) == typeof(Nullable<>)) + pt = Reflection.Instance.GetGenericArguments(pt)[0]; #if !SILVERLIGHT return Enum.Parse(pt, v.ToString()); #else diff --git a/fastBinaryJSON/Reflection.cs b/fastBinaryJSON/Reflection.cs index 88aa2fd..093ac48 100644 --- a/fastBinaryJSON/Reflection.cs +++ b/fastBinaryJSON/Reflection.cs @@ -315,7 +315,7 @@ private myPropInfo CreateMyProp(Type t, string name) else if (t == typeof(string)) d_type = myPropInfoType.String; else if (t == typeof(bool) || t == typeof(bool?)) d_type = myPropInfoType.Bool; else if (t == typeof(DateTime) || t == typeof(DateTime?)) d_type = myPropInfoType.DateTime; - else if (t.IsEnum) d_type = myPropInfoType.Enum; + else if (GetChangeType(t).IsEnum) d_type = myPropInfoType.Enum; else if (t == typeof(Guid) || t == typeof(Guid?)) d_type = myPropInfoType.Guid; else if (t == typeof(StringDictionary)) d_type = myPropInfoType.StringDictionary; else if (t == typeof(NameValueCollection)) d_type = myPropInfoType.NameValue; @@ -365,7 +365,7 @@ private myPropInfo CreateMyProp(Type t, string name) private Type GetChangeType(Type conversionType) { - if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) + if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition() == typeof(Nullable<>)) return Reflection.Instance.GetGenericArguments(conversionType)[0]; return conversionType;