-Fixed deserializer attempting to populate JSON onto an object that was created with a non-default constructor

-Added GetResolvedPropertyName to DefaultContractResolver
This commit is contained in:
James Newton-King
2012-05-27 21:29:02 +12:00
parent 2fbf5f7808
commit aef0759038
9 changed files with 132 additions and 26 deletions
@@ -1434,6 +1434,31 @@ keyword such as type of business.""
Assert.AreEqual(@"{""SubProp"":""my subprop"",""SuperProp"":""overrided superprop""}", newJson); Assert.AreEqual(@"{""SubProp"":""my subprop"",""SuperProp"":""overrided superprop""}", newJson);
} }
[Test]
public void DeserializePropertiesOnToNonDefaultConstructorWithReferenceTracking()
{
SubKlass i = new SubKlass("my subprop");
i.SuperProp = "overrided superprop";
string json = JsonConvert.SerializeObject(i, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
Assert.AreEqual(@"{""$id"":""1"",""SubProp"":""my subprop"",""SuperProp"":""overrided superprop""}", json);
SubKlass ii = JsonConvert.DeserializeObject<SubKlass>(json, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
string newJson = JsonConvert.SerializeObject(ii, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
Assert.AreEqual(@"{""$id"":""1"",""SubProp"":""my subprop"",""SuperProp"":""overrided superprop""}", newJson);
}
[Test] [Test]
public void SerializeJsonPropertyWithHandlingValues() public void SerializeJsonPropertyWithHandlingValues()
{ {
@@ -2019,8 +2044,7 @@ keyword such as type of business.""
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.TestObjects.Person' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly. @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.TestObjects.Person' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type to an array or a type that implements a collection interface (e.g. IEnumerable, ICollection, IList) like List<T>. To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Another option is to add the [JsonArray] attribute to the type to force it to work with a JSON array.
Path '', line 1, position 1.", Path '', line 1, position 1.",
() => () =>
{ {
@@ -2035,8 +2059,7 @@ Path '', line 1, position 1.",
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly. @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type to an array or a type that implements a collection interface (e.g. IEnumerable, ICollection, IList) like List<T>. To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Another option is to add the [JsonArray] attribute to the type to force it to work with a JSON array.
Path '', line 1, position 1.", Path '', line 1, position 1.",
() => () =>
{ {
@@ -2052,8 +2075,7 @@ Path '', line 1, position 1.",
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Exception' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly. @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Exception' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type to an array or a type that implements a collection interface (e.g. IEnumerable, ICollection, IList) like List<T>. To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Another option is to add the [JsonArray] attribute to the type to force it to work with a JSON array.
Path '', line 1, position 1.", Path '', line 1, position 1.",
() => () =>
{ {
@@ -2068,9 +2090,8 @@ Path '', line 1, position 1.",
string json = @"[]"; string json = @"[]";
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Double' because the type requires a JSON primitive value (e.g. a string or a number) to deserialize correctly. @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type to an array or a type that implements a collection interface (e.g. IEnumerable, ICollection, IList) like List<T>. To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Another option is to add the [JsonArray] attribute to the type to force it to work with a JSON array.
Path '', line 1, position 1.", Path '', line 1, position 1.",
() => () =>
{ {
@@ -2086,8 +2107,7 @@ Path '', line 1, position 1.",
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.Linq.DynamicDictionary' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly. @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.Linq.DynamicDictionary' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type to an array or a type that implements a collection interface (e.g. IEnumerable, ICollection, IList) like List<T>. To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Another option is to add the [JsonArray] attribute to the type to force it to work with a JSON array.
Path '', line 1, position 1.", Path '', line 1, position 1.",
() => () =>
{ {
@@ -2142,8 +2162,7 @@ Path '', line 1, position 1.",
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. @"Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) or a dictionary type (e.g. Dictionary<TKey, TValue>). To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Another option is to add the [JsonObject] attribute to the type to force it to work with a JSON object.
Path '', line 1, position 2.", Path '', line 1, position 2.",
() => () =>
{ {
@@ -3037,7 +3056,8 @@ Path '', line 1, position 2.",
{ {
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"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. @"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.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path 'booleanValue', line 1, position 14.", 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.
Path 'booleanValue', line 1, position 14.",
() => () =>
{ {
JsonTypeReflector.SetFullyTrusted(false); JsonTypeReflector.SetFullyTrusted(false);
@@ -3058,7 +3078,7 @@ To fix this error either change the environment to be fully trusted, change the
{ {
ExceptionAssert.Throws<JsonSerializationException>( ExceptionAssert.Throws<JsonSerializationException>(
@"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. @"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.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path ''.", 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. Path ''.",
() => () =>
{ {
JsonTypeReflector.SetFullyTrusted(false); JsonTypeReflector.SetFullyTrusted(false);
@@ -6388,6 +6408,44 @@ Parameter name: value",
s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json)));
}); });
} }
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
[Test]
public void DeserializeException()
{
string json = @"{ ""ClassName"" : ""System.InvalidOperationException"",
""Data"" : null,
""ExceptionMethod"" : ""8\nLogin\nAppBiz, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null\nMyApp.LoginBiz\nMyApp.User Login()"",
""HResult"" : -2146233079,
""HelpURL"" : null,
""InnerException"" : { ""ClassName"" : ""System.Exception"",
""Data"" : null,
""ExceptionMethod"" : null,
""HResult"" : -2146233088,
""HelpURL"" : null,
""InnerException"" : null,
""Message"" : ""Inner exception..."",
""RemoteStackIndex"" : 0,
""RemoteStackTraceString"" : null,
""Source"" : null,
""StackTraceString"" : null,
""WatsonBuckets"" : null
},
""Message"" : ""Outter exception..."",
""RemoteStackIndex"" : 0,
""RemoteStackTraceString"" : null,
""Source"" : ""AppBiz"",
""StackTraceString"" : "" at MyApp.LoginBiz.Login() in C:\\MyApp\\LoginBiz.cs:line 44\r\n at MyApp.LoginSvc.Login() in C:\\MyApp\\LoginSvc.cs:line 71\r\n at SyncInvokeLogin(Object , Object[] , Object[] )\r\n at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)\r\n at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"",
""WatsonBuckets"" : null
}";
InvalidOperationException exception = JsonConvert.DeserializeObject<InvalidOperationException>(json);
Assert.IsNotNull(exception);
CustomAssert.IsInstanceOfType(typeof(InvalidOperationException), exception);
Assert.AreEqual("Outter exception...", exception.Message);
}
#endif
} }
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
@@ -50,6 +50,22 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("James", p.Name); Assert.AreEqual("James", p.Name);
} }
[Test]
public void PopulateArray()
{
IList<Person> people = new List<Person>
{
new Person { Name = "Initial" }
};
JsonConvert.PopulateObject(@"[{""Name"":""James""}, null]", people);
Assert.AreEqual(3, people.Count);
Assert.AreEqual("Initial", people[0].Name);
Assert.AreEqual("James", people[1].Name);
Assert.AreEqual(null, people[2]);
}
[Test] [Test]
public void PopulateStore() public void PopulateStore()
{ {
@@ -52,7 +52,7 @@ namespace Newtonsoft.Json.Converters
foreach (DataTable table in dataSet.Tables) foreach (DataTable table in dataSet.Tables)
{ {
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(table.TableName) : table.TableName); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(table.TableName) : table.TableName);
converter.WriteJson(writer, table, serializer); converter.WriteJson(writer, table, serializer);
} }
@@ -56,7 +56,7 @@ namespace Newtonsoft.Json.Converters
if (serializer.NullValueHandling == NullValueHandling.Ignore && (row[column] == null || row[column] == DBNull.Value)) if (serializer.NullValueHandling == NullValueHandling.Ignore && (row[column] == null || row[column] == DBNull.Value))
continue; continue;
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(column.ColumnName) : column.ColumnName); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(column.ColumnName) : column.ColumnName);
serializer.Serialize(writer, row[column]); serializer.Serialize(writer, row[column]);
} }
writer.WriteEndObject(); writer.WriteEndObject();
+6
View File
@@ -350,6 +350,12 @@ namespace Newtonsoft.Json
} }
} }
/// <summary>
/// Gets a value indicating whether there will be a check for additional JSON content after deserializing an object.
/// </summary>
/// <value>
/// <c>true</c> if there will be a check for additional JSON content after deserializing an object; otherwise, <c>false</c>.
/// </value>
public virtual bool CheckAdditionalContent public virtual bool CheckAdditionalContent
{ {
get { return _checkAdditionalContent ?? JsonSerializerSettings.DefaultCheckAdditionalContent; } get { return _checkAdditionalContent ?? JsonSerializerSettings.DefaultCheckAdditionalContent; }
@@ -216,6 +216,12 @@ namespace Newtonsoft.Json
set { _culture = value; } set { _culture = value; }
} }
/// <summary>
/// Gets a value indicating whether there will be a check for additional content after deserializing an object.
/// </summary>
/// <value>
/// <c>true</c> if there will be a check for additional content after deserializing an object; otherwise, <c>false</c>.
/// </value>
public bool CheckAdditionalContent public bool CheckAdditionalContent
{ {
get { return _checkAdditionalContent ?? DefaultCheckAdditionalContent; } get { return _checkAdditionalContent ?? DefaultCheckAdditionalContent; }
@@ -1067,5 +1067,17 @@ namespace Newtonsoft.Json.Serialization
{ {
return propertyName; return propertyName;
} }
/// <summary>
/// Gets the resolved name of the property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>Name of the property.</returns>
public string GetResolvedPropertyName(string propertyName)
{
// this is a new method rather than changing the visibility of ResolvePropertyName to avoid
// a breaking change for anyone who has overidden the method
return ResolvePropertyName(propertyName);
}
} }
} }
@@ -286,7 +286,7 @@ namespace Newtonsoft.Json.Serialization
case JsonContractType.Array: case JsonContractType.Array:
return @"JSON array (e.g. [1,2,3])"; return @"JSON array (e.g. [1,2,3])";
case JsonContractType.Primitive: case JsonContractType.Primitive:
return @"JSON primitive value (e.g. a string or a number)"; return @"JSON primitive value (e.g. string, number, boolean, null)";
case JsonContractType.String: case JsonContractType.String:
return @"JSON string value"; return @"JSON string value";
default: default:
@@ -344,12 +344,17 @@ namespace Newtonsoft.Json.Serialization
switch (contract.ContractType) switch (contract.ContractType)
{ {
case JsonContractType.Object: case JsonContractType.Object:
bool createdFromNonDefaultConstructor = false;
JsonObjectContract objectContract = (JsonObjectContract) contract; JsonObjectContract objectContract = (JsonObjectContract) contract;
object targetObject; object targetObject;
if (existingValue != null) if (existingValue != null)
targetObject = existingValue; targetObject = existingValue;
else else
targetObject = CreateNewObject(reader, objectContract, member, containerMember, id); targetObject = CreateNewObject(reader, objectContract, member, containerMember, id, out createdFromNonDefaultConstructor);
// don't populate if read from non-default constructor because the object has already been read
if (createdFromNonDefaultConstructor)
return targetObject;
return PopulateObject(targetObject, reader, objectContract, member, id); return PopulateObject(targetObject, reader, objectContract, member, id);
case JsonContractType.Primitive: case JsonContractType.Primitive:
@@ -386,8 +391,7 @@ namespace Newtonsoft.Json.Serialization
} }
throw JsonSerializationException.Create(reader, @"Cannot deserialize the current JSON object (e.g. {{""name"":""value""}}) into type '{0}' because the type requires a {1} to deserialize correctly. throw JsonSerializationException.Create(reader, @"Cannot deserialize the current JSON object (e.g. {{""name"":""value""}}) into type '{0}' because the type requires a {1} to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) or a dictionary type (e.g. Dictionary<TKey, TValue>). To fix this error either change the JSON to a {1} or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Another option is to add the [JsonObject] attribute to the type to force it to work with a JSON object.
".FormatWith(CultureInfo.InvariantCulture, objectType, GetExpectedDescription(contract))); ".FormatWith(CultureInfo.InvariantCulture, objectType, GetExpectedDescription(contract)));
} }
@@ -518,8 +522,7 @@ Another option is to add the [JsonObject] attribute to the type to force it to w
JsonArrayContract arrayContract = contract as JsonArrayContract; JsonArrayContract arrayContract = contract as JsonArrayContract;
if (arrayContract == null) if (arrayContract == null)
throw JsonSerializationException.Create(reader, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '{0}' because the type requires a {1} to deserialize correctly. throw JsonSerializationException.Create(reader, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '{0}' because the type requires a {1} to deserialize correctly.
To fix this error either change the JSON to what is required or change the deserialized type to an array or a type that implements a collection interface (e.g. IEnumerable, ICollection, IList) like List<T>. To fix this error either change the JSON to a {1} or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Another option is to add the [JsonArray] attribute to the type to force it to work with a JSON array.
".FormatWith(CultureInfo.InvariantCulture, objectType, GetExpectedDescription(contract))); ".FormatWith(CultureInfo.InvariantCulture, objectType, GetExpectedDescription(contract)));
return arrayContract; return arrayContract;
@@ -939,7 +942,7 @@ Another option is to add the [JsonArray] attribute to the type to force it to wo
if (!JsonTypeReflector.FullyTrusted) if (!JsonTypeReflector.FullyTrusted)
{ {
throw JsonSerializationException.Create(reader, @"Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data. throw JsonSerializationException.Create(reader, @"Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. 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.
".FormatWith(CultureInfo.InvariantCulture, objectType)); ".FormatWith(CultureInfo.InvariantCulture, objectType));
} }
@@ -1262,7 +1265,7 @@ To fix this error either change the environment to be fully trusted, change the
return (reader.TokenType != JsonToken.None); return (reader.TokenType != JsonToken.None);
} }
public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id) public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id, out bool createdFromNonDefaultConstructor)
{ {
object newObject = null; object newObject = null;
@@ -1272,7 +1275,10 @@ To fix this error either change the environment to be fully trusted, change the
if (objectContract.OverrideConstructor != null) if (objectContract.OverrideConstructor != null)
{ {
if (objectContract.OverrideConstructor.GetParameters().Length > 0) if (objectContract.OverrideConstructor.GetParameters().Length > 0)
{
createdFromNonDefaultConstructor = true;
return CreateObjectFromNonDefaultConstructor(reader, objectContract, containerMember, objectContract.OverrideConstructor, id); return CreateObjectFromNonDefaultConstructor(reader, objectContract, containerMember, objectContract.OverrideConstructor, id);
}
newObject = objectContract.OverrideConstructor.Invoke(null); newObject = objectContract.OverrideConstructor.Invoke(null);
} }
@@ -1287,12 +1293,14 @@ To fix this error either change the environment to be fully trusted, change the
} }
else if (objectContract.ParametrizedConstructor != null) else if (objectContract.ParametrizedConstructor != null)
{ {
createdFromNonDefaultConstructor = true;
return CreateObjectFromNonDefaultConstructor(reader, objectContract, containerMember, objectContract.ParametrizedConstructor, id); return CreateObjectFromNonDefaultConstructor(reader, objectContract, containerMember, objectContract.ParametrizedConstructor, id);
} }
if (newObject == null) if (newObject == null)
throw JsonSerializationException.Create(reader, "Unable to find a constructor to use for type {0}. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType)); throw JsonSerializationException.Create(reader, "Unable to find a constructor to use for type {0}. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType));
createdFromNonDefaultConstructor = false;
return newObject; return newObject;
} }
@@ -521,7 +521,7 @@ namespace Newtonsoft.Json.Serialization
if (!JsonTypeReflector.FullyTrusted) if (!JsonTypeReflector.FullyTrusted)
{ {
throw JsonSerializationException.Create(null, writer.ContainerPath, @"Type '{0}' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data. throw JsonSerializationException.Create(null, writer.ContainerPath, @"Type '{0}' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null); 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.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
} }
contract.InvokeOnSerializing(value, Serializer.Context); contract.InvokeOnSerializing(value, Serializer.Context);