In this post, I am going to explain different was to pretty print a map content in Java.
Standard way is to convert the map to json and pretty print it. There are multiple libraries available to convert the map to json.
a. Using org.json library
b. Using gson library
c. Using Jackson
d. Using Genson
e. Using javax.json
f. Using moshi
Using org.json library
PrettyPrintMapUsingOrgJson.java
package com.sample.app.collections;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
public class PrettyPrintMapUsingOrgJson {
public static void main(String args[]) {
Map<Integer, Object> map = getMap();
JSONObject jsonObject = new JSONObject(map);
String prettyJson = jsonObject.toString(4);
System.out.println(prettyJson);
}
private static Map<Integer, Object> getMap() {
Map<Integer, Object> map = new HashMap<> ();
Map<Integer, Object> nestedMap1 = new HashMap<> ();
nestedMap1.put(1, "One");
nestedMap1.put(2, "Two");
nestedMap1.put(3, "Three");
map.put(4, "Four");
map.put(0, nestedMap1);
return map;
}
}
Output
{ "0": { "1": "One", "2": "Two", "3": "Three" }, "4": "Four" }
Dependency used
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
Using gson library
PrettyPrintMapUsingGson.java
package com.sample.app.collections;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class PrettyPrintMapUsingGson {
public static void main(String args[]) {
Map<Integer, Object> map = getMap();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(map);
System.out.println(prettyJson);
}
private static Map<Integer, Object> getMap() {
Map<Integer, Object> map = new HashMap<>();
Map<Integer, Object> nestedMap1 = new HashMap<>();
nestedMap1.put(1, "One");
nestedMap1.put(2, "Two");
nestedMap1.put(3, "Three");
map.put(4, "Four");
map.put(0, nestedMap1);
return map;
}
}
Output
{ "0": { "1": "One", "2": "Two", "3": "Three" }, "4": "Four" }
Dependency used
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
Using Jackson library
PrettyPrintMapUsingJackson.java
package com.sample.app.collections;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class PrettyPrintMapUsingJackson {
public static void main(String args[]) throws JsonProcessingException {
Map<Integer, Object> map = getMap();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String prettyJson = mapper.writeValueAsString(map);
System.out.println(prettyJson);
}
private static Map<Integer, Object> getMap() {
Map<Integer, Object> map = new HashMap<> ();
Map<Integer, Object> nestedMap1 = new HashMap<> ();
nestedMap1.put(1, "One");
nestedMap1.put(2, "Two");
nestedMap1.put(3, "Three");
map.put(4, "Four");
map.put(0, nestedMap1);
return map;
}
}
Output
{ "0" : { "1" : "One", "2" : "Two", "3" : "Three" }, "4" : "Four" }
Dependency used
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Using Genson library
PrettyPrintMapUsingGenson.java
package com.sample.app.collections;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
public class PrettyPrintMapUsingGenson {
public static void main(String args[]) throws JsonProcessingException {
Map<Integer, Object> map = getMap();
Genson prettyGenson = new GensonBuilder().useIndentation(true).create();
String prettyJson = prettyGenson.serialize(map);
System.out.println(prettyJson);
}
private static Map<Integer, Object> getMap() {
Map<Integer, Object> map = new HashMap<> ();
Map<Integer, Object> nestedMap1 = new HashMap<> ();
nestedMap1.put(1, "One");
nestedMap1.put(2, "Two");
nestedMap1.put(3, "Three");
map.put(4, "Four");
map.put(0, nestedMap1);
return map;
}
}
Output
{ "0":{ "1":"One", "2":"Two", "3":"Three" }, "4":"Four" }
Dependencies used
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.6</version>
</dependency>
No comments:
Post a Comment