Fix blank extension data values with required properties and ctor (#1850)
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !NET20
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.Serialization;
|
||||
#if !(NET20 || NET35 || NET40 || PORTABLE40)
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if DNXCORE50
|
||||
using Xunit;
|
||||
using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Issues
|
||||
{
|
||||
[TestFixture]
|
||||
public class Issue1834 : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
string json = "{'foo':'test!'}";
|
||||
ItemWithJsonConstructor c = JsonConvert.DeserializeObject<ItemWithJsonConstructor>(json);
|
||||
|
||||
Assert.IsNull(c.ExtensionData);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_UnsetRequired()
|
||||
{
|
||||
string json = "{'foo':'test!'}";
|
||||
ItemWithJsonConstructorAndDefaultValue c = JsonConvert.DeserializeObject<ItemWithJsonConstructorAndDefaultValue>(json);
|
||||
|
||||
Assert.IsNull(c.ExtensionData);
|
||||
}
|
||||
|
||||
public class ItemWithJsonConstructor
|
||||
{
|
||||
[JsonExtensionData]
|
||||
public IDictionary<string, JToken> ExtensionData;
|
||||
|
||||
[JsonConstructor]
|
||||
private ItemWithJsonConstructor(string foo)
|
||||
{
|
||||
Foo = foo;
|
||||
}
|
||||
|
||||
[JsonProperty(PropertyName = "foo", Required = Required.Always)]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
public class ItemWithJsonConstructorAndDefaultValue
|
||||
{
|
||||
[JsonExtensionData]
|
||||
public IDictionary<string, JToken> ExtensionData;
|
||||
|
||||
[JsonConstructor]
|
||||
private ItemWithJsonConstructorAndDefaultValue(string foo)
|
||||
{
|
||||
Foo = foo;
|
||||
}
|
||||
|
||||
[JsonProperty("foo")]
|
||||
public string Foo { get; set; }
|
||||
|
||||
[JsonProperty(PropertyName = "bar", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
|
||||
[System.ComponentModel.DefaultValue("default")]
|
||||
public string Bar { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -35,6 +35,7 @@ using System.Xml;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
#if HAVE_XLINQ
|
||||
using System.Xml.Linq;
|
||||
|
||||
@@ -522,6 +523,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="value">The object to serialize.</param>
|
||||
/// <returns>A JSON string representation of the object.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value)
|
||||
{
|
||||
return SerializeObject(value, null, (JsonSerializerSettings)null);
|
||||
@@ -535,6 +537,7 @@ namespace Newtonsoft.Json
|
||||
/// <returns>
|
||||
/// A JSON string representation of the object.
|
||||
/// </returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, Formatting formatting)
|
||||
{
|
||||
return SerializeObject(value, formatting, (JsonSerializerSettings)null);
|
||||
@@ -546,6 +549,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The object to serialize.</param>
|
||||
/// <param name="converters">A collection of converters used while serializing.</param>
|
||||
/// <returns>A JSON string representation of the object.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, params JsonConverter[] converters)
|
||||
{
|
||||
JsonSerializerSettings settings = (converters != null && converters.Length > 0)
|
||||
@@ -562,6 +566,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="formatting">Indicates how the output should be formatted.</param>
|
||||
/// <param name="converters">A collection of converters used while serializing.</param>
|
||||
/// <returns>A JSON string representation of the object.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, Formatting formatting, params JsonConverter[] converters)
|
||||
{
|
||||
JsonSerializerSettings settings = (converters != null && converters.Length > 0)
|
||||
@@ -580,6 +585,7 @@ namespace Newtonsoft.Json
|
||||
/// <returns>
|
||||
/// A JSON string representation of the object.
|
||||
/// </returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, JsonSerializerSettings settings)
|
||||
{
|
||||
return SerializeObject(value, null, settings);
|
||||
@@ -599,6 +605,7 @@ namespace Newtonsoft.Json
|
||||
/// <returns>
|
||||
/// A JSON string representation of the object.
|
||||
/// </returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, Type type, JsonSerializerSettings settings)
|
||||
{
|
||||
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
|
||||
@@ -616,6 +623,7 @@ namespace Newtonsoft.Json
|
||||
/// <returns>
|
||||
/// A JSON string representation of the object.
|
||||
/// </returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings)
|
||||
{
|
||||
return SerializeObject(value, null, formatting, settings);
|
||||
@@ -636,6 +644,7 @@ namespace Newtonsoft.Json
|
||||
/// <returns>
|
||||
/// A JSON string representation of the object.
|
||||
/// </returns>
|
||||
[DebuggerStepThrough]
|
||||
public static string SerializeObject(object value, Type type, Formatting formatting, JsonSerializerSettings settings)
|
||||
{
|
||||
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
|
||||
@@ -665,6 +674,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON to deserialize.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static object DeserializeObject(string value)
|
||||
{
|
||||
return DeserializeObject(value, null, (JsonSerializerSettings)null);
|
||||
@@ -679,6 +689,7 @@ namespace Newtonsoft.Json
|
||||
/// If this is <c>null</c>, default serialization settings will be used.
|
||||
/// </param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static object DeserializeObject(string value, JsonSerializerSettings settings)
|
||||
{
|
||||
return DeserializeObject(value, null, settings);
|
||||
@@ -690,6 +701,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The JSON to deserialize.</param>
|
||||
/// <param name="type">The <see cref="Type"/> of object being deserialized.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static object DeserializeObject(string value, Type type)
|
||||
{
|
||||
return DeserializeObject(value, type, (JsonSerializerSettings)null);
|
||||
@@ -701,6 +713,7 @@ namespace Newtonsoft.Json
|
||||
/// <typeparam name="T">The type of the object to deserialize to.</typeparam>
|
||||
/// <param name="value">The JSON to deserialize.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static T DeserializeObject<T>(string value)
|
||||
{
|
||||
return DeserializeObject<T>(value, (JsonSerializerSettings)null);
|
||||
@@ -717,6 +730,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The JSON to deserialize.</param>
|
||||
/// <param name="anonymousTypeObject">The anonymous type object.</param>
|
||||
/// <returns>The deserialized anonymous type from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static T DeserializeAnonymousType<T>(string value, T anonymousTypeObject)
|
||||
{
|
||||
return DeserializeObject<T>(value);
|
||||
@@ -737,6 +751,7 @@ namespace Newtonsoft.Json
|
||||
/// If this is <c>null</c>, default serialization settings will be used.
|
||||
/// </param>
|
||||
/// <returns>The deserialized anonymous type from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static T DeserializeAnonymousType<T>(string value, T anonymousTypeObject, JsonSerializerSettings settings)
|
||||
{
|
||||
return DeserializeObject<T>(value, settings);
|
||||
@@ -749,6 +764,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The JSON to deserialize.</param>
|
||||
/// <param name="converters">Converters to use while deserializing.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static T DeserializeObject<T>(string value, params JsonConverter[] converters)
|
||||
{
|
||||
return (T)DeserializeObject(value, typeof(T), converters);
|
||||
@@ -764,6 +780,7 @@ namespace Newtonsoft.Json
|
||||
/// If this is <c>null</c>, default serialization settings will be used.
|
||||
/// </param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static T DeserializeObject<T>(string value, JsonSerializerSettings settings)
|
||||
{
|
||||
return (T)DeserializeObject(value, typeof(T), settings);
|
||||
@@ -776,6 +793,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="type">The type of the object to deserialize.</param>
|
||||
/// <param name="converters">Converters to use while deserializing.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public static object DeserializeObject(string value, Type type, params JsonConverter[] converters)
|
||||
{
|
||||
JsonSerializerSettings settings = (converters != null && converters.Length > 0)
|
||||
@@ -820,6 +838,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON to populate values from.</param>
|
||||
/// <param name="target">The target object to populate values onto.</param>
|
||||
[DebuggerStepThrough]
|
||||
public static void PopulateObject(string value, object target)
|
||||
{
|
||||
PopulateObject(value, target, null);
|
||||
|
||||
@@ -790,6 +790,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="TextReader"/> that contains the JSON structure to read values from.</param>
|
||||
/// <param name="target">The target object to populate values onto.</param>
|
||||
[DebuggerStepThrough]
|
||||
public void Populate(TextReader reader, object target)
|
||||
{
|
||||
Populate(new JsonTextReader(reader), target);
|
||||
@@ -800,6 +801,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> that contains the JSON structure to read values from.</param>
|
||||
/// <param name="target">The target object to populate values onto.</param>
|
||||
[DebuggerStepThrough]
|
||||
public void Populate(JsonReader reader, object target)
|
||||
{
|
||||
PopulateInternal(reader, target);
|
||||
@@ -839,6 +841,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> that contains the JSON structure to deserialize.</param>
|
||||
/// <returns>The <see cref="Object"/> being deserialized.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public object Deserialize(JsonReader reader)
|
||||
{
|
||||
return Deserialize(reader, null);
|
||||
@@ -851,6 +854,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="reader">The <see cref="TextReader"/> containing the object.</param>
|
||||
/// <param name="objectType">The <see cref="Type"/> of object being deserialized.</param>
|
||||
/// <returns>The instance of <paramref name="objectType"/> being deserialized.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public object Deserialize(TextReader reader, Type objectType)
|
||||
{
|
||||
return Deserialize(new JsonTextReader(reader), objectType);
|
||||
@@ -863,6 +867,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="reader">The <see cref="JsonReader"/> containing the object.</param>
|
||||
/// <typeparam name="T">The type of the object to deserialize.</typeparam>
|
||||
/// <returns>The instance of <typeparamref name="T"/> being deserialized.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public T Deserialize<T>(JsonReader reader)
|
||||
{
|
||||
return (T)Deserialize(reader, typeof(T));
|
||||
@@ -875,6 +880,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="reader">The <see cref="JsonReader"/> containing the object.</param>
|
||||
/// <param name="objectType">The <see cref="Type"/> of object being deserialized.</param>
|
||||
/// <returns>The instance of <paramref name="objectType"/> being deserialized.</returns>
|
||||
[DebuggerStepThrough]
|
||||
public object Deserialize(JsonReader reader, Type objectType)
|
||||
{
|
||||
return DeserializeInternal(reader, objectType);
|
||||
|
||||
@@ -1906,14 +1906,17 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
foreach (JsonProperty property in contract.Properties)
|
||||
{
|
||||
if (propertyContexts.All(p => p.Property != property))
|
||||
if (!property.Ignored)
|
||||
{
|
||||
propertyContexts.Add(new CreatorPropertyContext
|
||||
if (propertyContexts.All(p => p.Property != property))
|
||||
{
|
||||
Property = property,
|
||||
Name = property.PropertyName,
|
||||
Presence = PropertyPresence.None
|
||||
});
|
||||
propertyContexts.Add(new CreatorPropertyContext
|
||||
{
|
||||
Property = property,
|
||||
Name = property.PropertyName,
|
||||
Presence = PropertyPresence.None
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2076,7 +2079,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
foreach (CreatorPropertyContext propertyValue in propertyContexts)
|
||||
{
|
||||
if (!propertyValue.Used)
|
||||
if (!propertyValue.Used && propertyValue.Presence != PropertyPresence.None)
|
||||
{
|
||||
contract.ExtensionDataSetter(createdObject, propertyValue.Name, propertyValue.Value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user