Edit: It\'s been almost 5 years and I don\'t think this is the way to go. The client should post the data in the correct numerical format. With current fram
Thanx a lot! I was looking for a solution to make decimals always serialize in a similar manner and this post sent me in the right direction. This is my code:
internal class DecimalConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(decimal) || objectType == typeof(decimal?));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Decimal? d = default(Decimal?);
if (value != null)
{
d = value as Decimal?;
if (d.HasValue) // If value was a decimal?, then this is possible
{
d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conversion removes all unnessecary precision
}
}
JToken.FromObject(d).WriteTo(writer);
}
}