Merge pull request #1082 from esdrubal/bug19287
authorMarek Safar <marek.safar@gmail.com>
Wed, 4 Jun 2014 11:02:04 +0000 (13:02 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 4 Jun 2014 11:02:04 +0000 (13:02 +0200)
Improved JavaScriptSerializer.ConvertToType Nullable handling. Fixes #19287.

mcs/class/System.Web.Extensions/System.Web.Script.Serialization/JavaScriptSerializer.cs
mcs/class/System.Web.Extensions/Test/System.Web.Script.Serialization/JavaScriptSerializerTest.cs

index f8f65c52e8b2c8091b76fa7e62a32e94b84f6f6c..70e6fc9bebd955960d0fdc9d9d2f213d37bf26e6 100644 (file)
@@ -180,16 +180,20 @@ namespace System.Web.Script.Serialization
                                return c.ConvertFrom (obj);
                        }
 
-                       /*
-                        * Take care of the special case whereas in JSON an empty string ("") really means 
-                        * an empty value 
-                        * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
-                        */
-                       if ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition() == typeof(Nullable<>)))
-                       {
-                               string s = obj as String;
-                               if (String.IsNullOrEmpty(s))
+                       if ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition () == typeof (Nullable<>))) {
+                               if (obj is String) {
+                                       /*
+                                        * Take care of the special case whereas in JSON an empty string ("") really means 
+                                        * an empty value 
+                                        * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
+                                        */
+                                       if(String.IsNullOrEmpty ((String)obj))
                                                return null;
+                               } else if (c.CanConvertFrom (typeof (string))) {
+                                       TypeConverter objConverter = TypeDescriptor.GetConverter (obj);
+                                       string s = objConverter.ConvertToInvariantString (obj);
+                                       return c.ConvertFromInvariantString (s);
+                               }
                        }
 
                        return Convert.ChangeType (obj, targetType);
index 5883104a95ea894d3da7243815b4396ec45f012e..995590eeb2ec6cf0c5370555a82f0e304a7babeb 100644 (file)
@@ -1365,5 +1365,28 @@ namespace MonoTests.System.Web.Script.Serialization
                        var ret2vad = (IDictionary<string,object>) ret2va [0];
                        Assert.AreEqual ("subval", ret2vad ["subkey"], "#2.4");
                }
+               
+               class ClassWithNullableEnum
+               {
+                       public MyEnum? Value { get; set; }
+               }
+               
+               [Test]
+               public void DeserializeNullableEnum ()
+               {               
+                       var jsonValues = new Dictionary<string, MyEnum?> {
+                               { "{\"Value\":0}", MyEnum.AAA},
+                               { "{\"Value\":\"0\"}", MyEnum.AAA},
+                               { "{\"Value\":null}", null}
+                       };
+                       
+                       var ser = new JavaScriptSerializer ();
+                       
+                       foreach (var kv in jsonValues)
+                       {
+                               var obj = ser.Deserialize<ClassWithNullableEnum> (kv.Key);
+                               Assert.AreEqual (kv.Value, obj.Value);
+                       }
+               }
        }
 }