-Added support for enum values to LINQ to JSON

-Added TypeNameAssemblyFormat property to JsonSerializer. Property controls whether .NET type names are written with a full or partial assembly name
-Added TypeNameHandling to JsonProperty attribute. Allows a type names to be included on an individual property basis
-Fixed serializing HashSet<T>
-Additional permission demands added to DynamicCodeGeneration flag
This commit is contained in:
JamesNK
2010-04-02 09:56:43 +00:00
parent 0f148a9788
commit f7cc6abfd9
28 changed files with 355 additions and 79 deletions
@@ -3372,5 +3372,31 @@ keyword such as type of business.""
Assert.AreEqual(4.3, d[1]);
Assert.AreEqual(null, d[2]);
}
#if !SILVERLIGHT && !NET20 && !PocketPC
[Test]
public void SerializeHashSet()
{
string jsonText = JsonConvert.SerializeObject(new HashSet<string>()
{
"One",
"2",
"III"
}, Formatting.Indented);
Assert.AreEqual(@"[
""One"",
""2"",
""III""
]", jsonText);
HashSet<string> d = JsonConvert.DeserializeObject<HashSet<string>>(jsonText);
Assert.AreEqual(3, d.Count);
Assert.IsTrue(d.Contains("One"));
Assert.IsTrue(d.Contains("2"));
Assert.IsTrue(d.Contains("III"));
}
#endif
}
}
@@ -26,10 +26,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Tests.TestObjects;
using NUnit.Framework;
using Newtonsoft.Json.Utilities;
using System.Net;
namespace Newtonsoft.Json.Tests.Serialization
{
@@ -38,7 +41,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void WriteTypeNameForObjects()
{
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple);
EmployeeReference employee = new EmployeeReference();
@@ -58,7 +61,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void DeserializeTypeName()
{
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple);
string json = @"{
""$id"": ""1"",
@@ -76,6 +79,26 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("Name!", ((EmployeeReference)employee).Name);
}
#if !SILVERLIGHT && !PocketPC
[Test]
public void DeserializeTypeNameFromGacAssembly()
{
string cookieRef = ReflectionUtils.GetTypeName(typeof(Cookie), FormatterAssemblyStyle.Simple);
string json = @"{
""$id"": ""1"",
""$type"": """ + cookieRef + @"""
}";
object cookie = JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
Assert.IsInstanceOfType(typeof(Cookie), cookie);
}
#endif
[Test]
public void SerializeGenericObjectListWithTypeName()
{
@@ -101,7 +124,8 @@ namespace Newtonsoft.Json.Tests.Serialization
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
});
Assert.AreEqual(@"[
@@ -157,7 +181,8 @@ namespace Newtonsoft.Json.Tests.Serialization
List<object> values = (List<object>)JsonConvert.DeserializeObject(json, typeof(List<object>), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
});
Assert.AreEqual(4, values.Count);
@@ -191,7 +216,8 @@ namespace Newtonsoft.Json.Tests.Serialization
JsonConvert.DeserializeObject(json, typeof(Person), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
});
}
@@ -275,7 +301,8 @@ namespace Newtonsoft.Json.Tests.Serialization
ICorrelatedMessage message = JsonConvert.DeserializeObject<ICorrelatedMessage>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
});
Assert.IsInstanceOfType(typeof(SendHttpRequest), message);
@@ -302,7 +329,8 @@ namespace Newtonsoft.Json.Tests.Serialization
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.All
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
});
Assert.AreEqual(@"{
@@ -317,5 +345,86 @@ namespace Newtonsoft.Json.Tests.Serialization
}
}", json);
}
public class TypeNameProperty
{
public string Name { get; set; }
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public object Value { get; set; }
}
[Test]
public void WriteObjectTypeNameForProperty()
{
string typeNamePropertyRef = ReflectionUtils.GetTypeName(typeof(TypeNameProperty), FormatterAssemblyStyle.Simple);
TypeNameProperty typeNameProperty = new TypeNameProperty
{
Name = "Name!",
Value = new TypeNameProperty
{
Name = "Nested!"
}
};
string json = JsonConvert.SerializeObject(typeNameProperty, Formatting.Indented);
Assert.AreEqual(@"{
""Name"": ""Name!"",
""Value"": {
""$type"": """ + typeNamePropertyRef + @""",
""Name"": ""Nested!"",
""Value"": null
}
}", json);
TypeNameProperty deserialized = JsonConvert.DeserializeObject<TypeNameProperty>(json);
Assert.AreEqual("Name!", deserialized.Name);
Assert.IsInstanceOfType(typeof(TypeNameProperty), deserialized.Value);
TypeNameProperty nested = (TypeNameProperty)deserialized.Value;
Assert.AreEqual("Nested!", nested.Name);
Assert.AreEqual(null, nested.Value);
}
[Test]
public void WriteListTypeNameForProperty()
{
string listRef = ReflectionUtils.GetTypeName(typeof(List<int>), FormatterAssemblyStyle.Simple);
TypeNameProperty typeNameProperty = new TypeNameProperty
{
Name = "Name!",
Value = new List<int> { 1, 2, 3, 4, 5 }
};
string json = JsonConvert.SerializeObject(typeNameProperty, Formatting.Indented);
Assert.AreEqual(@"{
""Name"": ""Name!"",
""Value"": {
""$type"": """ + listRef + @""",
""$values"": [
1,
2,
3,
4,
5
]
}
}", json);
TypeNameProperty deserialized = JsonConvert.DeserializeObject<TypeNameProperty>(json);
Assert.AreEqual("Name!", deserialized.Name);
Assert.IsInstanceOfType(typeof(List<int>), deserialized.Value);
List<int> nested = (List<int>)deserialized.Value;
Assert.AreEqual(5, nested.Count);
Assert.AreEqual(1, nested[0]);
Assert.AreEqual(2, nested[1]);
Assert.AreEqual(3, nested[2]);
Assert.AreEqual(4, nested[3]);
Assert.AreEqual(5, nested[4]);
}
}
}