There are two API endpoints on two different projects, which receive json and deserialize it to instances the same class.
Json:
{
"name": "MyId",
"objectData": 4141
}
Class:
public class MyObject
{
public string name { get; set; }
public object objectData { get; set; }
}
Endpoints (identical)
[HttpPost, Route("v1/testController/TestEndpoint")]
public IActionResult TestEndpoint(MyObject myObject)
{
try
{
//Do stuff
}
catch (ApiException e)
{
//Handle error
}
}
However when the json is deserialized, objectData holds different values between the two projects:
myObjectProjectA
{
name: "MyId"
objectData: 4141
}
myObjectProjectB
{
name: "MyId"
objectData: ValueKind = Number : "4141"
}
The desired result is myObjectProjectA. Any ideas why they are different or resources to better understand how the deserialer works for the HttpPost method?
On both projects we are using the following packages: Microsoft.AspNetCore (2.2.0) Microsoft.AspNetCore.Mvc.NewtonsoftJson (3.1.15)
Thanks!
Found the answer from a post about JObject having a similar problem. Even though Newtonsoft.Json was installed the endpoint was still using system.Text.Json because Newtonsoft.Json needed to be added to ConfigureServices()
serviceCollection.AddControllers() .AddNewtonsoftJson();