As I said moved from .NET Core 2.2 with Newtonsoft up to .NET 6, but now with System.Text.Json built in, it seems to take precedence.
So in .NET Core 2.2, data is sent from the UI down to the dynamic mapping parameter where it is received as a Newtonsoft.Json.Linq.Object (see below)
From here I could use this code:
var customBranchMapping = JsonConvert.DeserializeObject<List<CustomBranchMappingDto>>(mapping.data.ToString());
importerService.SaveCustomBranchMapping(customBranchMapping);
// class
public class CustomBranchMappingDto
{
public int Ours { get; set; }
public string Theirs { get; set; }
public int CompanyId { get; set; }
}
and this would work...
But now in .NET 6, the dynamic parameter is being received as valueKind object??
which fails with
System.Text.Json.JsonElement' does not contain a definition for 'data'
(obviously). So System.Text.Json is overwriting the Newtonsoft... Is there any way to undo this or how can I fix this? I would like to keep it with Newtsonsoft as all my project is already Newtsonsoft and don't want to change everything over...
I tried breaking it down and taking a step back to converting the dynamic to a json string with only one record
[Authorize(Policy = "CanManageCompany")]
[HttpPost("SaveCustomBranchMapping")]
public IActionResult SaveCustomBranchMapping([FromBody] dynamic mapping)
{
try
{
string result1 = Convert.ToString(mapping);
// result 1 values are {"data":[{"ours":193,"theirs":"TAMPA","companyId":111}]}
var customBranchMapping9 = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomBranchMappingDto>(result1);
}
but this is returning 0,0, null
What am I doing wrong? Any help please?


