Jackson JSON Deserialization: array elements in each line

后端 未结 6 1921
野趣味
野趣味 2020-12-15 06:28

I am using Jackson and would like to pretty-print JSON such that each element in arrays goes to each line, like:

{
  \"foo\" : \"bar\",
  \"blah\" : [
    1,
             


        
相关标签:
6条回答
  • 2020-12-15 06:50

    Mapper can be configured (jackson-2.6) with:

    ObjectMapper mapper = ...
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
    mapper.setDefaultPrettyPrinter(prettyPrinter);
    
    //use mapper 
    
    0 讨论(0)
  • 2020-12-15 06:59

    If you don't want to extend DefaultPrettyPrinter you can also just set the indentArraysWith property externally:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
    
    String json = objectMapper.writer(prettyPrinter).writeValueAsString(object);
    
    0 讨论(0)
  • 2020-12-15 06:59

    Thanks to the helpful hints, I was able to configure my ObjectMapper with desired indentation as follows:

    private static class PrettyPrinter extends DefaultPrettyPrinter {
        public static final PrettyPrinter instance = new PrettyPrinter();
    
        public PrettyPrinter() {
            _arrayIndenter = Lf2SpacesIndenter.instance;
        }
    }
    
    private static class Factory extends JsonFactory {
        @Override
        protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException {
            return super._createGenerator(out, ctxt).setPrettyPrinter(PrettyPrinter.instance);
        }
    }
    
    {
        ObjectMapper mapper = new ObjectMapper(new Factory());
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    }
    
    0 讨论(0)
  • 2020-12-15 07:00

    try out JSON Generator...

    API Reference
    good example

    0 讨论(0)
  • 2020-12-15 07:01

    The answer thankfully provided by OP shows a way for obtain a single-array-element-per-line formatted JSON String from writeValueAsString. Based on it here a solution to write the same formatted JSON directly to a file with writeValue with less code:

    private static class PrettyPrinter extends DefaultPrettyPrinter {
        public static final PrettyPrinter instance = new PrettyPrinter();
    
        public PrettyPrinter() {
            _arrayIndenter = Lf2SpacesIndenter.instance;
        }
    }
    
    {
        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = mapper.writer(PrettyPrinter.instance);
        writer.writeValue(destFile, objectToSerialize);
    }
    
    0 讨论(0)
  • 2020-12-15 07:12

    You could extend the DefaultPrettyPrinter and override the methods beforeArrayValues(…) and writeArrayValueSeparator(…) to archieve the desired behaviour. Afterwards you have to add your new Implementation to your JsonGenerator via setPrettyPrinter(…).

    0 讨论(0)
提交回复
热议问题