users@jersey.java.net

[Jersey] Re: How to avoid JSON marshalling over an already JSON object?

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Thu, 28 Jul 2011 10:43:00 +0200

Hi Juan,

You will need to provide your custom MessageBodyWriter [1],
which will shield the default one (that is the default behaviour).
Your custom provider has to just copy the given String to the entity stream.
Look at the entity provider example for details [2].

~Jakub

[1]http://jsr311.java.net/nonav/releases/1.1/javax/ws/rs/ext/MessageBodyWriter.html
[2]http://download.java.net/maven/2/com/sun/jersey/samples/entity-provider/1.8/entity-provider-1.8-project.zip

On 27.7.2011 2:21, jamoros wrote:
> Hi!
>
> I have a problem. I need one of the methods that serves the my REST API to
> produce "application/json". The problem is that I inside that method the
> information is already in JSON format. So, Jersey makes the marshalling over
> a marshalled data.
>
> The code:
> /_at_GET
> @Path("/query.json")
> public Response excecuteSPARQLJSONQuery(@QueryParam(value = "q") String
> query) {
>
> return excecuteQuery(query,Format.JSON,Language.SPARQL);
> }
>
> private Response excecuteQuery(String query, Format format, Language
> language){
> ...
>
> try {
> body = repositoryGateway.query(format, query,
> language);
> ...
> resp = Response.ok(body,MediaType.APPLICATION_JSON_TYPE).build();
> }else{
> resp = Response.ok(body,MediaType.APPLICATION_XML_TYPE).build();
> }
> ...
> ...
> return resp;
> }/
>
> I would like to get for example something like this:
>
> *{
> "head": {
> "vars": [ "s", "p", "o" ]
> }
> }*
>
> Instead of that i get this:
>
> *"{\n\t\"head\": {\n\t\t\"vars\": [ \"s\", \"p\", \"o\" ]\n\t}\n}"*
>
> It looks like jersey replace the \n and \t and marshall the json inside
> another json.
>
> How can avoid this behaviour?
>
>
> Thanks in advance,
>
> Juan.
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/How-to-avoid-JSON-marshalling-over-an-already-JSON-object-tp6624356p6624356.html
> Sent from the Jersey mailing list archive at Nabble.com.
>