-Change Type to always be written as AssemblyQualifiedName

This commit is contained in:
JamesNK
2010-01-31 05:32:53 +00:00
parent fa85ebfbbf
commit 4dd8a2f6b8
2 changed files with 19 additions and 3 deletions
@@ -960,13 +960,23 @@ keyword such as type of business.""
[Test] [Test]
public void SerializeTypeProperty() public void SerializeTypeProperty()
{ {
string boolRef = typeof(bool).AssemblyQualifiedName;
TypeClass typeClass = new TypeClass { TypeProperty = typeof(bool) }; TypeClass typeClass = new TypeClass { TypeProperty = typeof(bool) };
string json = JsonConvert.SerializeObject(typeClass); string json = JsonConvert.SerializeObject(typeClass);
Assert.AreEqual(@"{""TypeProperty"":""System.Boolean""}", json); Assert.AreEqual(@"{""TypeProperty"":""" + boolRef + @"""}", json);
TypeClass typeClass2 = JsonConvert.DeserializeObject<TypeClass>(json); TypeClass typeClass2 = JsonConvert.DeserializeObject<TypeClass>(json);
Assert.AreEqual(typeof(bool), typeClass2.TypeProperty); Assert.AreEqual(typeof(bool), typeClass2.TypeProperty);
string jsonSerializerTestRef = typeof(JsonSerializerTest).AssemblyQualifiedName;
typeClass = new TypeClass { TypeProperty = typeof(JsonSerializerTest) };
json = JsonConvert.SerializeObject(typeClass);
Assert.AreEqual(@"{""TypeProperty"":""" + jsonSerializerTestRef + @"""}", json);
typeClass2 = JsonConvert.DeserializeObject<TypeClass>(json);
Assert.AreEqual(typeof(JsonSerializerTest), typeClass2.TypeProperty);
} }
[Test] [Test]
@@ -232,7 +232,7 @@ namespace Newtonsoft.Json.Serialization
#if !SILVERLIGHT #if !SILVERLIGHT
&& !(converter is ComponentConverter) && !(converter is ComponentConverter)
#endif #endif
&& (converter.GetType() != typeof(TypeConverter) || value is Type)) && converter.GetType() != typeof(TypeConverter))
{ {
if (converter.CanConvertTo(typeof(string))) if (converter.CanConvertTo(typeof(string)))
{ {
@@ -247,13 +247,19 @@ namespace Newtonsoft.Json.Serialization
#endif #endif
#if SILVERLIGHT || PocketPC #if SILVERLIGHT || PocketPC
if (value is Guid || value is Type || value is Uri || value is TimeSpan) if (value is Guid || value is Uri || value is TimeSpan)
{ {
s = value.ToString(); s = value.ToString();
return true; return true;
} }
#endif #endif
if (value is Type)
{
s = ((Type)value).AssemblyQualifiedName;
return true;
}
s = null; s = null;
return false; return false;
} }