1

I have a "ValueObject" which I construct from a double and hold the value in private field.

I don't expose any properties or fields as public. How do I instruct Json.Net to serialize this objects value as double, and not as string?

I want this

"TimeStamp":0.00

Not this

"TimeStamp":"0.00"

Basically, I want a value of the "value object" to be serialized into json as double.

I implemented TypeConverter and added attribute to my value object, but serializer wants to serialize it into string.

epitka
  • 17,275
  • 20
  • 88
  • 141
  • Add a `TypeConverter` or `JsonConverter` as shown in [Json.Net: Serialize/Deserialize property as a value, not as an object](https://stackoverflow.com/q/40480489/3744182). Be sure to serialize your timestamp in the [invariant culture](https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture(v=vs.110).aspx). The type converter methods are passed a culture (and Json.NET calls them with the correct culture) while the implicit operator is not passed a culture. – dbc Sep 01 '17 at 16:44
  • 1
    Possible duplicate of [Json.Net: Serialize/Deserialize property as a value, not as an object](https://stackoverflow.com/questions/40480489/json-net-serialize-deserialize-property-as-a-value-not-as-an-object) – dbc Sep 01 '17 at 16:48
  • I created a TypeConverter that converts from double, and to double, but serializer wants to serialize value into string. Any ideas? – epitka Sep 05 '17 at 13:59
  • 1
    You may have to take the `JsonConverter` option then, I know that can work to serialize a c# object as a numeric value. – dbc Sep 05 '17 at 16:14

0 Answers0