using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
public static void Main()
List<string> propertiesToSerialize = new List<string>(new string[]
ThirdPartyObject _objToSerialize = new ThirdPartyObject
MasterString = "Provider...",
OtherStuff = "This property should not appear in the output."
DynamicContractResolver contractResolver = new DynamicContractResolver(propertiesToSerialize);
string json = JsonConvert.SerializeObject(_objToSerialize, Formatting.Indented, new JsonSerializerSettings { ContractResolver = contractResolver });
public class DynamicContractResolver : CamelCasePropertyNamesContractResolver
private IList<string> _propertiesToSerialize = null;
public DynamicContractResolver(IList<string> propertiesToSerialize)
_propertiesToSerialize = propertiesToSerialize;
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization)
.Where(p => _propertiesToSerialize.Contains(p.PropertyName)).ToList();
foreach (JsonProperty prop in properties)
prop.Order = _propertiesToSerialize.IndexOf(prop.PropertyName) + 1;
return properties.OrderBy(p => p.Order).ToList();
public string MasterString { get; set; }
public int CruiseMode { get; set; }
public string Name { get; set; }
public string Account { get; set; }
public string OtherStuff { get; set; }