Repository navigation
Closed
Description
Found this issue while trying to build and run tests for System.Text.Json
Example stacktrace from System.Text.Json.Serialization.Tests.ObjectTests.ReadSimpleTestClassWithSimpleTestStruct
System.Text.Json.JsonException : The JSON value could not be converted to System.DateTime. Path: $.MySimpleTestStruct.MyDateTime | LineNumber: 0 | BytePositionInLine: 912.\r\n---- System.FormatException : The JSON value is not in a supported DateTime format.
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\ThrowHelper.Serialization.cs:line 172
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.cs:line 124
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Helpers.cs:line 22
at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs:line 74
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs:line 31
at System.Text.Json.Serialization.Tests.ObjectTests.ReadSimpleTestClassWithSimpleTestStruct() in C:\Users\mb\src\gh\corefx\src\System.Text.Json\tests\Serialization\Object.ReadTests.cs:line 319
----- Inner Stack Trace -----
at System.Text.Json.Utf8JsonReader.GetDateTime() in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Reader\Utf8JsonReader.TryGet.cs:line 392
at System.Text.Json.Serialization.Converters.JsonConverterDateTime.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\Converters\JsonValueConverterDateTime.cs:line 11
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonPropertyInfoNotNullable.cs:line 25
at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonPropertyInfo.cs:line 324
at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandleValue.cs:line 28
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.cs:line 50
Steps to reproduce:
- Set TimeZone to something that has an UTC offset larger than 0
- Run following code:
class Class
{
public DateTime MyDateTime {get; set;}
}
class Program
{
static void Main(string[] args)
{
var json = "{\"MyDateTime\": \"0001-01-01T00:00:00\"}";
var c = System.Text.Json.JsonSerializer.Deserialize<Class>(json);
Console.WriteLine(c.MyDateTime);
}
}- This should give same exception
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.MyDateTime | LineNumber: 0 | BytePositionInLine: 36.
---> System.FormatException: The JSON value is not in a supported DateTime format.
at System.Text.Json.Utf8JsonReader.GetDateTime()
at System.Text.Json.Serialization.Converters.JsonConverterDateTime.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
--- End of inner exception stack trace ---
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at json_repro.Program.Main(String[] args) in C:\Users\mb\src\tmp\json-repro\Program.cs:line 15
Tried debugging, and the issue is here:
https://github.com/dotnet/corefx/blob/da3ba7ebe34771dde769b65dfb7f78fa55a5abb6/src/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs#L450-L466
dateTime returned from TryCreateDateTime() previously is var dateTime = new DateTime(0, DateTimeKind.Local), which means that it will be out of range for a DateTimeOffset if UTC offset is larger than 0.
Activity
bjorkstromm commentedon Jun 30, 2019
Biggest issue for me is that I'm unable to build and test
System.Text.Jsonwithout adjusting my timezone. 😢bjorkstromm commentedon Jul 1, 2019
Related to (or possible duplicate of) #29964
bjorkstromm commentedon Jul 1, 2019
Probably same issue with
DateTime.MaxValuewhen in time zone with UTC offset smaller than 0 (haven’t tested).DateTime.MinValueis however more problematic since it’s the default value ofDateTime.steveharter commentedon Jul 1, 2019
@layomia can you take a look please? This may have been fixed.
steveharter commentedon Jul 1, 2019
cc @JeremyKuhne if you still looking at DateTime support.
JeremyKuhne commentedon Jul 1, 2019
@layomia I'll look at this since I'm in this code at the moment.
JeremyKuhne commentedon Jul 4, 2019
Should be ok with dotnet/corefx#39187.
bjorkstromm commentedon Jul 8, 2019
@JeremyKuhne I tried running tests from your PR branch (#39187), but I am still seeing the same issues.
JeremyKuhne commentedon Jul 8, 2019
Interesting. I wonder what the difference is? I set myself to UTC + 1 and didn't have a problem.
bjorkstromm commentedon Jul 8, 2019
Weird, well, here's my settings:

Let me know if you'd like me to debug anything.
JeremyKuhne commentedon Jul 8, 2019
@layomia can you try to repro this as well once dotnet/corefx#39187 goes in?
ericstj commentedon Jul 10, 2019
I can help here. I repro'ed the original issue. Building your change and testing now.
4 remaining items
tarekgh commentedon Jul 10, 2019
I am seeing the code is already catching this ArgumentOutOfRangeException. are we failing the operation after that?
https://github.com/dotnet/corefx/blob/1247fa7a193e52bc89bc49c4ba6e44739df2dcee/src/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs#L493
ericstj commentedon Jul 11, 2019
Yeah, it's a "Try" method, so when this fails and returns false the outer call ends up throwing.
This is happening because we're trying to parse as DateTime (which can accept this value) but then we're flowing it through a DateTimeOffset (which cannot accept this value since UTC representation underflows year 1) here only to convert back to DateTime.
https://github.com/dotnet/corefx/blob/1247fa7a193e52bc89bc49c4ba6e44739df2dcee/src/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs#L18-L41
The fix, as Jeremy mentioned, would be to avoid this smuggling, and instead either go straight to DateTime or DateTimeOffset, depending on what the target type is.
tarekgh commentedon Jul 11, 2019
That make sense.
layomia commentedon Jul 11, 2019
I'm taking a look at this now.
markvincze commentedon Aug 1, 2019
Hey folks,
I checked this issue and #29964, but it's unclear to me if this is fixed already or not.
If I run this code:
I'm still getting the
The JSON value could not be converted to System.DateTimeexception.I'm using version
4.6.0-preview7.19362.9ofSystem.Text.Json, which was released 9 days ago, so dotnet/corefx#39547 should've been included in it. (Update: okay, now I see that PR only changes the tests)Is this a bug which is still not fixed, or am I trying to do something invalid?
karelz commentedon Aug 3, 2019
It was fixed in master (3.0 at that moment) in PR dotnet/corefx#39541.
@markvincze if you see a problem on latest master or release/3.0 builds, please file a new bug.
markvincze commentedon Aug 3, 2019
Hi @karelz,
Thanks for the info!
Do you happen to know if there is an easy way for me to check if a specific Nuget release (for example 4.6.0-preview7.19362.9) already contains a certain PR or not? (For example can I check the commit based on which that package was released?
bjorkstromm commentedon Aug 3, 2019
@markvincze the releases are tagged. By looking at the src in preview7 tag I can see that this fix isn’t available in preview7. See https://github.com/dotnet/corefx/commits/v3.0.0-preview7.19362.9/src/System.Text.Json/src
markvincze commentedon Aug 3, 2019
@mholo65 clear, thanks! I'll look out for the next preview and will test it there.
bjorkstromm commentedon Aug 3, 2019
@markvincze if you don’t want to wait for another preview, you can try out the nightly builds as explained here https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/dogfooding.md
markvincze commentedon Aug 15, 2019
@mholo65 @karelz,
Thanks for help, I verified that the issue is fixed in
4.6.0-preview8.19405.3👍