Skip to content

XML Serialization + JsonAnyGetter as attributes #309

@asa-git

Description

@asa-git

Hi there,

I'm having some issues with the serialization of dynamic data as attributes of an XML element.
Note: Tested on both version 2.9.6 and 2.9.7

Given the Object:

@JacksonXmlRootElement(localName = "metadata")
private static class ObjectWithAttributes {

	@JsonAnyGetter
	public Map<String, String> getData() {
		Map<String, String> map = new HashMap<>();
		map.put("a", "some value");
		map.put("b", "and another one");
		return map;
	}
}

I was expecting to get the following XML :

<metadata a="some value" b="and another one" />

but got:

<metadata>
  <a>some value</a>
  <b>and another one</b>
</metadata>

I tried to fiddle around with some of the annotations available be never got it to serialize the values as attributes.
I even tried to write a custom serializer which worked fine when annotating the class with the @JsonSerialize(using =...) but would results in any other attribute fields to not be serialized :(

I couldn't find any documentations regarding the uses of @JsonAnyGetter to get the values as attributes and not sub elements.

Is this use case not supported by Jackson ?

Thanks in advance,
Best regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xIssue planned for 3.x (and not 2.x)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions