Allow custom prefix for attributes name

Đang mở
#667 4 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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/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 bằng cách lần theo XMLMapper và MapSerializer hiện có để tìm hiểu cách xử lý đầu vào và đầu ra của các mục trong map, sau đó xác định đường dẫn deserialization tương ứng. Công việc được hoàn tất khi một tiền tố thuộc tính có thể cấu hình phân biệt thuộc tính với phần tử, hỗ trợ hành vi tương thích cho null và tiền tố rỗng, đồng thời xử lý ví dụ XML được cung cấp theo cả hai chiều.

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

Mô tả

The default behavior of the XML deserialization, when reading into non-bean objects is to treat attributes the same way as elements. On serialization, I could not find way to generate attributes - all hash map entries were converted into objects. The only way I found to be able to tell the difference between elements and attributes in the outbound was to write a custom serializer, still working on trying to get this done with inbound serializer.

Example:

var mapper = new XMLMapper();
Object value = mapper.readValue("<DATA FOO="BAR"> <COLOR> WHITE </COLOR> </DATA>

The resulting object will be set of LinkedHashMap, with no obvious way to tell the different between the attributes and the elements.

(LinkedHashMap) {
    "FOO": "BAR",
    "COLOR": "WHITE",,
}

Would like to suggest/ask for a new setting on the XMLMapper that will allow specifying a prefix for attributes. On the inbound, each attribute will be stored with the prefix inside the key. In my case, I'm using '@' as prefix, there the example above will result in

var mapper = new XMLMapper();
Object value = mapper.readValue("<DATA FOO="BAR"> <COLOR> WHITE </COLOR> </DATA>
// Define Custom Prefix
mapper.setAttributPrefix("@");

Result:

(LinkedHashMap) {
    // FOO is prefixed with '@'
    "@FOO": "BAR",
    "COLOR": "WHITE",,
}

For the Serializer - same logic in reverse. If map key starts with the designated prefix it will be converted into an attribute (with the prefix removed). For backward compatibility:

  • If the prefix is NULL (or not set), current logic remain
  • If the prefix is "" (empty string), every map entry will be converted into attribute with the same name (subject to the text attribute, which already has special handling).

While the custom serializer was short to write - It took some googling/effort to write, and I believe it's not as robust as the built in logic that is currently implemented in the MapSerializer. Will address lot of use cases when it's critical separate attribute/elements, without having to predefined beans.

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.