runtime: JsonSerializer is unable to deserialize a valid JSON
Given the following payload:
{
"picture": "http://placehold.it/32x32",
"eyeColor": "Brown",
"registered": "2015-05-30T01:50:21 -01:00"
}
And the following models:
public sealed class Model
{
public Color EyeColor { get; set; }
public Uri Picture { get; set; }
public DateTime Registered { get; set; }
}
public enum Color
{
Blue,
Green,
Brown
}
An exception is thrown when attempting to deserialize using:
JsonSerializer.Parse<Model>(
json,
new JsonSerializerOptions
{
IgnoreNullValues = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = false
});
Exception:
System.Text.Json.JsonException: ‘The JSON value could not be converted to System.Uri. Path: $.picture | LineNumber: 1 | BytePositionInLine: 39.’
In fact, it is unable to deserialize any of the three properties and not just the Uri.
This is running against the following:
Version: 3.0.0-preview6-27804-01
Commit: fdf81c6faf
The same payload is deserialized as expected in both UTF8Json and JSON.NET.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (8 by maintainers)
@NimaAra I’ve added a scenario test example at https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/tests/Serialization/ReadScenarioTests.cs#L17 that you can reference on how to do this.
Uriworks now out of the box. You can plug in the built-in “JsonStringEnumConverter” to get the enum as string parsing. ForDateTimeyou can make a more lenient converter pretty easily. All of this is shown in the link.No problem, thanks for your work on this. Looking forward to the additional features closing some of the gaps with other widely used libraries.
The Datetime has a whitespace before the Timezone. Last Time I checked that’s not allowed.
Also enums are integer per default (afaik), so that would explain the enums.