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);
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);
+ }
+ }
}
}