question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.
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();
  }
  1. 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]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
RMIN3452commented, Dec 13, 2019

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 <JwtToken>(response.Content,new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

2reactions
Hunvcommented, Jan 7, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when deserializing to ProblemDetails with System. ...
Text.JsonSerialiser i found the following issues: Status, Code and Title are not deserialised to the problem details properties, they are null ...
Read more >
How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
Read more >
Serialization and deserialization of 'System.IntPtr' instances ...
Json serializer cannot serialize an exception. So when I return the BadRequest(ex), ex cannot be serialized to a JSON text string. And since...
Read more >
C# - How to read problem details JSON with HttpClient
I'll show examples of deserializing the problem details JSON. Table of Contents. Deserialize problem details JSON. Deserialize with System.Text.
Read more >
Sending and Receiving JSON using HttpClient with System ...
In this post, I introduce System.Net.Http.Json for sending and recieveing JSON content to external services using HttpClient in .NET.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found