runtime: JsonSerializer doesn't support valid TimeSpan formats

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

using Newtonsoft.Json;
using System.Buffers.Text;
using System.Text;

string[] timeSpanStrings = ["00:01", "10"];

foreach (string timeSpanString in timeSpanStrings)
{
    TimeSpan timeSpan = TimeSpan.Parse(timeSpanString);
    Console.WriteLine(timeSpan);

    var timeSpanStringUtf8 = Encoding.UTF8.GetBytes(timeSpanString);
    Utf8Parser.TryParse(timeSpanStringUtf8, out timeSpan, out _, standardFormat: 'c');
    Console.WriteLine(timeSpan);

    string json = $$"""
    {
      "Delay": "{{timeSpanString}}"
    }
    """;

    var options = JsonConvert.DeserializeObject<MyOptions>(json);
    Console.WriteLine(options?.Delay);

    try
    {
        options = System.Text.Json.JsonSerializer.Deserialize<MyOptions>(json);
        Console.WriteLine(options?.Delay);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    Console.WriteLine("----");
}
public class MyOptions
{
    public TimeSpan Delay { get; set; }
}

Expected behavior

The above code should print each TimeSpan value in timeSpanStrings 4 times:

  • TimeSpan.Parse
  • Utf8Parser.TryParse
  • The result of Newtonsoft.Json’s JsonConvert.DeserializeObject
  • The result from JsonSerializer.Deserialize

Actual behavior

00:01:00
00:01:00
00:01:00
System.Text.Json.JsonException: The JSON value could not be converted to System.TimeSpan. Path: $.Delay | LineNumber: 1 | BytePositionInLine: 18.
 ---> System.FormatException: The JSON value is not in a supported TimeSpan format.
   at System.Text.Json.ThrowHelper.ThrowFormatException(DataType dataType)
   at System.Text.Json.Serialization.Converters.TimeSpanConverter.ReadCore(Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in C:\Users\eerhardt\source\repos\ConsoleApp115\ConsoleApp115\Program.cs:line 27
----
10.00:00:00
10.00:00:00
10.00:00:00
System.Text.Json.JsonException: The JSON value could not be converted to System.TimeSpan. Path: $.Delay | LineNumber: 1 | BytePositionInLine: 15.
 ---> System.FormatException: The JSON value is not in a supported TimeSpan format.
   at System.Text.Json.ThrowHelper.ThrowFormatException(DataType dataType)
   at System.Text.Json.Serialization.Converters.TimeSpanConverter.ReadCore(Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in C:\Users\eerhardt\source\repos\ConsoleApp115\ConsoleApp115\Program.cs:line 27
----

Regression?

No

Known Workarounds

No response

Configuration

No response

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (18 by maintainers)

Commits related to this issue

Most upvoted comments