Skip to content

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

Open
@perilbrain

Description

@perilbrain

In following function

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

@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

<ElementList>
  <Element>
    <p1>p1</p1>
    <p2>p2</p2>
  </Element>
  <Element>
    <p1>p1</p1>
    <p2>p2</p2>
  </Element>
</ElementList>

getting:

<ElementList>
  <item>
    <p1>p1</p1>
    <p2>p2</p2>
  </item>
  <item>
    <p1>p1</p1>
    <p2>p2</p2>
  </item>
</ElementList>

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:

[ 
  {"p1": "p1", "p2": "p2"},
  {"p1": "p1", "p2": "p2"}
]

for xml:

<ElementList>
  <Element>
    <p1>p1</p1>
    <p2>p2</p2>
  </Element>
  <Element>
    <p1>p1</p1>
    <p2>p2</p2>
  </Element>
</ElementList>

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions