Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public class sampleClass
  2. {
  3. public double x { get; set; }
  4. public double y { get; set; }
  5. public double z { get; set; }
  6. public double w { get; set; }
  7. }
  8.  
  9. sampleClass c = new sampleClass()
  10. {
  11. x = -1,
  12. y = 2,
  13. z = -1,
  14. w = 8
  15. };
  16.  
  17. {
  18. "x": null,
  19. "y": "2",
  20. "z": null,
  21. "w": "8"
  22. }
  23.  
  24. {
  25. "x": null,
  26. "y": "2",
  27. "z": "-1",
  28. "w": "8"
  29. }
  30.  
  31. class DecimalConverter : JsonConverter
  32. {
  33. public override void WriteJson(JsonWriter jsonWriter, object inputObject, JsonSerializer jsonSerializer)
  34. {
  35. // Set the properties of the Json Writer
  36. jsonWriter.Formatting = Formatting.Indented;
  37.  
  38. // Typecast the input object
  39. var numericObject = inputObject as double?;
  40. jsonWriter.WriteValue((numericObject == -1.0) ? null : numericObject);
  41. }
  42.  
  43. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  44. {
  45. double? readValue = reader.ReadAsDouble();
  46. return (readValue == null) ? -1 : readValue;
  47. }
  48.  
  49. public override bool CanConvert(Type objectType)
  50. {
  51. return (typeof(double) == objectType);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement