FasterXML / FasterXML/jackson-dataformat-xml

Allow custom prefix for attributes name

未關閉
#667 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Java
星號
631
分支
246
平均合併
7 天 9 小時
30 天內合併 PR
13

描述

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(" WHITE
```
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(" WHITE
// 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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。