Created
September 26, 2015 17:03
-
-
Save gpltaylor/33b5e6056c1d59e83b1a to your computer and use it in GitHub Desktop.
Sample ContractResolver that conditionally removes properties from JSON. This can be plumbed into ASP.NET/API
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Linq | |
{ | |
using AutoMapper; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
using System.Reflection; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Mapper.CreateMap<Person, Member>() | |
.ForMember(dest => dest.dob, opt => opt.MapFrom(src => src.Age)); | |
//.ForMember(dest => dest.Age, opt => opt.Condition(src => (src.Age >= 20))); | |
//.ForMember(c => c.FirstName, option => option.Ignore()); | |
var s = new ShouldSerializeContractResolver(); | |
var p = new Person() { | |
FirstName = "Garry", Surname = "Taylor", Age = 19, | |
isMaxLoan = true, isMaxValuation = false, isMinLoan = false, isMinValuation = true | |
}; | |
Member dto = Mapper.Map<Member>(p); | |
var memberJson = Newtonsoft.Json.JsonConvert.SerializeObject(dto, | |
new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver() }); | |
Console.WriteLine(memberJson); | |
Console.ReadLine(); | |
} | |
} | |
class Person | |
{ | |
public String FirstName { get; set; } | |
public String Surname { get; set; } | |
public int Age { get; set; } | |
public bool? isMinLoan { get; set; } | |
public bool? isMaxLoan { get; set; } | |
public bool? isMinValuation { get; set; } | |
public bool? isMaxValuation { get; set; } | |
} | |
class Member | |
{ | |
public String FirstName { get; set; } | |
public String Surname { get; set; } | |
public int? dob { get; set; } | |
[JsonIgnore] | |
public String fullname { get; set; } | |
public bool? isMinLoan { get; set; } | |
public bool? isMaxLoan { get; set; } | |
public bool? isMinValuation { get; set; } | |
public bool? isMaxValuation { get; set; } | |
} | |
public class ShouldSerializeContractResolver : DefaultContractResolver | |
{ | |
public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver(); | |
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | |
{ | |
JsonProperty property = base.CreateProperty(member, memberSerialization); | |
if (property.DeclaringType == typeof(Member) && property.PropertyName.StartsWith("is")) | |
{ | |
property.ShouldSerialize = | |
instance => | |
{ | |
Member e = (Member)instance; | |
var value = (bool?) e.GetType().GetProperty(property.PropertyName).GetValue(e, null); | |
return value == false; | |
}; | |
} | |
return property; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment