Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
28 lines (25 sloc)
938 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Reflection; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
namespace HttpPatchSample.Models | |
{ | |
/// <summary> | |
/// Class that plugs in to Newtonsoft deserialization pipeline for classes descending from <see cref="PatchDtoBase"/>. | |
/// For all properties, that are present in JSON it calls <see cref="PatchDtoBase.SetHasProperty"/>.` | |
/// </summary> | |
public class PatchRequestContractResolver : DefaultContractResolver | |
{ | |
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | |
{ | |
var prop = base.CreateProperty(member, memberSerialization); | |
prop.SetIsSpecified += (o, o1) => | |
{ | |
if (o is PatchDtoBase patchDtoBase) | |
{ | |
patchDtoBase.SetHasProperty(prop.PropertyName); | |
} | |
}; | |
return prop; | |
} | |
} | |
} |