在Jackson中将JsonNode序列化为非常特定的JSON格式

在Jackson中将JsonNode序列化为非常特定的JSON格式,第1张

在Jackson中将JsonNode序列化为非常特定的JSON格式

您可以自定义Jackson如何缩进输出。根据您使用的Jackson版本,有多种实现方法。

杰克逊2.5及更高版本

请执行下列 *** 作:

DefaultPrettyPrinter printer = new DefaultPrettyPrinter();Indenter indenter = new DefaultIndenter();printer.indentObjectsWith(indenter); // Indent JSON objectsprinter.indentArraysWith(indenter);  // Indent JSON arraysObjectMapper mapper = new ObjectMapper();mapper.writer(printer).writevalue(new FileOutputStream(outputFile), node);

默认情况下,将使用2个空格。对于不同数量的空格,请使用

DefaultIndenter
接收字符串以缩进级别和行分隔符的构造函数:

Indenter indenter = new DefaultIndenter("      ", DefaultIndenter.SYS_LF);
杰克逊2.4及更低版本

请执行下列 *** 作:

DefaultPrettyPrinter printer = new DefaultPrettyPrinter();Indenter indenter = new Lf2SpacesIndenter();printer.indentObjectsWith(indenter); // Indent JSON objectsprinter.indentArraysWith(indenter);  // Indent JSON arraysObjectMapper mapper = new ObjectMapper();mapper.writer(printer).writevalue(new FileOutputStream(outputFile), node);

Lf2SpacesIndenter
被限制在2个空格,你不能改变它。

如果需要不同数量的空格,则需要编写自定义实现。这是使用与

DefaultIndenter
Jackson
2.5中引入的代码相同的代码的代码:

public class CustomSpaceIndenter extends DefaultPrettyPrinter.NopIndenter {    public final static String SYS_LF;    static {        String lf;        try { lf = System.getProperty("line.separator");        } catch (Throwable t) { lf = "n"; // fallback when security manager denies access        }        SYS_LF = lf;    }    public static final CustomSpaceIndenter SYSTEM_LINEFEED_INSTANCE =  new CustomSpaceIndenter("  ", SYS_LF);        private final static int INDENT_LEVELS = 16;    private final char[] indents;    private final int charsPerLevel;    private final String eol;        public CustomSpaceIndenter() {        this("  ", SYS_LF);    }        public CustomSpaceIndenter(String indent, String eol)  {        charsPerLevel = indent.length();        indents = new char[indent.length() * INDENT_LEVELS];        int offset = 0;        for (int i=0; i<INDENT_LEVELS; i++) { indent.getChars(0, indent.length(), indents, offset); offset += indent.length();        }        this.eol = eol;    }    public CustomSpaceIndenter withLinefeed(String lf) {        if (lf.equals(eol)) { return this;        }        return new CustomSpaceIndenter(getIndent(), lf);    }    public CustomSpaceIndenter withIndent(String indent) {        if (indent.equals(getIndent())) { return this;        }        return new CustomSpaceIndenter(indent, eol);    }    public String getEol() {        return eol;    }    public String getIndent() {        return new String(indents, 0, charsPerLevel);    }    @Override    public boolean isInline() {         return false;    }    @Override    public void writeIndentation(JsonGenerator jg, int level) throws IOException {        jg.writeRaw(eol);        if (level > 0) { // should we err on negative values (as there's some flaw?) level *= charsPerLevel; while (level > indents.length) { // unlike to happen but just in case     jg.writeRaw(indents, 0, indents.length);      level -= indents.length; } jg.writeRaw(indents, 0, level);        }    }}

可以如下使用:

Indenter indenter = new CustomSpaceIndenter("      ", CustomSpaceIndenter.SYS_LF);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4908795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存