0

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?

4
  • 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? Commented Jun 24 at 9:33
  • 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 Commented Jun 24 at 9:35
  • We have had some dllHell using Newtoonsoft. We have various solutions, both in-house and third party, that require various different versions of Newtonsoft. Where different assemblies cross-over requiring different versions of Newtonsoft we have had tremendous issues. Assembly redirects don't always solve them. We have decided to try and migrate in-house solutions to System.Text.Json to mitigate the issues. We can probably handle third party requirements for Newtonsoft easier. Can I create a .net core api that doesn't use Newtonsoft but can still reference our framework 4.8 Data assembly? Commented Jun 24 at 10:38
  • 1
    If your data assembly can be compiled with .Net Standard as a target, or just re-created in .NET Core easily, then yes Commented Jun 24 at 10:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.