withDefaultPrettyPrinter() doesn't make the output be formatted

2.2k views Asked by At

I want to save some json to file

This is my serializtion code:

private String serializeToJson(T item) {
    String json;
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    try {
        json = ow.writeValueAsString(item);
    } catch (IOException e) {
        e.printStackTrace();
        json = "";
    }
    return json;
}

and yet my json is save to file not formatted:

e.g. part of it is:

{"path":{"segmentId":69798169,"nodeId":18477384,"x":-71.12074,"y":42.33235},"street":1,"altStreets":null,"distance":59,"length":178,"crossTime":49,"crossTimeWithoutRealTime":49,"tiles":[0,1],"clientIds":[166,177],"instruction":

1

There are 1 answers

0
Sheetal Mohan Sharma On

Tried Jackson pretty print mapper?assumed object has implemented toString method

   ObjectMapper mapper      = new ObjectMapper();


       try {
       System.out.println("Default output:"+mapper.writeValueAsString(object));
       System.out.println("Pretty printing:\n"+mapper.defaultPrettyPrintingWriter().writeValueAsString(object));