Skip to content

Commit

Permalink
Fix Issue 1340. Bring back default behavior for JsonSerializer.Binder…
Browse files Browse the repository at this point in the history
… if not set. Added tests.
  • Loading branch information
shahabhijeet committed Jun 9, 2017
1 parent 5d0e84a commit 43e9bd3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
16 changes: 0 additions & 16 deletions Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,14 +1341,6 @@ public void JsonSerializerProperties()
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);

Expand Down Expand Up @@ -1578,14 +1570,6 @@ public void JsonSerializerProxyProperties()
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);

Expand Down
46 changes: 45 additions & 1 deletion Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
using System.Net;
using System.Runtime.Serialization;
using System.IO;
using System.Reflection;

namespace Newtonsoft.Json.Tests.Serialization
{
Expand Down Expand Up @@ -883,7 +884,7 @@ public override Type BindToType(string assemblyName, string typeName)
}
}
#endif

[Test]
public void NewSerializeUsingCustomBinder()
{
Expand Down Expand Up @@ -1317,6 +1318,7 @@ private void TestJsonSerializationRoundTrip(SerializableWrapper e, TypeNameHandl
#endif

#if !(NET20 || NET35)

[Test]
public void SerializationBinderWithFullName()
{
Expand Down Expand Up @@ -2122,6 +2124,48 @@ public void DeserializeNullableStructProperty_Auto()
StringAssert.AreEqual("Hello!", objWithMessage.Message.Value.Value);
}
#endif


#if !(NET20 || NET35)

[Test]
public void SerializerWithDefaultBinder()
{
var serializer = JsonSerializer.Create();
#pragma warning disable CS0618
Assert.NotNull(serializer.Binder);
StringAssert.Equals(typeof(DefaultSerializationBinder).FullName, serializer.Binder.GetType().FullName);
#pragma warning restore CS0618 // Type or member is obsolete
StringAssert.Equals(typeof(DefaultSerializationBinder).FullName, serializer.SerializationBinder.GetType().FullName);
}

[Test]
public void ObsoleteBinderThrowsIfISerializationBinderSet()
{
var serializer = JsonSerializer.Create(new JsonSerializerSettings() { SerializationBinder = new FancyBinder() });
#pragma warning disable CS0618
Assert.Throws<InvalidOperationException>(() => { var foo = serializer.Binder; });
#pragma warning restore CS0618 // Type or member is obsolete
StringAssert.Equals(typeof(FancyBinder).Name, serializer.SerializationBinder.GetType().Name);
}


public class FancyBinder : ISerializationBinder
{
string annotate = new string(':', 3);
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = string.Format("FancyAssemblyName=>{0}", Assembly.GetAssembly(serializedType)?.GetName().Name);
typeName = string.Format("{0}{1}{0}", annotate, serializedType.Name);
}

public Type BindToType(string assemblyName, string typeName)
{
return null;
}
}
#endif

}

public struct Message2
Expand Down
11 changes: 11 additions & 0 deletions Src/Newtonsoft.Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public virtual SerializationBinder Binder
return adapter.SerializationBinder;
}

//Check if Default Binder was set
if (_serializationBinder.GetType().IsAssignableFrom(typeof(DefaultSerializationBinder)))
{
adapter = new SerializationBinderAdapter(_serializationBinder as DefaultSerializationBinder);
}

if (adapter != null)
{
return adapter.SerializationBinder;
}

throw new InvalidOperationException("Cannot get SerializationBinder because an ISerializationBinder was previously set.");
}
set
Expand Down

0 comments on commit 43e9bd3

Please sign in to comment.