Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class sampleClass
- {
- public double x { get; set; }
- public double y { get; set; }
- public double z { get; set; }
- public double w { get; set; }
- }
- sampleClass c = new sampleClass()
- {
- x = -1,
- y = 2,
- z = -1,
- w = 8
- };
- {
- "x": null,
- "y": "2",
- "z": null,
- "w": "8"
- }
- {
- "x": null,
- "y": "2",
- "z": "-1",
- "w": "8"
- }
- class DecimalConverter : JsonConverter
- {
- public override void WriteJson(JsonWriter jsonWriter, object inputObject, JsonSerializer jsonSerializer)
- {
- // Set the properties of the Json Writer
- jsonWriter.Formatting = Formatting.Indented;
- // Typecast the input object
- var numericObject = inputObject as double?;
- jsonWriter.WriteValue((numericObject == -1.0) ? null : numericObject);
- }
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- double? readValue = reader.ReadAsDouble();
- return (readValue == null) ? -1 : readValue;
- }
- public override bool CanConvert(Type objectType)
- {
- return (typeof(double) == objectType);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement