• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSON Formatting with Jersey, Jackson, & Google's GSON

 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Java 6, Tomcat 7, Jersey 1.15, Jackson 2.0.6 (from FasterXml maven repo), & Google GSON 2.2.2,
I am trying to pretty print the JSON String so it will look indented by the curl -X GET command line.

I created a simple web service which has the following architecture:

My POJOs (model classes):





Using a Utility Class, I decided to hard code the POJOs as follows:



My web service:



When I run this using:



I get this in my Eclipse's console (which is exactly what I want):



But from the command line curl command listed above, I get this
(there are 4 spaces after \n but JavaRanch's forum doesn't show it):


"{\n \"father\": \"Joe\",\n \"mother\": \"Jennifer\",\n \"children\": [\n {\n \"name\": \"Jimmy\",\n \"age\": \"12\",\n \"gender\": \"male\"\n }\n ]\n}"


How can I make the JSON formatting to be identical to what I am getting in Eclipse's console by using the curl command?

This is similar to my other two posts, located using Jackson and Using json.org/java. The only reason I am posting using all 3 parsers is because I want to help other people with similar problems who are using a specific JSON library of choice, in the future.

Thanks for taking the time to read this...
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its not required to club Jackson and Gson. though I was not able to reproduce the problem you seeing by mixing it. I am just using following code



may be something to do with the way your OS is treating it, I am using cgywin on windows and get following output

 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praful,

It still didn't work on Linux... Thank you for your help, nonetheless.

-James
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeha, I as mention its to do with OS, as webservice is corrently adding \n to the ouput along with indentation. so you may want to capture the output of webservice in variable and echo it using -e.
when i tried this



I get output as is, but adding -e to echo i.e echo -e ... I get the formatted output.

Of course I am trying on cywin as I don't have Linux.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic