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() .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(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; } } }