-Fixed error attempting to deserialize into read-only collections from non-default constructor
This commit is contained in:
@@ -24,7 +24,9 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Newtonsoft.Json.Schema;
|
using Newtonsoft.Json.Schema;
|
||||||
@@ -50,7 +52,6 @@ using Test = Xunit.FactAttribute;
|
|||||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||||
#else
|
#else
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using System.Collections;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -1636,30 +1637,51 @@ namespace Newtonsoft.Json.Tests
|
|||||||
public void ShouldNotPopulateReadOnlyEnumerableObjectWithNonDefaultConstructor()
|
public void ShouldNotPopulateReadOnlyEnumerableObjectWithNonDefaultConstructor()
|
||||||
{
|
{
|
||||||
object actual = JsonConvert.DeserializeObject<HasReadOnlyEnumerableObject>("{\"foo\":{}}");
|
object actual = JsonConvert.DeserializeObject<HasReadOnlyEnumerableObject>("{\"foo\":{}}");
|
||||||
Assert.NotNull(actual);
|
Assert.IsNotNull(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ShouldNotPopulateReadOnlyEnumerableObjectWithDefaultConstructor()
|
public void ShouldNotPopulateReadOnlyEnumerableObjectWithDefaultConstructor()
|
||||||
{
|
{
|
||||||
object actual = JsonConvert.DeserializeObject<HasReadOnlyEnumerableObjectAndDefaultConstructor>("{\"foo\":{}}");
|
object actual = JsonConvert.DeserializeObject<HasReadOnlyEnumerableObjectAndDefaultConstructor>("{\"foo\":{}}");
|
||||||
Assert.NotNull(actual);
|
Assert.IsNotNull(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ShouldNotPopulateContructorArgumentEnumerableObject()
|
public void ShouldNotPopulateContructorArgumentEnumerableObject()
|
||||||
{
|
{
|
||||||
object actual = JsonConvert.DeserializeObject<AcceptsEnumerableObjectToConstructor>("{\"foo\":{}}");
|
object actual = JsonConvert.DeserializeObject<AcceptsEnumerableObjectToConstructor>("{\"foo\":{}}");
|
||||||
Assert.NotNull(actual);
|
Assert.IsNotNull(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ShouldNotPopulateEnumerableObjectProperty()
|
public void ShouldNotPopulateEnumerableObjectProperty()
|
||||||
{
|
{
|
||||||
object actual = JsonConvert.DeserializeObject<HasEnumerableObject>("{\"foo\":{}}");
|
object actual = JsonConvert.DeserializeObject<HasEnumerableObject>("{\"foo\":{}}");
|
||||||
Assert.NotNull(actual);
|
Assert.IsNotNull(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !(NET40 || NET35 || NET20)
|
||||||
|
[Test]
|
||||||
|
public void ShouldNotPopulateReadOnlyDictionaryObjectWithNonDefaultConstructor()
|
||||||
|
{
|
||||||
|
object actual = JsonConvert.DeserializeObject<HasReadOnlyDictionary>("{\"foo\":{'key':'value'}}");
|
||||||
|
Assert.IsNotNull(actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class HasReadOnlyDictionary
|
||||||
|
{
|
||||||
|
[JsonProperty("foo")]
|
||||||
|
public IReadOnlyDictionary<string, string> Foo { get; } = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>());
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
|
public HasReadOnlyDictionary([JsonProperty("bar")] int bar)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
public sealed class HasReadOnlyEnumerableObject
|
public sealed class HasReadOnlyEnumerableObject
|
||||||
{
|
{
|
||||||
[JsonProperty("foo")]
|
[JsonProperty("foo")]
|
||||||
|
|||||||
@@ -98,6 +98,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp1.0'">
|
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp1.0'">
|
||||||
<PackageReference Include="FSharp.Core" Version="4.1.0" />
|
<PackageReference Include="FSharp.Core" Version="4.1.0" />
|
||||||
|
<PackageReference Include="System.ObjectModel" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
|
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
|
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
|
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
|
||||||
|
|||||||
@@ -5031,7 +5031,7 @@ Path '', line 1, position 1.");
|
|||||||
JsonTypeReflector.SetFullyTrusted(false);
|
JsonTypeReflector.SetFullyTrusted(false);
|
||||||
|
|
||||||
JsonConvert.DeserializeObject<ISerializableTestObject>("{booleanValue:true}");
|
JsonConvert.DeserializeObject<ISerializableTestObject>("{booleanValue:true}");
|
||||||
}, @"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data." + Environment.NewLine +
|
}, @"Type 'Newtonsoft.Json.Tests.Serialization.ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data." + Environment.NewLine +
|
||||||
@"To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true." + Environment.NewLine +
|
@"To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true." + Environment.NewLine +
|
||||||
@"Path 'booleanValue', line 1, position 14.");
|
@"Path 'booleanValue', line 1, position 14.");
|
||||||
}
|
}
|
||||||
@@ -5052,7 +5052,7 @@ Path '', line 1, position 1.");
|
|||||||
ISerializableTestObject value = new ISerializableTestObject("string!", 0, default(DateTimeOffset), null);
|
ISerializableTestObject value = new ISerializableTestObject("string!", 0, default(DateTimeOffset), null);
|
||||||
|
|
||||||
JsonConvert.SerializeObject(value);
|
JsonConvert.SerializeObject(value);
|
||||||
}, @"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data." + Environment.NewLine +
|
}, @"Type 'Newtonsoft.Json.Tests.Serialization.ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data." + Environment.NewLine +
|
||||||
@"To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true." + Environment.NewLine +
|
@"To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true." + Environment.NewLine +
|
||||||
@"Path ''.");
|
@"Path ''.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2032,11 +2032,13 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
JsonArrayContract propertyArrayContract = (JsonArrayContract)propertyContract;
|
JsonArrayContract propertyArrayContract = (JsonArrayContract)propertyContract;
|
||||||
|
|
||||||
|
if (propertyArrayContract.CanDeserialize)
|
||||||
|
{
|
||||||
object createdObjectCollection = property.ValueProvider.GetValue(createdObject);
|
object createdObjectCollection = property.ValueProvider.GetValue(createdObject);
|
||||||
if (createdObjectCollection != null)
|
if (createdObjectCollection != null)
|
||||||
{
|
{
|
||||||
IWrappedCollection createdObjectCollectionWrapper = propertyArrayContract.CreateWrapper(createdObjectCollection);
|
IList createdObjectCollectionWrapper = (propertyArrayContract.ShouldCreateWrapper) ? propertyArrayContract.CreateWrapper(createdObjectCollection) : (IList)createdObjectCollection;
|
||||||
IWrappedCollection newValues = propertyArrayContract.CreateWrapper(value);
|
IList newValues = (propertyArrayContract.ShouldCreateWrapper) ? propertyArrayContract.CreateWrapper(value) : (IList)value;
|
||||||
|
|
||||||
foreach (object newValue in newValues)
|
foreach (object newValue in newValues)
|
||||||
{
|
{
|
||||||
@@ -2044,10 +2046,13 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (propertyContract.ContractType == JsonContractType.Dictionary)
|
else if (propertyContract.ContractType == JsonContractType.Dictionary)
|
||||||
{
|
{
|
||||||
JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)propertyContract;
|
JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)propertyContract;
|
||||||
|
|
||||||
|
if (!dictionaryContract.IsReadOnlyOrFixedSize)
|
||||||
|
{
|
||||||
object createdObjectDictionary = property.ValueProvider.GetValue(createdObject);
|
object createdObjectDictionary = property.ValueProvider.GetValue(createdObject);
|
||||||
if (createdObjectDictionary != null)
|
if (createdObjectDictionary != null)
|
||||||
{
|
{
|
||||||
@@ -2070,6 +2075,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
context.Used = true;
|
context.Used = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user