aspose file tools*
The moose likes Web Services and the fly likes JSON Formatting with Jersey, Jackson, & Google's GSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JSON Formatting with Jersey, Jackson, & Google Watch "JSON Formatting with Jersey, Jackson, & Google New topic
Author

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

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 219
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

Joined: Feb 10, 2001
Posts: 641
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



All desirable things in life are either illegal, banned, expensive or married to someone else !!!
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 219
Praful,

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

-James
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSON Formatting with Jersey, Jackson, & Google's GSON