I have a class
public class Money
{
public string Currency { get; set; }
public decimal Amount { get; set; }
}
and would like to serialize it to JSON. If I use the JavaScriptSerializer
I get
{"Currency":"USD","Amount":100.31000}
Because of the API I have to conform to needs JSON amounts with maximum two decimal places, I feel it should be possible to somehow alter the way the JavaScriptSerializer
serializes a decimal field, but I can't find out how. There is the SimpleTypeResolver you can pass in the constructor, but it only work on types as far as I can understand. The JavaScriptConverter, which you can add through RegisterConverters(...) seems to be made for Dictionary
.
I would like to get
{"Currency":"USD","Amount":100.31}
after I serialize. Also, changing to double is out of the question. And I probably need to do some rounding (100.311 should become 100.31).
Does anyone know how to do this? Is there perhaps an alternative to the JavaScriptSerializer
that lets you control the serializing in more detail?