Support XML element name based on runtime type

Đang mở
#679 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Bắt đầu với mô hình Java và ví dụ substitution-group của XSD, sau đó kiểm tra cách các annotation của Jackson XML hiện xác định tên phần tử trong quá trình tuần tự hóa và giải tuần tự. So sánh các instance XML của cow và goat với hành vi mong muốn của kiểu tại thời điểm chạy; được coi là hoàn tất khi cả hai dạng đều được ánh xạ tới subtype Animal phù hợp và được tuần tự hóa trở lại bằng tên phần tử tương ứng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

XSD schema's support a way to name an element based on its runtime type through substitution groups. I'm trying to deserialise such a substitution group. Is this supported in some way?

Example. Suppose there is two types of Animals: Cows and Goats. Based on their type, I want their element to be named either cow or goat. This would be modelled as follows:

XSD:

<!-- XML root element -->
<xs:element name="document">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="animal"/> <!-- Note that the name of this element is determined by the actual type of animal -->
    </xs:sequence>
  </xs:complexType>
</xs:element>

<!-- Element names: `Cow`s should be serialised as an element named `cow`, `Goat`s should be serialised as an element named `goat`. -->
<xs:element name="animal" type="Animal" abstract="true" />
<xs:element name="cow" type="Cow" substitutionGroup="animal" />
<xs:element name="goat" type="Goat" substitutionGroup="animal" />

<!-- Types: `Cow` and `Goat` extend from `Animal` -->
<xs:complexType name="Animal">
  <xs:attribute name="name" type="xs:string" />
</xs:complexType>
<xs:complexType name="Cow">
  <xs:complexContent>
    <xs:extension base="Animal" />
  </xs:complexContent>
</xs:complexType>
<xs:complexType name="Goat">
  <xs:complexContent>
    <xs:extension base="Animal" />
  </xs:complexContent>
</xs:complexType>

XML example instances:

<document>
    <cow name="Cow1" />
</document>
<document>
    <goat name="Goat1" />
</document>

Java model:

@XmlRootElement(name="document")
public class Document {
  private Animal animal;

  public Animal getAnimal() {
    return animal;
  }
  public void setAnimal(Animal animal) {
    this.animal = animal;
  }
}

public class Animal {
  @JacksonXmlProperty(isAttribute = true)
  private String name;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

public class Cow extends Animal {}

public class Goat extends Animal {}

Is there a way of annotating the classes to get the desired behaviour?

Ngôn ngữ chính
Java
Star
636
Fork
250
Merge trung bình
7 giờ 10 phút
Pull request đã merge (30 ngày)
8

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của FasterXML/jackson-dataformat-xml

Tất cả issue của FasterXML/jackson-dataformat-xml

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.