Heiko Rupp
Heiko Rupp

Reputation: 30994

Jackson automatically map properties named @foo?

When playing with REST, my provider is generating JSON with attribute names starting with an at sign when the property is marked as @XmlAttibute like this:

@XmlAttribute
int foo = 1;

will return

{"@foo":1}

How can I tell Jackson for deserializing that if I have on the client

int foo;

that it should take the json-Attribute @foo for this. Or in more general terms: how to tell Jackson to ignore the @ when deserializing?

Update: I know about @JsonProperty("@foo") annotation that StaxMan is referring - I forgot to put that in my original question, as I was especially interested in a "global setting" and not on a per property level.

Upvotes: 0

Views: 1558

Answers (2)

Alkanshel
Alkanshel

Reputation: 4449

I think you want @XmlElement rather than @XmlAttribute. Values for the latter are always given a @ at the beginning.

Upvotes: 0

StaxMan
StaxMan

Reputation: 116600

Easiesti thing might be to disable adding those '@' signs there as they seem useless. I know some XML-to-JSON libs (Jettison) want to use this to differentiate between XML attributes and elements, but it's of little use with actual JSON processing.

But Jackson can be given expected property name in JSON with @JsonProperty annotation:

@JsonProperty("@foo")
public int foo; // or add in setter

if it is necessary to keep those at signs in there.

Upvotes: 4

Related Questions