2

I wrote this code:

ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try
{
    writer.writeValue(new File(jsonFile.getAbsolutePath()), jsonData);
}
catch (IOException e)
{
}

I got result like:

{
  "type" : "aaa",
  "key" : {
    "key1" : "bbb",
    "key 2" : [ {
      "value" : "xx"
    }, {
      "value" : "sss"
    }, {
      "value" : "zzz"
    }]
  }
}

I want the content will be:

{
  "type" : "aaa",
  "key" : 
   {
    "key1" : "bbb",
    "key 2" :
    [
      {
        "value" : "xx"
      }, 
      {
        "value" : "sss"
      }, 
      {
        "value" : "zzz"
      }
    ]
  }
}

I want it to be with drop line in any bracket, and number of tabs compatible. How can I add line drop and appropriate tabs between Json fields?

  • Similar questions: [Jackson JSON Not Formatting Correctly](https://stackoverflow.com/questions/17411586/jackson-json-not-formatting-correctly), [Custom pretty printer using Jackson library](https://stackoverflow.com/questions/18098513/custom-pretty-printer-using-jackson-library), [Jackson JSON Deserialization: array elements in each line](https://stackoverflow.com/questions/14938667/jackson-json-deserialization-array-elements-in-each-line/40044685) – Michał Ziober Oct 21 '20 at 17:08

2 Answers2

0

I don't think you can achieve this simply by configuring the pretty printer, you will have to implement your own class for this. See https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/PrettyPrinter.html

Gereon
  • 17,258
  • 4
  • 42
  • 73
0

To achieve format like this you need to implement your own PrettyPrinter:

class ObjectArrayInNewLinePrettyPrinter extends DefaultPrettyPrinter {

    public ObjectArrayInNewLinePrettyPrinter() {
        super();
    }

    public ObjectArrayInNewLinePrettyPrinter(DefaultPrettyPrinter base) {
        super(base);
    }

    @Override
    public void writeStartObject(JsonGenerator g) throws IOException {
        _objectIndenter.writeIndentation(g, _nesting);
        super.writeStartObject(g);
    }

    @Override
    public void writeStartArray(JsonGenerator g) throws IOException {
        _arrayIndenter.writeIndentation(g, _nesting);
        super.writeStartArray(g);
    }

    @Override
    public DefaultPrettyPrinter createInstance() {
        return new ObjectArrayInNewLinePrettyPrinter(this);
    }
}

And you can use it as below:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.File;
import java.io.IOException;

public class JsonPrettyPrinterApp {
    public static void main(String[] args) throws IOException {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        DefaultPrettyPrinter printer = new ObjectArrayInNewLinePrettyPrinter();
        printer.indentArraysWith(new DefaultIndenter());

        JsonMapper mapper = JsonMapper.builder()
                .enable(SerializationFeature.INDENT_OUTPUT)
                .defaultPrettyPrinter(printer)
                .build();

        JsonNode root = mapper.readTree(jsonFile);

        mapper.writeValue(System.out, root);
    }
}

above code prints:

{
  "type" : "aaa",
  "key" : 
  {
    "key1" : "bbb",
    "key 2" : 
    [
      
      {
        "value" : "xx"
      },
      
      {
        "value" : "sss"
      },
      
      {
        "value" : "zzz"
      }
    ]
  }
}

which prints something similar to what you want.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146