Allow custom prefix for attributes name

未关闭
#667 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
java

调研方向

首先跟踪 XMLMapper 和现有的 MapSerializer,了解 map 条目的入站和出站处理,然后确定对应的反序列化路径。当可配置的属性前缀能够区分属性和元素,支持 null 和空前缀的兼容行为,并且能够双向处理所提供的 XML 示例时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Java
星标
636
派生
250
平均合并
7 小时 10 分钟
30 天内合并 PR
8

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

FasterXML/jackson-dataformat-xml 的其他 Issue

查看 FasterXML/jackson-dataformat-xml 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。