-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
JsonSerializer doesn't support valid TimeSpan formats #100538
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
The built-in converter for |
I can totally understand restricting to the |
We ran into a similar issue. Newtonsoft.Json supports both the 'c' format and the 'g' format (short version) but the built-in Example Newtonsoft.Json accepts To not break wire compatibility, we had to implement a custom TimeSpanConverter that is an almost 1:1 copy of the .NET version but allows starting with 7 digits (shortest format). |
The built-in converter does delegate to that routine when parsing: Line 60 in 9be6287
Modulo rejecting leading whitespace the two should be identical. |
Except this check Line 35 in 9be6287
|
Can you share examples of valid inputs that are caught by this particular check? This is there simply to avoid allocating an intermediate buffer (and multiplies the maximum expected size by a factor of 6 to account for potential escaped values) |
|
The following program demonstrates things
fails with
while Utf8Parser.TryParse(Encoding.UTF8.GetBytes("0:00:02"), out TimeSpan parsedTimeSpan, out _, standardFormat: 'c') would return true |
Right, but that input would be rejected on account of it not satisfying the |
Utf8Parser.TryParse(Encoding.UTF8.GetBytes("0:00:02"), out TimeSpan parsedTimeSpan, out _, standardFormat: 'c') returns true |
I see, so presumably it gets rejected because the input is too short? I think we should address that. |
Happy to accept a PR that addresses this by the way, it's unlikely we would be able to prioritize this any time soon. |
That's my understanding but I haven't spent enough time to be 100% sure |
I see what I can do |
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
@eiriktsarpalis Here we go #102091 // cc @eerhardt |
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
…d be accepted by Utf8Parser.TryParse Fix dotnet#100538
Will the fix be backported? |
Unfortunately, no. Only high-impact bugs and security fixes get backported. |
…d be accepted by Utf8Parser.TryParse (dotnet#102091) Fix dotnet#100538 Co-authored-by: Daniel Marbach <danielmarbach@users.noreply.github.com>
Description
When trying to deserialize TimeSpan strings from JSON into an object using
JsonSerializer.Deserialize
, not all valid TimeSpan formats are supported.This is inconsistent with our other APIs that deserialize TimeSpan values from strings (for example
Microsoft.Extensions.Configuration.Binder
). It can cause problems when people are reading TimeSpan information from other sources and can't control the format being written.Reproduction Steps
Expected behavior
The above code should print each TimeSpan value in
timeSpanStrings
4 times:JsonConvert.DeserializeObject
Actual behavior
Regression?
No
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: