Jackson JSON Deserialization: array elements in each line

后端 未结 6 1935
野趣味
野趣味 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: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);
    }
    

提交回复
热议问题