users@jersey.java.net

RE: [Jersey] json object value string has extra quotes, jsonp, service chaining

From: stuart mellanby <stuart_mellanby_at_hotmail.com>
Date: Thu, 11 Jun 2009 12:55:11 +0000

Paul,

 

Thanks for the response. I'm impressed you make sense of my madness.

 

using

 

String s = "{\"ABCD\" : 123}";
JSONObject o = new JSONObject();
o.put("result", new JSONObject(s));
return new JSONWithPadding(o, callback);

 

works as I needed.

 

Thanks,

 

Stuart

 

 
> Date: Thu, 11 Jun 2009 12:19:55 +0200
> From: Paul.Sandoz_at_Sun.COM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] json object value string has extra quotes, jsonp, service chaining
>
> Hi Stuart,
>
> The problem is the JAXB the JSON conversion is correctly quoting the
> string obtained from the client response because it is a string value
> and not JSON syntax, this is because the element of the QueryResult is
> a text element that is not meant to directly contain further structure.
>
> You will get the same if you do the following:
>
> String s = "{\"ABCD\" : 123}";
> JSONObject o = new JSONObject();
> o.put("result", s);
> return new JSONWithPadding(o, callback);
>
> Also JAXB is not the right thing to use here because you are mixing
> layers and syntaxes.
>
> Since what you are getting back from the client is JSON you can do:
>
> String s = "{\"ABCD\" : 123}";
> return new JSONWithPadding(o, s);
>
> or if you want the "result" object and validation of what the JSON
> returned do:
>
> String s = "{\"ABCD\" : 123}";
> JSONObject o = new JSONObject();
> o.put("result", new JSONObject(s));
> return new JSONWithPadding(o, callback);
>
> or if you do not require validation do:
>
> String s = "{\"ABCD\" : 123}";
> return new JSONWithPadding(o, "{\"result\": " + s + "}");
>
> Paul.
>
> On Jun 11, 2009, at 12:13 AM, stuart mellanby wrote:
>
> > My short question is why does my jsonp response have an extra set of
> > quotes around the json object, my second is how do I fix it. I'm so
> > close to it all working.
> >
> > I am returning a jsonp response from two different approaches. The
> > first is by using jaxb annotation objects, this works well. (see
> > callback1)
> > The second is by wrapping/chaining another external rest service
> > into a string and returning the reponse as below using Client, this
> > doesn't work (callback2). I think my problem is in the string and
> > the jaxb object queryResult's String result property.
> >
> > @GET @Path("test")
> > @Produces({"application/javascript","application/json"})
> > public JSONWithPadding getQueryResult(@QueryParam("callback") String
> > callback, @QueryParam("param1") double param1 ... etc)
> > {
> > ...
> > Client client = Client.create();
> > WebResource wr = client.resource(sb.toString()); //sb is a rest
> > service url
> >
> > String stringResponse = wr.get(String.class);
> > QueryResult = new QueryResult();
> > queryResult.setResult(stringResponse); // seems fine {}, not "{}"
> >
> > return new JSONWithPadding(new
> > GenericEntity<QueryResult>(queryResult){}, callback);
> > }
> >
> >
> > ....
> > the responses are
> > callback1({"correctResult":{"location":"xxx,"status":"false"}) // no
> > extra quote before json object/location, this was built using jaxb
> > object, not a string returned as above in getqueryresult
> > callback2({"queryResult":"{\"displayFieldName\" //see extra quote
> > before {\, so String representation of property result's json not
> > valid... my client could be coded to be ok with this, but it's wrong..
> > ...
> > //pseudo example
> > @XmlAccessorType(XmlAccessType.FIELD)
> > @XmlType(name = "queryResult", propOrder = {"result"})
> > @XmlRootElement(name="queryresult")
> > public class QueryResult {
> >
> > @XmlElement(required = true,name="result")
> > protected String result; // i think this the
> > problem beastie, the string being added is ok but when jersey makes
> > this a callback json element it adds it with extra quotes. Is my
> > client.resource to blame? what would solve the double quote issue?
> >
> > /**
> > * Gets the value of the result property.
> > *
> > * @return
> > * possible object is
> > * {_at_link String }
> > *
> > */
> > public String getResult() {
> > return result;
> > }
> >
> > /**
> > * Sets the value of the result property.
> > *
> > * @param value
> > * allowed object is
> > * {_at_link String }
> > *
> > */
> > public void setResult(String value) {
> > System.out.println(value);
> > this.result = value;
> >
> > }
> >
> > }
> >
> >
> >
> >
> >
> > View your Twitter and Flickr updates from one place – Learn more!
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

_________________________________________________________________
Get the best of MSN on your mobile
http://clk.atdmt.com/UKM/go/147991039/direct/01/