From 6da0bfff5c8f07ae6539005eee7c96d8f45bea18 Mon Sep 17 00:00:00 2001 From: Remco Beurskens Date: Tue, 22 Aug 2017 11:21:14 +0200 Subject: [PATCH] Added support for nullable enums --- UnitTests/Tests.cs | 32 ++++++++++++++++++++++++++++++++ fastBinaryJSON/BJSON.cs | 2 ++ fastBinaryJSON/Reflection.cs | 6 +++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/UnitTests/Tests.cs b/UnitTests/Tests.cs index 922a632..f3231ec 100644 --- a/UnitTests/Tests.cs +++ b/UnitTests/Tests.cs @@ -306,6 +306,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 374dabc..871a6c3 100644 --- a/fastBinaryJSON/BJSON.cs +++ b/fastBinaryJSON/BJSON.cs @@ -623,6 +623,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 ae1cbd0..46759a2 100644 --- a/fastBinaryJSON/Reflection.cs +++ b/fastBinaryJSON/Reflection.cs @@ -204,7 +204,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; @@ -254,7 +254,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; @@ -286,7 +286,7 @@ internal Type GetTypeFromCache(string typename) //if (t == null) // RaptorDB : loading runtime assemblies //{ // t = Type.GetType(typename, (name) => { - // return AppDomain.CurrentDomain.GetAssemblies().Where(z => z.FullName == name.FullName).FirstOrDefault(); + // return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(z => z.FullName == name.FullName); // }, null, true); //} _typecache.Add(typename, t);