-Added ObjectCreationHandling to JsonPropertyAttribute
-Added IValueProvider, ReflectionValueProvider, DynamicValueProvider -Added Dynamic.snk -Added Required enum with options Default, AllowNull and Always -Added serialization event support to Silverlight version -Added Converter to JsonProperty for JsonConverter -Changed IsRequired to Required enum -Changed schema generator to allow for Required enum options -Changed Silverlight project version to Silverlight 3 -Improved error message when JSON array encountered for object and vice-versa when deserializing -Improved error message for converting types when deserializing -Improved error message from getting and setting properties when serializing and deserializing -Fixed empty string being converted to null when deserializing -Removed dependency on Entity Framework and LINQ to SQL assemblies
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
public class DynamicContractResolver : DefaultContractResolver
|
||||
{
|
||||
private readonly char _startingWithChar;
|
||||
public DynamicContractResolver(char startingWithChar)
|
||||
{
|
||||
_startingWithChar = startingWithChar;
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract)
|
||||
{
|
||||
IList<JsonProperty> properties = base.CreateProperties(contract);
|
||||
|
||||
// only serializer properties that start with the specified character
|
||||
properties =
|
||||
properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
|
||||
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
public class Book
|
||||
{
|
||||
public string BookName { get; set; }
|
||||
public decimal BookPrice { get; set; }
|
||||
public string AuthorName { get; set; }
|
||||
public int AuthorAge { get; set; }
|
||||
public string AuthorCountry { get; set; }
|
||||
}
|
||||
|
||||
public class ContractResolverTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void SingleTypeWithMultipleContractResolvers()
|
||||
{
|
||||
Book book = new Book
|
||||
{
|
||||
BookName = "The Gathering Storm",
|
||||
BookPrice = 16.19m,
|
||||
AuthorName = "Brandon Sanderson",
|
||||
AuthorAge = 34,
|
||||
AuthorCountry = "United States of America"
|
||||
};
|
||||
|
||||
string startingWithA = JsonConvert.SerializeObject(book, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') });
|
||||
|
||||
// {
|
||||
// "AuthorName": "Brandon Sanderson",
|
||||
// "AuthorAge": 34,
|
||||
// "AuthorCountry": "United States of America"
|
||||
// }
|
||||
|
||||
string startingWithB = JsonConvert.SerializeObject(book, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') });
|
||||
|
||||
// {
|
||||
// "BookName": "The Gathering Storm",
|
||||
// "BookPrice": 16.19
|
||||
// }
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""AuthorName"": ""Brandon Sanderson"",
|
||||
""AuthorAge"": 34,
|
||||
""AuthorCountry"": ""United States of America""
|
||||
}", startingWithA);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""BookName"": ""The Gathering Storm"",
|
||||
""BookPrice"": 16.19
|
||||
}", startingWithB);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user