-Added ISerializationBinder

-Added SerializationBinder properties to JsonSerializer and JsonSerializerSettings
-Changed Binder properties on JsonSerializer and JsonSerializerSettings to obsolete
This commit is contained in:
James Newton-King
2016-09-16 16:10:13 +12:00
parent a0403492ad
commit 4f8a22594f
19 changed files with 358 additions and 39 deletions
@@ -1178,8 +1178,23 @@ namespace Newtonsoft.Json.Tests.Serialization
JsonSerializer serializer = new JsonSerializer();
DefaultSerializationBinder customBinder = new DefaultSerializationBinder();
#pragma warning disable CS0618 // Type or member is obsolete
serializer.Binder = customBinder;
Assert.AreEqual(customBinder, serializer.Binder);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsInstanceOf(typeof(SerializationBinderAdapter), serializer.SerializationBinder);
serializer.SerializationBinder = customBinder;
Assert.AreEqual(customBinder, serializer.SerializationBinder);
ExceptionAssert.Throws<InvalidOperationException>(() =>
{
#pragma warning disable CS0618 // Type or member is obsolete
var serializationBinder = serializer.Binder;
#pragma warning restore CS0618 // Type or member is obsolete
serializationBinder.ToString();
}, "Cannot get SerializationBinder because an ISerializationBinder was previously set.");
serializer.CheckAdditionalContent = true;
Assert.AreEqual(true, serializer.CheckAdditionalContent);
@@ -1273,8 +1288,10 @@ namespace Newtonsoft.Json.Tests.Serialization
JsonSerializerSettings settings = new JsonSerializerSettings();
DefaultSerializationBinder customBinder = new DefaultSerializationBinder();
#pragma warning disable CS0618 // Type or member is obsolete
settings.Binder = customBinder;
Assert.AreEqual(customBinder, settings.Binder);
#pragma warning restore CS0618 // Type or member is obsolete
settings.CheckAdditionalContent = true;
Assert.AreEqual(true, settings.CheckAdditionalContent);
@@ -1374,8 +1391,23 @@ namespace Newtonsoft.Json.Tests.Serialization
JsonSerializerProxy serializerProxy = new JsonSerializerProxy(new JsonSerializerInternalReader(new JsonSerializer()));
DefaultSerializationBinder customBinder = new DefaultSerializationBinder();
#pragma warning disable CS0618 // Type or member is obsolete
serializerProxy.Binder = customBinder;
Assert.AreEqual(customBinder, serializerProxy.Binder);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsInstanceOf(typeof(SerializationBinderAdapter), serializerProxy.SerializationBinder);
serializerProxy.SerializationBinder = customBinder;
Assert.AreEqual(customBinder, serializerProxy.SerializationBinder);
ExceptionAssert.Throws<InvalidOperationException>(() =>
{
#pragma warning disable CS0618 // Type or member is obsolete
var serializationBinder = serializerProxy.Binder;
#pragma warning restore CS0618 // Type or member is obsolete
serializationBinder.ToString();
}, "Cannot get SerializationBinder because an ISerializationBinder was previously set.");
serializerProxy.CheckAdditionalContent = true;
Assert.AreEqual(true, serializerProxy.CheckAdditionalContent);
@@ -731,7 +731,9 @@ namespace Newtonsoft.Json.Tests.Serialization
object p = JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
#pragma warning disable CS0618 // Type or member is obsolete
Binder = new CustomSerializationBinder()
#pragma warning restore CS0618 // Type or member is obsolete
});
CustomAssert.IsInstanceOfType(typeof(Person), p);
@@ -772,7 +774,9 @@ namespace Newtonsoft.Json.Tests.Serialization
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
#pragma warning disable CS0618 // Type or member is obsolete
Binder = binder
#pragma warning restore CS0618 // Type or member is obsolete
});
//[
@@ -804,7 +808,9 @@ namespace Newtonsoft.Json.Tests.Serialization
IList<object> newValues = JsonConvert.DeserializeObject<IList<object>>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
#pragma warning disable CS0618 // Type or member is obsolete
Binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests")
#pragma warning restore CS0618 // Type or member is obsolete
});
CustomAssert.IsInstanceOfType(typeof(Customer), newValues[0]);
@@ -840,6 +846,95 @@ namespace Newtonsoft.Json.Tests.Serialization
}
#endif
[Test]
public void NewSerializeUsingCustomBinder()
{
NewTypeNameSerializationBinder binder = new NewTypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests");
IList<object> values = new List<object>
{
new Customer
{
Name = "Caroline Customer"
},
new Purchase
{
ProductName = "Elbow Grease",
Price = 5.99m,
Quantity = 1
}
};
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
SerializationBinder = binder
});
//[
// {
// "$type": "Customer",
// "Name": "Caroline Customer"
// },
// {
// "$type": "Purchase",
// "ProductName": "Elbow Grease",
// "Price": 5.99,
// "Quantity": 1
// }
//]
StringAssert.AreEqual(@"[
{
""$type"": ""Customer"",
""Name"": ""Caroline Customer""
},
{
""$type"": ""Purchase"",
""ProductName"": ""Elbow Grease"",
""Price"": 5.99,
""Quantity"": 1
}
]", json);
IList<object> newValues = JsonConvert.DeserializeObject<IList<object>>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
SerializationBinder = new NewTypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests")
});
CustomAssert.IsInstanceOfType(typeof(Customer), newValues[0]);
Customer customer = (Customer)newValues[0];
Assert.AreEqual("Caroline Customer", customer.Name);
CustomAssert.IsInstanceOfType(typeof(Purchase), newValues[1]);
Purchase purchase = (Purchase)newValues[1];
Assert.AreEqual("Elbow Grease", purchase.ProductName);
}
public class NewTypeNameSerializationBinder : ISerializationBinder
{
public string TypeFormat { get; private set; }
public NewTypeNameSerializationBinder(string typeFormat)
{
TypeFormat = typeFormat;
}
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = null;
typeName = serializedType.Name;
}
public Type BindToType(string assemblyName, string typeName)
{
string resolvedTypeName = string.Format(TypeFormat, typeName);
return Type.GetType(resolvedTypeName, true);
}
}
[Test]
public void CollectionWithAbstractItems()
{
@@ -1193,7 +1288,9 @@ namespace Newtonsoft.Json.Tests.Serialization
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full,
#pragma warning disable CS0618 // Type or member is obsolete
Binder = new MetroBinder(),
#pragma warning restore CS0618 // Type or member is obsolete
ContractResolver = new DefaultContractResolver
{
#if !(NETFX_CORE || PORTABLE || DNXCORE50)