Aspnetcore: System.Text.Json.JsonSerializer.Deserialize not working with class ProblemDetails

Created on 20 Nov 2019  ·  5Comments  ·  Source: dotnet/aspnetcore

Describe the bug

The deserialization of class ProblemDetails always return NULL value for properties(Title, status, etc.)

To Reproduce

  1. create a MVC application with .net core 3.0, try to deserialize below JSON string in Index() action. This JSON string was created by system.text.json.jsonserialize.serialize.
    ``` C#
    public IActionResult Index()
    {
    var json = @"{
    ""title"": ""SOME TITLE"",
    ""status"": 500,
    ""detail"": ""Some detail"",
    ""errors"": [
    {
    ""Parameter"": null,
    ""Code"": ""SOME CODE"",
    ""Message"": ""SOME MESSAGE"",
    ""Details"": ""SOME EXTRA DETAILS""
    }
    ]
    }";

        var result = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(json);
    
     return View();
    

    }
    ```

  2. The out put always have null value properties, please check below image for outputs
    https://github.com/MsNewbie/test/blob/master/test1.png

Further technical details

  • ASP.NET Core version 3.0.0 & 3.0.1
  • Include the output of
    C:\Users\yejxu>dotnet --info
    .NET Core SDK (reflecting any global.json):
    Version: 3.0.101
    Commit: bc5f8df0f5

Runtime Environment:
OS Name: Windows
OS Version: 10.0.17623
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.101\

Host (useful for support):
Version: 3.0.1
Commit: 32085cbc72

.NET Core SDKs installed:
2.1.801 [C:\Program Files\dotnet\sdk]
2.2.401 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.0.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

area-mvc

Most helpful comment

Version of System.Text.Json I'm using

#region Assembly System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.Text.Json.dll
#endregion

There still appears t be an issue with System.Text.Json.JsonSerializer.Deserialize deserializing what appears to be valid JSON. All the values for problemDetails are NULL when the code executes. Newtonsoft JsonConvert.DeserializeObject on the other hand works as expected.

ProblemDetails problemDetails = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(ex.Response);

ex.Response contains the following JSON:

"{\"type\":\"https://tools.ietf.org/html/rfc7231#section-6.5.4\",\"title\":\"Not Found\",\"status\":404,\"traceId\":\"|2b127cb8-48a6a0aa0a2beaf9.\"}"

i have the same problem and fix it by add PropertyNamingPolicy = JsonNamingPolicy.CamelCase setting:

JsonSerializer.Deserialize (response.Content,new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});

All 5 comments

@MsNewbie thanks for contacting us.

@pranavkm is this a system.text.json thing? (Are we missing setters in ProblemDetails?)

This is a bug that we fixed in 3.1, the problem can be worked around by writing a custom json converter.

See #17031 for more details on a similar issue.

Version of System.Text.Json I'm using

#region Assembly System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.Text.Json.dll
#endregion

There still appears t be an issue with System.Text.Json.JsonSerializer.Deserialize deserializing what appears to be valid JSON. All the values for problemDetails are NULL when the code executes. Newtonsoft JsonConvert.DeserializeObject on the other hand works as expected.

ProblemDetails problemDetails = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(ex.Response);

ex.Response contains the following JSON:

"{\"type\":\"https://tools.ietf.org/html/rfc7231#section-6.5.4\",\"title\":\"Not Found\",\"status\":404,\"traceId\":\"|2b127cb8-48a6a0aa0a2beaf9.\"}"

Version of System.Text.Json I'm using

#region Assembly System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.Text.Json.dll
#endregion

There still appears t be an issue with System.Text.Json.JsonSerializer.Deserialize deserializing what appears to be valid JSON. All the values for problemDetails are NULL when the code executes. Newtonsoft JsonConvert.DeserializeObject on the other hand works as expected.

ProblemDetails problemDetails = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(ex.Response);

ex.Response contains the following JSON:

"{\"type\":\"https://tools.ietf.org/html/rfc7231#section-6.5.4\",\"title\":\"Not Found\",\"status\":404,\"traceId\":\"|2b127cb8-48a6a0aa0a2beaf9.\"}"

i have the same problem and fix it by add PropertyNamingPolicy = JsonNamingPolicy.CamelCase setting:

JsonSerializer.Deserialize (response.Content,new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});

I am using 3.1 and have the same issue. The comment of @RMIN3452 works.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TanvirArjel picture TanvirArjel  ·  3Comments

Kevenvz picture Kevenvz  ·  3Comments

snebjorn picture snebjorn  ·  3Comments

markrendle picture markrendle  ·  3Comments

FourLeafClover picture FourLeafClover  ·  3Comments