users@jersey.java.net

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

From: feldberg ian <ianfeldberg_at_yahoo.com>
Date: Wed, 2 Oct 2013 11:12:42 -0700 (PDT)

Thank you all for the feedback. I guess what I don't get is this: I have a simple REST @GET endpoint that returns a JAXB object like so:
 
@GET
@Produces('application/xml','application/json')
public Graph findGraph() {
.....
 
That JAXB object is created by Netbeans from an XML schema and starts off like so:
 
@XmlRootElement
public class Graph {
    @XmlAttribute(name = "Graphic")
    protected String Graphic
    ... a bunch of other fields
 
Pretty simple. And when I call that endpoint and ask for JSON output I get:
 
[ '_at_Graphic' : 'whatever', ...]
 
I'm using Jersey 1.9.1. I'm not asking for any special variant of JSON (BadgerFish, Natural, Mapped) so I'm just getting whatever Jersey1.9.1 serves up as it's default JSON. The question: what is Jersey 1.9.1 using to generate JSON? It doesn't seem to be Jackson, at least not plain vanilla Jackson. Is it Jettison? If I could find out I could at least start with that library to try and parse the incoming JSON.
 

________________________________
 From: Tatu Saloranta <tsaloranta_at_gmail.com>
To: feldberg ian <ianfeldberg_at_yahoo.com>
Cc: "users_at_jersey.java.net" <users_at_jersey.java.net>
Sent: Wednesday, October 2, 2013 11:55 AM
Subject: [Jersey] Re: Parsing JSON with @-marked attributes
  


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 +-