小编Nor*_*and的帖子

JSonConverter how to make generic deserialization

I have been able to do a custom Converter that transforms to a list of interfaces.

Here my custom converter:

public class InvoiceDetailConverter : JsonConverter {
    public override bool CanConvert(Type objectType) {
        //assume we can convert to anything for now
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        List<InvoiceDetail> data = serializer.Deserialize<List<InvoiceDetail>>(reader);

        // now loop to make the proper list
        List<IInvoiceDetail> result = new List<IInvoiceDetail>();
        foreach (IInvoiceDetail d in data) {
            result.Add(d);
        } …
Run Code Online (Sandbox Code Playgroud)

c# json json.net deserialization asp.net-core

4
推荐指数
1
解决办法
5417
查看次数

标签 统计

asp.net-core ×1

c# ×1

deserialization ×1

json ×1

json.net ×1