users@jersey.java.net

[Jersey] Re: Parsing JSON with _at_-marked attributes

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Wed, 2 Oct 2013 08:55:24 -0700

On Wed, Oct 2, 2013 at 7:44 AM, feldberg ian <ianfeldberg_at_yahoo.com> wrote:

> Hi. I'm using Jersey 1.x to serve up some JAXB-annotated Java objects
> through a REST endpoint. When I request these objects in JSON format
> (application/json) they correctly get served, that is, fields in the
> object annotated as @XmlAttribute's have an at sign prepended to their
> name. Then when I PUT or POST those strings back to the REST endpoint, they
> get correctly parsed as attributes and not XML elements. Good Jersey.
>

I think '@' is only added (and expected) by specific libraries, like
Jettison.
There is nothing in JAXB specfication about JSON, since it is XML-specific
specification.
Jettison would work this way, I think, so while it has a host of other
issues (related to handling of arrays, Lists, especially), here it might be
your simplest choice.

Given that use of '@' is feature of specific library, Jackson only uses
explicit information given by annotations; and so @XmlAttribute alone is
not indicative of non-standard name. If it's name (or was it value)
property said that was something like "@myProperty", it would use that
name; otherwise whatever getter indicates name to be.

While there is no simple feature or flag to set in Jackson for this, it is
possible to override methods in AnnotationIntrospector to take
@XmlAttribute to mean addition of at character. It is some work (maybe 4
methods or so to override) but should work.

Also going forward, if this is widely used convention, it would be possible
to add as a feature for Jackcson's JAXB module (piece of code that
understands JAXB annotations), to make it simple on/off thing. If so, issue
tracker is at:

https://github.com/FasterXML/jackson-module-jaxb-annotations/issues

I hope this helps,

-+ Tatu +-