using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class ChargesDetail
public double DiscountRate { get; set; }
public Amount DiscountAmount { get; set; }
public class AmountConverter : JsonConverter
public override bool CanConvert(Type objectType)
return objectType == typeof(Amount);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var value = serializer.Deserialize<double?>(reader);
return new Amount(value.Value);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
writer.WriteValue(((Amount)value).Value);
[JsonConverter(typeof(AmountConverter))]
public class Amount : IConvertible
private int _decimal = 5;
public double Value { get { return _val; } }
public Amount(double amount)
_val = Math.Round(amount, _decimal);
#region IConvertible Members
public TypeCode GetTypeCode()
throw new NotImplementedException();
public bool ToBoolean(IFormatProvider provider)
throw new NotImplementedException();
public byte ToByte(IFormatProvider provider)
throw new NotImplementedException();
public char ToChar(IFormatProvider provider)
throw new NotImplementedException();
public DateTime ToDateTime(IFormatProvider provider)
throw new NotImplementedException();
public decimal ToDecimal(IFormatProvider provider)
throw new NotImplementedException();
public double ToDouble(IFormatProvider provider)
throw new NotImplementedException();
public short ToInt16(IFormatProvider provider)
throw new NotImplementedException();
public int ToInt32(IFormatProvider provider)
throw new NotImplementedException();
public long ToInt64(IFormatProvider provider)
throw new NotImplementedException();
public sbyte ToSByte(IFormatProvider provider)
throw new NotImplementedException();
public float ToSingle(IFormatProvider provider)
throw new NotImplementedException();
public string ToString(IFormatProvider provider)
throw new NotImplementedException();
public object ToType(Type conversionType, IFormatProvider provider)
throw new NotImplementedException();
public ushort ToUInt16(IFormatProvider provider)
throw new NotImplementedException();
public uint ToUInt32(IFormatProvider provider)
throw new NotImplementedException();
public ulong ToUInt64(IFormatProvider provider)
throw new NotImplementedException();
public static void Test()
var detail = new ChargesDetail
DiscountAmount = new Amount(101.25),
var json = JsonConvert.SerializeObject(detail, Formatting.Indented);
var detail2 = JsonConvert.DeserializeAnonymousType(json, detail);
Assert.IsTrue(detail.DiscountAmount.Value == detail2.DiscountAmount.Value);
var settings = new JsonSerializerSettings
Converters = { new AmountConverter() },
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");