不支持使用 System.Text.JSON 反序列化接口类型

joh*_*ohn 14 c# json deserialization system.text.json

我正在将有效的 JSON 对象传递到 .net core 3 Web api 应用程序上的控制器。这样做,我收到错误:

System.NotSupportedException:不支持接口类型的反序列化。输入“OrderTranslationContracts.OrderContracts+IImportOrderLineModel”

所以我查看了我的代码,并得到了以下接口的具体实现。这是我认为引发错误的行:

   public List<OrderContracts.IImportOrderLineModel> Lines { get; set; }
Run Code Online (Sandbox Code Playgroud)

以下是我传递到控制器的 JSON 部分:

"lines": [
        {
            "orderNumber": "LV21131327",
            "lineNumber": 1,
            "itemId": "3083US",
            "customerItemId": "3083US",
            "quantity": 3,
            "price": 0.00,
            "quantityBackOrdered": null,
            "comments": "",
            "pickLocation": "",
            "orderFilled": "O",
            "hostUom": null,
            "type": null
        }
Run Code Online (Sandbox Code Playgroud)

所以我知道 JSON 是有效的。这是控制器的签名:

[HttpPost]
    public async Task<List<ImportOrderModel>> Post([FromBody] List<ImportOrderModel> orders)
    {
        var response = await _validateOrder.ValidateAllOrdersAsync(orders, null);
        return response;
    }
Run Code Online (Sandbox Code Playgroud)

我什至没有闯入这段代码,因为我假设 JSON 反序列化器在尝试转换它时抛出错误。那么我该如何克服这个错误呢?我受到接口的具体实现的约束,因此我无法更改接口,如果可能的话,我需要使用我这里拥有的接口。如果这是不可能的,是否有任何“解决方法”?

Chr*_*lin 5

HttpClient.GetFromJsonAsync 我尝试过遇到同样的问题httpClient.GetFromJsonAsync<ICustomer>(url);

我收到错误:

不支持使用 System.Text.JSON 反序列化接口类型

据我所知,必须有一个模型才能反序列化 InterfaceType。我的解决方案使用数据注释在界面中定义模型。

  1. 创建一个 TypeConverter (我在这里找到了这个类:Casting faces for deserialization in JSON.NET

    使用 Newtonsoft.Json;

    public class ConcreteTypeConverter<TConcrete> : 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)
         {
             //explicitly specify the concrete type we want to create
             return serializer.Deserialize<TConcrete>(reader);
         }
    
         public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
         {
             //use the default serialization - it works fine
             serializer.Serialize(writer, value);
         }
     }
    
    Run Code Online (Sandbox Code Playgroud)

2 在界面中添加数据注释 ( [JsonConverter(typeof(ConcreteTypeConverter<AddressModel>))])

using Newtonsoft.Json;
public interface ICustomer
{
    int Id { get; set; }
    int Name { get; set; }

    [JsonConverter(typeof(ConcreteTypeConverter<AddressModel>))]
    IAddress Address { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

3 不幸的HttpClient.GetFromJsonAsync是没有使用Newtonsoft。我自己写了一个方法

public async static Task<T> GetJsonAsync<T>(HttpClient client, string url)
{
    using var response = await client.GetAsync(url);
    response.EnsureSuccessStatusCode();

    using Stream stream = await response.Content.ReadAsStreamAsync();
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        return JsonConvert.DeserializeObject<T>(reader.ReadToEnd(), new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Objects,
            NullValueHandling= NullValueHandling.Ignore
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

4 现在我可以使用:

 HttpClient httpClient= new HttpClient();
 string url="https://example.com/api/customerlist";
 var myCustomerList[]=await GetJsonAsync<CutomerModel[]>(httpClient, url);
Run Code Online (Sandbox Code Playgroud)


Ash*_*ani -7

这里:

public List<OrderContracts.IImportOrderLineModel> Lines { get; set; }
Run Code Online (Sandbox Code Playgroud)

您的列表属于IImportOrderLineModel接口类型。

它应该像

 public List<ImportOrderLineModel> Lines { get; set; }
Run Code Online (Sandbox Code Playgroud)

ImportOrderLineModel是一个实现它的类,如下所示:

public class ImportOrderLineModel : IImportOrderLineModel
{
    //......
}
Run Code Online (Sandbox Code Playgroud)

  • OP 应该使用 JsonConverter 将接口映射到具体类型。以这种方式使用具体实现违背了接口的目的(并且违反了 SOLID)。 (10认同)