Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Google%20Gson%20Interview%20Questions%20and%20Answers

Question: How to provide Pretty Printing for JSON Output Format?
Answer:
The default JSON output that is provide by Gson is a compact JSON format. This means that there will not be any white-space in the output JSON structure. To generate a more readable and pretty looking JSON use setPrettyPrinting() in GsonBuilder.

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(employee);
 
Output:

{
  "id": 1,
  "firstName": "Lokesh",
  "lastName": "Gupta",
  "roles": [
    "ADMIN",
    "MANAGER"
  ],
  "birthDate": "17/06/2014"
}
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook