-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
@@ -243,6 +243,8 @@ namespace Newtonsoft.Json.Tests.Linq
byte[] data = new byte[0];
Assert.AreEqual(data, (byte[])(new JValue(data)));
Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase)));
}
[Test]
@@ -66,6 +66,10 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(data, v.Value);
Assert.AreEqual(JTokenType.Bytes, v.Type);
v.Value = StringComparison.OrdinalIgnoreCase;
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
Assert.AreEqual(JTokenType.Integer, v.Type);
}
[Test]
@@ -23,7 +23,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\Debug\Compact\</OutputPath>
<DefineConstants>TRACE;DEBUG;PocketPC</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
@@ -35,7 +35,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>bin\Release\Compact\</OutputPath>
<DefineConstants>TRACE;$(PlatformFamilyName)</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
@@ -33,7 +33,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\Debug\DotNet20\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET20</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@@ -41,7 +41,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>bin\Release\DotNet20\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@@ -22,7 +22,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>Bin\Debug</OutputPath>
<OutputPath>Bin\Debug\Silverlight\</OutputPath>
<DefineConstants>DEBUG;TRACE;SILVERLIGHT</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
@@ -32,7 +32,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>Bin\Release</OutputPath>
<OutputPath>Bin\Release\Silverlight\</OutputPath>
<DefineConstants>TRACE;SILVERLIGHT</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
@@ -31,7 +31,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\Debug\DotNet\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@@ -39,7 +39,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>bin\Release\DotNet\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
+35 -2
View File
@@ -33,8 +33,8 @@ namespace Newtonsoft.Json.Tests
public class PerformanceTests : TestFixtureBase
{
private const int Iterations = 100;
//private const int Iterations = 5000;
//private const int Iterations = 100;
private const int Iterations = 5000;
#region Data
private const string BsonHex =
@@ -472,6 +472,39 @@ namespace Newtonsoft.Json.Tests
return (T)formatter.Deserialize(new MemoryStream(bytes));
}
#endregion
[Test]
public void SerializeLargeObject()
{
LargeRecursiveTestClass rootValue = null;
LargeRecursiveTestClass parentValue = null;
for (int i = 0; i < 20; i++)
{
LargeRecursiveTestClass currentValue = new LargeRecursiveTestClass()
{
Integer = int.MaxValue,
Text = "The quick red fox jumped over the lazy dog."
};
if (rootValue == null)
rootValue = currentValue;
if (parentValue != null)
parentValue.Child = currentValue;
parentValue = currentValue;
}
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, rootValue);
}
}
public class LargeRecursiveTestClass
{
public LargeRecursiveTestClass Child { get; set; }
public string Text { get; set; }
public int Integer { get; set; }
}
#region Classes
@@ -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]);
}
}
}