2

When I parse a String that contains a valid RFC3339 datetimestring, JOject.Parse() automatically makes an implizit type conversion to DateTime:

String toParse = "{\"datetime\": \"2020-08-04T23:45:00+02:00\"}";
JObject p = JObject.Parse(toParse);
String d = (String)p["datetime"];
Console.Write(d);
// Gives 08/04/2020 23:45:00
// But I want 2020-08-04T23:45:00+02:00

I think the "unwanted" typecast takes place while reading the p["datetime"] and not during JOject.Parse(), but how can I totally avoid that behavior and let it only deserialize everything as string?

I have this problem with different types, also decimals which are automatically casted, by I need them as String.

Thank you!

dontspeak
  • 37
  • 6
  • 3
    Does this answer your question? [Json.NET Disable the deserialization on DateTime](https://stackoverflow.com/questions/11856694/json-net-disable-the-deserialization-on-datetime) – ColinM Jul 30 '20 at 19:24
  • There is `Value()` method – Pavel Anikhouski Jul 30 '20 at 19:34
  • 2
    ... I closed this as a duplicate, then I saw your final sentence: *I have this problem with different types, also decimals which are automatically casted, by I need them as String.* Unfortunately there is no way to get the original text for a JSON number using Json.NET. Best you can do is to parse then as `decimal` rather than `double` using `FloatParseHandling.Decimal`, see [JObject.Parse modifies end of floating point values](https://stackoverflow.com/q/26484176/3744182). Do you want me to reopen it? – dbc Jul 30 '20 at 20:00
  • 2
    Only suggestion I can give is to switch to [tag:system.text.json] which **does** preserve the raw text for numeric values unchanged. – dbc Jul 30 '20 at 20:01
  • 1
    DateParseHandling.None and FloatParseHandling.Decimal solved my problem. Thanks a lot! – dontspeak Jul 31 '20 at 18:41

0 Answers0