I would be happy to use either of NewtonSoft or System.Text.Json deserializers.
I’ll have many different responses coming from an API, and would prefer not to write up seperate response objects for each.
For example, every response has a message property and a status code property, then it’s actual data.
So, what I’d like to have is
public class ApiResponse<T>
{
public string StatusCode{ get; set; }
public string Message{ get; set; }
public ICollection<T> Data { get; set; }
}
The issue is that, while I can correctly set ‘T’, I can’t have the deserialization map to it, because it expects the property to be “Users” or “Purchases” etc., not “data”.
I am aware I can set JsonProperty[] decorator, but the issue is this isn’t dynamic with T.
If there’s technically a way but it’s no cleaner than just creating separate response objects for each I’m happy to continue to write up separate objects; but would be interested in a better solution I haven’t found if one exist.
Owen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.