FasterXML / FasterXML/jackson-dataformat-xml

Hard coding for Indexed type item/wrapper name removes all possiblity of customization

未关闭
#356 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Java
星标
631
派生
246
平均合并
7 天 9 小时
30 天内合并 PR
13

描述

https://github.com/FasterXML/jackson-dataformat-xml/blob/6bd59fb77aa27bfba8d51eb36a346d218f201142/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java#L146

In following function
```java
protected void _startRootArray(ToXmlGenerator xgen, QName rootName) throws IOException
{
xgen.writeStartObject();
// Could repeat root name, but what's the point? How to customize?
xgen.writeFieldName("item");
}
```

Although `rootName` has been passed it is not being used. This makes it impossible to change array type wrapper name. If someone needs `item` as their wrapper they can set `@JacksonXmlRootElement(localName="item")` but otherwise is not possible.

Alternatively we can have another attribute in annotation like `@JacksonXmlRootElement(localName="some_node", wrapperForIndexedType="some_node")`
having interface declaration of _JacksonXmlRootElement.java_ as
```java
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface JacksonXmlRootElement
{
String namespace() default "";
String localName() default "";
String wrapperForIndexedType() default "item";
}
```

___
Problem:
Trying to generate output like
```xml


p1
p2


p1
p2

```

getting:
```xml


p1
p2


p1
p2

```
However if hard-coding is removed it would be easier to generate json and xml from a same function and entity definition with some varied level of alteration at XML layer. This avoids re-declaring everything to get two different representation of output i.e. json and xml.

For example in my case I have to port an old API which has response format for json:
```json
[
{"p1": "p1", "p2": "p2"},
{"p1": "p1", "p2": "p2"}
]
```
for xml:
```xml


p1
p2


p1
p2

```
but I cannot achieve the xml format without rewriting all POJOs.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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