I'm having issue trying to print array in Jackson vertically. I managed to get it to pretty print, but is there a way to print array vertically? (see the result section below, list of friends Anne and Bill)
I know in this example it's very simple, but in my real code the array can be more than a hundred, which will be better to show it vertically.
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestJackson{
public void testJackson() throws JsonProcessingException{
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.ANY);
objectMapper.writer().withDefaultPrettyPrinter();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
MyClass myPojo = new MyClass();
String report = objectMapper.writeValueAsString(myPojo);
System.out.println(report);
}
}
My Pojo:
class MyClass{
private String name="John";
private List<String> friends = new LinkedList<String>();
private Address homeAddress = new Address("20", "Main St.");
private Address workAddress = new Address("11", "Corner St.");
public String getName(){
return name;
}
public List<String> getFriends(){
if (friends.size() == 0) {
friends.add("Anne");
friends.add("Bill");
}
return friends;
}
public Address getHomeAddress(){
return homeAddress;
}
public Address getWorkAddress(){
return workAddress;
}
}
class Address{
private String no;
private String street;
public Address(String no, String street){
this.no = no;
this.street = street;
}
public String getNo(){
return no;
}
public String getStreet(){
return street;
}
}
My Result:
"name" : "John",
"friends" : [ "Anne", "Bill" ],
"homeAddress" : {
"no" : "20",
"street" : "Main St."
},
"workAddress" : {
"no" : "11",
"street" : "Corner St."
}
}