JsonApiFramework icon indicating copy to clipboard operation
JsonApiFramework copied to clipboard

Move from Newtonsoft.Json to System.Text.Json

Open yurii-pelekh opened this issue 5 years ago • 5 comments

Hi Scott It would be perfect to move to the new (de)serialization library. Do you have any plans for that?

yurii-pelekh avatar Nov 05 '19 13:11 yurii-pelekh

Note: because of the hard dependency on Newtonsoft.Json, out of the box JsonApiFramework does not behave as expected on .NET Core 3.0/3.1 (the serialisation format doesn't match as Newtonsoft.Json attributes are ignored).

A workaround exists:

  • add dependency on Microsoft.AspNetCore.Mvc.NewtonsoftJson
  • call services.AddControllers().AddNewtonsoftJson(); instead of just AddControllers() in Startup

related:

  • https://github.com/aspnet/AspNetCore/issues/13564
  • https://github.com/dotnet/corefx/issues/38758

skolima avatar Jan 02 '20 15:01 skolima

For what it's worth.

I was testing the Blogging Web Service example from the repo JsonApiFramework.Samples.
The project was originally made for .net core 2.2, but I changed it to 3.1.
I knew I shoudn't have changed it but it was just a test anyway. "Lets try it out and see what happens", I thought.

I was getting weird responses like (below a small piece of a response)

"jsonApiVersion": {
    "version": "1.0",
    "meta": null
  },
"attributes": [
      {
        "name": "firstName"
      },
      {
        "name": "lastName"
      },
      {
        "name": "twitter"
      }
    ]

And when I added .AddNewtonsoftJson() to .AddControllers() , everything worked as expected:

"jsonapi": {
    "version": "1.0"
  },
"attributes": {
      "firstName": "Sina",
      "lastName": "Pesantes",
      "twitter": "@spesantes"
    }

Thanks skolima for your comment above. Maybe this comment will be useful for someone trying to use this library with .net core 3.1.

How to fix this?

Until the Newtonsoft.json dependency is in the code I think would be useful throw an exception if the serializer is not configured properly.

RichardsonWTR avatar Jul 31 '20 18:07 RichardsonWTR

We also just hit this in .Net Core 3 😅

abbottdev avatar Oct 14 '20 15:10 abbottdev