neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

ISO8601TimeSpanFormatter does not serialize to ISO 8601 compliant durations #174

Open rexcfnghk opened 4 years ago

rexcfnghk commented 4 years ago

According to ISO 8601 durations, this unit test should pass:

[Fact]
public void Test_TimeSpan()
{
    // Arrange
    const string expected = "{\"T\":\"PT36H\"}";
    var timespan = new Wrapper(TimeSpan.FromHours(36));

    // Act
    var serialized = JsonSerializer.Serialize(timespan);
    var actual = Encoding.UTF8.GetString(serialized);

    // Assert
    Assert.Equal(expected, actual);
}

public readonly struct Wrapper
{
    public Wrapper(TimeSpan t)
    {
        T = t;
    }

    public TimeSpan T { get; }
}

But it's failing with the message:

Assert.Equal() Failure
                ↓ (pos 6)
Expected: {"T":"PT36H"}
Actual:   {"T":"1.12:00:00"}
                ↑ (pos 6)

As I understand, "1.12:00:00" is not a valid ISO 8601 representation of a duration (i.e. C#'s TimeSpan). Is there a bug in ISO8601TimeSpanFormatter?

orangeagain commented 4 years ago

i meet this problem too. can not deserialize google recapachar (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)