I am in the process of updating one of our systems to use System.Text.Json rather than Newtonsoft.Json.
I have a Data class that I have amended to use System.Text.Json. This handles calls from MVVM WPF UI view models to get model data via a Web API. All calls to the API use application/json media type.
I construct a small Request class that is a container for the required data and parameters this gets passed to the API as follows
HttpResponseMessage httpresponse = client.PostAsync("api/data", new StringContent(SerialiseToJson(request), Encoding.UTF8, "application/json")).Result;
My DataController Post method on the API then takes the received request and passes it back, server side, to the Data class for further processing.
public HttpResponseMessage Post(Data.Request request)
{
HttpResponseMessage httpresponse = null;
Data.Response response = null;
try
{
if (request.Type == Data.Request.RequestType.Get)
response = Data.GetData(request);
else
{
response = Data.UpdateData(request);
if (Common.ReportProgressDetailSubscribedTo)
{
if (response.Status == Data.Response.ResponseStatus.Error)
Common.ReportProgressDetail(string.Format("UpdateData Response.Status={0}\r\nResponse.ErrorMessage={1}\r\nResponse.ErrorDetails={2}", response.Status.ToString(), response.ErrorMessage, response.ErrorDetails));
else
Common.ReportProgressDetail(string.Format("UpdateData Response.Status={0}", response.Status.ToString()));
}
}
httpresponse = Request.CreateResponse<Data.Response>(HttpStatusCode.OK, response);
}
catch (Exception ex)
{
response = new Data.Response("Unexpected error", ex.Message);
httpresponse = Request.CreateResponse<Data.Response>(HttpStatusCode.OK, response);
Common.ReportProgress(string.Format("ERROR: {0}", response.ErrorDetails));
}
return httpresponse;
}
Now, having updated the Data class to use System.Text.Json, when I deserialize the inner properties of the Request, I am expecting them to be typeof(JsonElement), however I am finding that they are typeof(Newtonsoft.Json.Linq.JObject).
From research, I believe this is because the NuGet packages that the Web API us built from have Newtonsoft references link
This link suggests creating an MVC site with plain controllers, however this is where I am struggling. Whenever I try something, required NuGet packages include Newtonsoft.
I am trying to build this in .NET 4.8 as this matches the level of the rest of the system.
I also need the API to be backward compatible, so whatever I change, I still need to transport HttpResponseMessage return types. As application/json is a standard format that both Newtonsoft.Json and System.Text.Json should be able to read, I don't see why this cannot be achieved.
The only thought I have been left with is, within the Post method, if the incoming Request is serialized using Newtonsoft, then I could deserialize it and reserialize it to System.Text.Json before passing it to my Data class. However, this will add additional processing and complexity.
Surely there is a better way?
I am in the process of updating one of our systems to use System.Text.Json rather than Newtonsoft.Json...for any particular reason? What gains do you expect to make from doing that?I believe this is because the NuGet packages that the WebAPI us built from have Newtonsoft references..yeah, in .NET 4.x the Web API uses newtonsoft by default to handle JSON operations. So by the time it gets into your controller the data has been deserialised to a C# object using newtonsoft