Dotnet Core - The JSON value could not be converted to Newtonsoft.Json.Linq.JToken

Viewed 212

A have a Blazor wasm application with the following model defined as such:

{
    public int StaticTableId { get; set; }
    public JArray StaticData { get; set; }

    public static void RunFluent(ModelBuilder modelBuilder)
    {
        EntityTypeBuilder<StaticTable> entity = modelBuilder.Entity<StaticTable>();
        entity.HasKey(p => p.StaticTableId);
        entity.Property(e => e.StaticData).HasConversion(
            v => v.ToString(),
            v => JArray.Parse(string.IsNullOrEmpty(v) ? "[]" : v)
        );
    }
}

Note that RunFluent is executed in DbContext.OnModelCreating

Calling the API directly using the below function in the controller produces the correct result; a list of JSON objects.

// GET: api/StaticTable/5
[HttpGet("{id}")]
public async Task<ActionResult<StaticTable>> StaticTable(int id)
{
    var staticTable = await _context.StaticTables.FindAsync(id);

    if (staticTable == null)
    {
        return NotFound();
    }

    return staticTable;
}

However, attempting to pull this data to the front-end produces the following error:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $.projectAnalyses[0].jobInstances[4].resultsData.staticTable.staticData[0] | LineNumber: 0 | BytePositionInLine: 3263.`

Similar questions on SO suggest updating ConfigureServices() with a call to AddNewtonsoftJson(), however this is already in place.

Updating StaticTable.StaticData to be of type JToken also gives me an error:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: The collection type 'Newtonsoft.Json.Linq.JToken' is abstract, an interface, or is read only, and could not be instantiated and populated.

0 Answers
Related