JsonApiFramework
JsonApiFramework copied to clipboard
Move from Newtonsoft.Json to System.Text.Json
Hi Scott It would be perfect to move to the new (de)serialization library. Do you have any plans for that?
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 justAddControllers()
inStartup
related:
- https://github.com/aspnet/AspNetCore/issues/13564
- https://github.com/dotnet/corefx/issues/38758
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.
We also just hit this in .Net Core 3 😅