Merge branch 'vontangen-master'
This commit is contained in:
@@ -27,7 +27,14 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
#if NET20
|
||||||
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
|
#else
|
||||||
|
using System.Linq;
|
||||||
|
#endif
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
#if NETFX_CORE
|
#if NETFX_CORE
|
||||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||||
@@ -386,6 +393,45 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
}", json);
|
}", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ShouldDeserialize_True()
|
||||||
|
{
|
||||||
|
string json = @"{'HasName':true,'Name':'Name!'}";
|
||||||
|
|
||||||
|
MemoryTraceWriter traceWriter = new MemoryTraceWriter();
|
||||||
|
ShouldDeserializeTestClass c = JsonConvert.DeserializeObject<ShouldDeserializeTestClass>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
ContractResolver = ShouldDeserializeContractResolver.Instance,
|
||||||
|
TraceWriter = traceWriter
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.AreEqual(null, c.ExtensionData);
|
||||||
|
Assert.AreEqual(true, c.HasName);
|
||||||
|
Assert.AreEqual("Name!", c.Name);
|
||||||
|
|
||||||
|
Assert.IsTrue(traceWriter.GetTraceMessages().Any(m => m.EndsWith("Verbose ShouldDeserialize result for property 'Name' on Newtonsoft.Json.Tests.Serialization.ShouldDeserializeTestClass: True. Path 'Name'.")));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ShouldDeserialize_False()
|
||||||
|
{
|
||||||
|
string json = @"{'HasName':false,'Name':'Name!'}";
|
||||||
|
|
||||||
|
MemoryTraceWriter traceWriter = new MemoryTraceWriter();
|
||||||
|
ShouldDeserializeTestClass c = JsonConvert.DeserializeObject<ShouldDeserializeTestClass>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
ContractResolver = ShouldDeserializeContractResolver.Instance,
|
||||||
|
TraceWriter = traceWriter
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.AreEqual(1, c.ExtensionData.Count);
|
||||||
|
Assert.AreEqual("Name!", (string)c.ExtensionData["Name"]);
|
||||||
|
Assert.AreEqual(false, c.HasName);
|
||||||
|
Assert.AreEqual(null, c.Name);
|
||||||
|
|
||||||
|
Assert.IsTrue(traceWriter.GetTraceMessages().Any(m => m.EndsWith("Verbose ShouldDeserialize result for property 'Name' on Newtonsoft.Json.Tests.Serialization.ShouldDeserializeTestClass: False. Path 'Name'.")));
|
||||||
|
}
|
||||||
|
|
||||||
public class Employee
|
public class Employee
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
@@ -588,4 +634,39 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
return (name != null);
|
return (name != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ShouldDeserializeContractResolver : DefaultContractResolver
|
||||||
|
{
|
||||||
|
public static new readonly ShouldDeserializeContractResolver Instance = new ShouldDeserializeContractResolver();
|
||||||
|
|
||||||
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||||
|
{
|
||||||
|
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||||
|
|
||||||
|
MethodInfo shouldDeserializeMethodInfo = member.DeclaringType.GetMethod("ShouldDeserialize" + member.Name);
|
||||||
|
|
||||||
|
if (shouldDeserializeMethodInfo != null)
|
||||||
|
{
|
||||||
|
property.ShouldDeserialize = o =>
|
||||||
|
{
|
||||||
|
return (bool)shouldDeserializeMethodInfo.Invoke(o, null);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return property;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ShouldDeserializeTestClass
|
||||||
|
{
|
||||||
|
[JsonExtensionData]
|
||||||
|
public IDictionary<string, JToken> ExtensionData { get; set; }
|
||||||
|
public bool HasName { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public bool ShouldDeserializeName()
|
||||||
|
{
|
||||||
|
return HasName;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -236,6 +236,12 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
/// <value>A predicate used to determine whether the property should be serialize.</value>
|
/// <value>A predicate used to determine whether the property should be serialize.</value>
|
||||||
public Predicate<object> ShouldSerialize { get; set; }
|
public Predicate<object> ShouldSerialize { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a predicate used to determine whether the property should be deserialized.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>A predicate used to determine whether the property should be deserialized.</value>
|
||||||
|
public Predicate<object> ShouldDeserialize { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a predicate used to determine whether the property should be serialized.
|
/// Gets or sets a predicate used to determine whether the property should be serialized.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -2051,6 +2051,15 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!ShouldDeserialize(reader, property, newObject))
|
||||||
|
{
|
||||||
|
if (!reader.Read())
|
||||||
|
break;
|
||||||
|
|
||||||
|
SetExtensionData(contract, member, reader, memberName, newObject);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (property.PropertyContract == null)
|
if (property.PropertyContract == null)
|
||||||
property.PropertyContract = GetContractSafe(property.PropertyType);
|
property.PropertyContract = GetContractSafe(property.PropertyType);
|
||||||
|
|
||||||
@@ -2097,6 +2106,21 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
return newObject;
|
return newObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool ShouldDeserialize(JsonReader reader, JsonProperty property, object target)
|
||||||
|
{
|
||||||
|
if (property.ShouldDeserialize == null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
bool shouldDeserialize = property.ShouldDeserialize(target);
|
||||||
|
|
||||||
|
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
|
||||||
|
{
|
||||||
|
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, reader.Path, "ShouldDeserialize result for property '{0}' on {1}: {2}".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, property.DeclaringType, shouldDeserialize)), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return shouldDeserialize;
|
||||||
|
}
|
||||||
|
|
||||||
private bool CheckPropertyName(JsonReader reader, string memberName)
|
private bool CheckPropertyName(JsonReader reader, string memberName)
|
||||||
{
|
{
|
||||||
if (Serializer.MetadataPropertyHandling == MetadataPropertyHandling.ReadAhead)
|
if (Serializer.MetadataPropertyHandling == MetadataPropertyHandling.ReadAhead)
|
||||||
|
|||||||
Reference in New Issue
Block a user