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?
In the first case the
000
does no harm, the value still is the same and will be deserialized to the exact same value.In the second case the JavascriptSerializer will not help you. The
JavacriptSerializer
is not supposed to change the data, since it serializes it to a well-known format it does not provide data conversion at member level (but it provides custom Object converters). What you want is a conversion + serialization, this is a two-phases task.Two suggestions:
1) Use
DataContractJsonSerializer
: add another property that rounds the value:2) Clone the object rounding the values:
I guess json.net cannot do this either, but I'm not 100% sure.
I wasn't completely satisfied with all of the techniques thus far to achieve this. JsonConverterAttribute seemed the most promising, but I couldn't live with hard-coded parameters and proliferation of converter classes for every combination of options.
So, I submitted a PR that adds the ability to pass various arguments to JsonConverter and JsonProperty. It's been accepted upstream and I expect will be in the next release (whatever's next after 6.0.5)
You can then do it like this:
Refer to the CustomDoubleRounding() test for an example.
For future reference, this can be achieved in Json.net pretty elegantly by creating a custom
JsonConverter
If you're creating serializers in code using constructor explicitly, this will work fine but I think it's nicer to decorate the relevant properties with
JsonConverterAttribute
, in which case the class must have a public, parameterless constructor. I solved this by creating a subclass which is specific to the format I want.The custom converter has been derived from Json.NET documentation.
I just went through the same trouble as I had some decimals being serialized with 1.00 and some with 1.0000. This is my change:
Create a JsonTextWriter that can round the value to 4 decimals. Every decimal will then be rounded to 4 decimals: 1.0 becomes 1.0000 and 1.0000000 becomes also 1.0000
Use your own writer instead of the standard one: