using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication6
public double First { get; set; }
public double Second { get; set; }
public Dictionary<double, Total> Dict1 { get; set; }
public Dictionary<double, Total> Dict2 { get; set; }
public double CustomProperty1 { get; set; }
public double CustomProperty2 { get; set; }
public class PredicateConverter<T> : JsonConverter
private readonly Func<T, bool> _strategy;
public PredicateConverter(Func<T, bool> strategy)
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var result = _strategy((T)value);
JToken.FromObject(new { First = result, Second = !result }).WriteTo(writer);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
throw new NotImplementedException();
public override bool CanConvert(Type objectType)
return typeof(T) == objectType;
public static void Main(string[] args)
Dict1 = new Dictionary<double, Total>
{1, new Total { First = 0.3, Second = 0.7 }},
{2, new Total { First = 0.4, Second = 0.6 }},
{3, new Total { First = 0.9, Second = 0.1 }}
Dict2 = new Dictionary<double, Total>
{1, new Total { First = 0.3, Second = 0.7 }},
{2, new Total { First = 0.4, Second = 0.6 }}
var result = JsonConvert.SerializeObject(mc, new PredicateConverter<Total>(total => total.First > total.Second));