-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing original string of DateTime type token in a custom converter #893
Comments
This behaviour is absolutely bonkers. @JamesNK has been closing these issues on the grounds of backwards compatibility, which is understandable. Look's like the only way forward would be a fork. |
Why can't you set DateParseHandling on the JsonReader before you start serializing? |
Can I do it once for all deserializations of |
Use JsonConvert.DefaultSettings + DateParseHandling.None |
But how can I tell Json.NET to always use |
This is shocking! |
For anyone else stumbling on this...
After this, reader.TokenType will be JToken.String and not JToken.Date. Same with reader.Value. |
Having the same problem. Unfortunately the solution above does not seem to work. Setting DateParseHandling seems to be to late and has no effect. |
I know this issue is already closed but wanted to mention I'm running into same issue. The context is ASP.NET Core 3.1 application where I'm using |
For this particular case, it would be nice to have access within one of the Specifically for the Newtonsoft.Json/Src/Newtonsoft.Json/JsonTextReader.cs Lines 202 to 240 in 6b9f467
I'm not sure what all the ramifications would be for exposing If it makes sense on For reference, here is the top level call to Json.NET from ASP.NET Core for deserializing request body JSON string: A totally different approach to achieving this goal, rather than exposing |
I am facing the same issue |
I faced a similar issue, I needed the raw value of a field in my custom converter. |
Same need here, exactly the use case described by jdlundquist:
|
I am having the same problem. Could @JamesNK please tell us the reason why a custom JsonConverter would not be able to access the raw text in the event that the format of that text is known only to that converter. Is it not presumptuous and counter to requirement that some other code converts the raw text to the destination type? |
Same problem as in #694 (default
DateParseHandling
settings lead to loss of original string value), different solution proposed.I wanted to write a custom converter to convert my class to/from string (and I need the exact string value even if it looks like datetime). Unfortunately, inside of
ReadJson
the value is already processed asDateTime
, it's too late to setDateParseHandling.None
as it cannot be re-read (forward-only) and it is not possible to recover its original string form.I don't know of any way to configure
DateParseHandling.None
to be used selectively with my converter only, and not for entire json I'm reading.I think the easiest way to solve the problem without breaking compatibility would be to provide original string value as a new property of
JsonReader
(RawValue
?) - alongsidePath
,LinePosition
etc.The text was updated successfully, but these errors were encountered: