users@jersey.java.net

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

From: feldberg ian <ianfeldberg_at_yahoo.com>
Date: Thu, 3 Oct 2013 19:31:30 -0700 (PDT)

Thanks again all for helping me out. I tried parsing the Jersey1.9.1-produced JSON with Jettison but no dice. I don't have the code in front of me but I tried both the Jettison BadgerFishXMLStreamReader and the MappedXMLStreamReader  but they both produced fairly obtuse syntax errors about the JSON I was trying to parse. I even tried all the different options in jettison.mapped.Configuration to no avail.

I'm thinking about trying something different. Is there any way I could just contact one of the Jersey developers directly and ask them? It seems like a fairly simple question - what does Jersey v1.9.1 use to marshal JSON and what options/mapping do they use? I feel like that would have to be more productive than me just grasping at straws trying to reverse-engineer it.


________________________________
 From: Tatu Saloranta <tsaloranta_at_gmail.com>
To: feldberg ian <ianfeldberg_at_yahoo.com>
Sent: Wednesday, October 2, 2013 7:24 PM
Subject: Re: [Jersey] Re: Parsing JSON with @-marked attributes
 


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

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

Ah. Not Jackson then, no. Jackson does not add '@'s.


I am guessing it is either Jettison, or what they refer to... "natural"?

Jackson can be enabled explicitly (add provider during bootstrapping), or by what Jersey documentation calls "POJO mapping".


-+ Tatu +-


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