-Changed ISerializable serialization to require SerializableAttribute

This commit is contained in:
James Newton-King
2017-11-04 17:53:17 +13:00
parent 1e612287b3
commit 48be21d4ce
8 changed files with 33 additions and 3 deletions
@@ -125,6 +125,15 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(JsonContractType.Serializable, contract.ContractType); Assert.AreEqual(JsonContractType.Serializable, contract.ContractType);
} }
[Test]
public void ResolveSerializableWithoutAttributeContract()
{
DefaultContractResolver contractResolver = new DefaultContractResolver();
JsonContract contract = contractResolver.ResolveContract(typeof(ISerializableWithoutAttributeTestObject));
Assert.AreEqual(JsonContractType.Object, contract.ContractType);
}
[Test] [Test]
public void ResolveObjectContractWithFieldsSerialization() public void ResolveObjectContractWithFieldsSerialization()
{ {
@@ -152,6 +152,7 @@ namespace Newtonsoft.Json.Tests.Serialization
} }
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_3 || NETSTANDARD2_0 #if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_3 || NETSTANDARD2_0
[Serializable]
public class MainClass : ISerializable public class MainClass : ISerializable
{ {
public ChildClass Child { get; set; } public ChildClass Child { get; set; }
@@ -162,6 +163,7 @@ namespace Newtonsoft.Json.Tests.Serialization
} }
} }
[Serializable]
public class ChildClass : ISerializable public class ChildClass : ISerializable
{ {
public string Name { get; set; } public string Name { get; set; }
@@ -29,6 +29,7 @@ using Newtonsoft.Json.Tests.TestObjects.Organization;
namespace Newtonsoft.Json.Tests.TestObjects namespace Newtonsoft.Json.Tests.TestObjects
{ {
[Serializable]
public class ISerializableTestObject : ISerializable public class ISerializableTestObject : ISerializable
{ {
internal string _stringValue; internal string _stringValue;
@@ -0,0 +1,13 @@
using System;
using System.Runtime.Serialization;
namespace Newtonsoft.Json.Tests.TestObjects
{
public class ISerializableWithoutAttributeTestObject : ISerializable
{
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException();
}
}
}
@@ -23,11 +23,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#endregion #endregion
using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Newtonsoft.Json.Tests.TestObjects namespace Newtonsoft.Json.Tests.TestObjects
{ {
#if !(PORTABLE || PORTABLE40 || DNXCORE50) || NETSTANDARD1_3 || NETSTANDARD2_0 #if !(PORTABLE || PORTABLE40 || DNXCORE50) || NETSTANDARD1_3 || NETSTANDARD2_0
[Serializable]
public class PreserveReferencesCallbackTestObject : ISerializable public class PreserveReferencesCallbackTestObject : ISerializable
{ {
internal string _stringValue; internal string _stringValue;
@@ -30,6 +30,7 @@ using System.Runtime.Serialization;
namespace Newtonsoft.Json.Tests.TestObjects namespace Newtonsoft.Json.Tests.TestObjects
{ {
#if !(DNXCORE50) || NETSTANDARD1_3 || NETSTANDARD2_0 #if !(DNXCORE50) || NETSTANDARD1_3 || NETSTANDARD2_0
[Serializable]
public struct Ratio : IConvertible, IFormattable, ISerializable public struct Ratio : IConvertible, IFormattable, ISerializable
{ {
private readonly int _numerator; private readonly int _numerator;
@@ -1171,7 +1171,7 @@ namespace Newtonsoft.Json.Serialization
} }
#if HAVE_BINARY_SERIALIZATION #if HAVE_BINARY_SERIALIZATION
if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t)) if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t) && JsonTypeReflector.IsSerializable(t))
{ {
return CreateISerializableContract(objectType); return CreateISerializableContract(objectType);
} }
@@ -383,7 +383,8 @@ namespace Newtonsoft.Json.Serialization
public static bool IsNonSerializable(object provider) public static bool IsNonSerializable(object provider)
{ {
#if HAVE_FULL_REFLECTION #if HAVE_FULL_REFLECTION
return (GetCachedAttribute<NonSerializedAttribute>(provider) != null); // no inheritance
return (ReflectionUtils.GetAttribute<NonSerializedAttribute>(provider, false) != null);
#else #else
FieldInfo fieldInfo = provider as FieldInfo; FieldInfo fieldInfo = provider as FieldInfo;
if (fieldInfo != null && (fieldInfo.Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized) if (fieldInfo != null && (fieldInfo.Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized)
@@ -400,7 +401,8 @@ namespace Newtonsoft.Json.Serialization
public static bool IsSerializable(object provider) public static bool IsSerializable(object provider)
{ {
#if HAVE_FULL_REFLECTION #if HAVE_FULL_REFLECTION
return (GetCachedAttribute<SerializableAttribute>(provider) != null); // no inheritance
return (ReflectionUtils.GetAttribute<SerializableAttribute>(provider, false) != null);
#else #else
Type type = provider as Type; Type type = provider as Type;
if (type != null && (type.GetTypeInfo().Attributes & TypeAttributes.Serializable) == TypeAttributes.Serializable) if (type != null && (type.GetTypeInfo().Attributes & TypeAttributes.Serializable) == TypeAttributes.Serializable)