序列化的儿童

发布于 2025-02-12 03:43:42 字数 1308 浏览 2 评论 0原文

我必须消耗一个REST API,其中内容在子列表中具有可变结构。

这是一个简化的表示A 小型汽车具有多个组件: - 窗口,引擎和门

{
   "ProductCode": 1234,
   "ProductName": "SmallCar",
   "Components": [
      {
          "PartType": "Window",
          "Operation": "Electric",
          "Tinted": true,
      },
      {
          "PartType": "Engine",
          "Capacity": 2000,
          "Cylinders": 6,
          "Layout": "V6"
      },
      {
          "PartType": "Door",
          "DoorStyle": "Standard"
      }
   ]
}

定义了我的Poco类如下: -

public class Product
{
    public int ProductCode {get; set;}
    public string ProductName {get; set;}
    public List<Component> Components {get; set;}
}

public class Component
{
    public string PartType {get; set;}
}

public class Window : Component
{
    public string Operation {get; set;}
    public bool Tinted {get; set;}
}

public class Engine : Component
{
    public int Capacity {get; set;}
    public int Cylinders {get; set;}
    public string Layout {get; set;}
}

public class Door : Component
{
    public string DoorStyle {get; set;}
}

在避难所期间,如何获得它来创建适当的组件对象(窗口,门,发动机)。使用var product = deserializeObject&lt; product&gt;(....)我只是获得组件 parttype 设置的列表,但我需要根据parttype的列表为每个类类型

I have to consume a Rest API where the content has variable structure in a child list ..

Here is a simplified representation that a Small Car has several components:- Window, Engine and Door

{
   "ProductCode": 1234,
   "ProductName": "SmallCar",
   "Components": [
      {
          "PartType": "Window",
          "Operation": "Electric",
          "Tinted": true,
      },
      {
          "PartType": "Engine",
          "Capacity": 2000,
          "Cylinders": 6,
          "Layout": "V6"
      },
      {
          "PartType": "Door",
          "DoorStyle": "Standard"
      }
   ]
}

My POCO classes are defined as below:-

public class Product
{
    public int ProductCode {get; set;}
    public string ProductName {get; set;}
    public List<Component> Components {get; set;}
}

public class Component
{
    public string PartType {get; set;}
}

public class Window : Component
{
    public string Operation {get; set;}
    public bool Tinted {get; set;}
}

public class Engine : Component
{
    public int Capacity {get; set;}
    public int Cylinders {get; set;}
    public string Layout {get; set;}
}

public class Door : Component
{
    public string DoorStyle {get; set;}
}

During the deserialization, how can I get it to create the appropriate component object (Window, Door, Engine). With var product = DeserializeObject<Product>(....) I just get a list of Components with PartType set, but I need the list to be of each class type according to the PartType

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2025-02-19 03:43:43

您可以使用自定义JSON转换器如果您使用的是system.text.json,并且有一个示例,说明如何做

public class ComponentJsonConverter : JsonConverter<Component>
{
  public override Component Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) 
  {
    var type = JsonDocument.Parse(reader.GetRawText())
      .RootElement.GetProperty("PartType").GetString();
    if (type == "Engine")
      return JsonSerializer.DeserializeAsync<Engine>(reader.GetRawText());
    ...
  }
    
  public override void Write(Utf8JsonWriter writer, Component value, JsonSerializerOptions options) =>
    writer.WriteStringValue(JsonSerializer.Serialize(value, options));
}

you can use Custom Json Converter if you are using system.text.json and there is an example of how you can do it

public class ComponentJsonConverter : JsonConverter<Component>
{
  public override Component Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) 
  {
    var type = JsonDocument.Parse(reader.GetRawText())
      .RootElement.GetProperty("PartType").GetString();
    if (type == "Engine")
      return JsonSerializer.DeserializeAsync<Engine>(reader.GetRawText());
    ...
  }
    
  public override void Write(Utf8JsonWriter writer, Component value, JsonSerializerOptions options) =>
    writer.WriteStringValue(JsonSerializer.Serialize(value, options));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文