In a .NET Core 3 web API, I want to serialize an object with many properties. Depending on the caller's permissions, I want to show or hide specific properties. For now, I'm working with the [JsonIgnore]
attribute exclusively. This is fine for properties, that you generally don't want to serialize.
But is there something similar like a [JsonIgnore(context => context.CheckUserPermissions())]
? Or maybe something that I can add to my API method?
My current code is pretty simple:
[HttpGet("PickingZoneLists/{pickingZone}")]
public async Task<ActionResult<PickingListEntry[]>> GetData(string pickingZone) {
// Get data
dsyWorkOrder[] workOrders;
try {
workOrders = await GetPickingDocList(pickingZone);
} catch (Exception ex) {
return Problem("Data could not be gathered. " + ex.Message);
}
// Validate result
if (workOrders == null)
return Problem("Service returned invalid data.");
if (workOrders.Length == 0)
return NotFound();
// Transform data
PickingListEntry[] result;
try {
result = PickingListEntry.FromServiceResponse(workOrders).ToArray();
} catch (Exception ex) {
return Problem("Data could not be transformed. " + ex.Message);
}
// Maybe apply the filters to PickingListEntry here <-------------------
// Create response object and return
return Ok(result);
}