After a CustomPrettyPrinter in jackson, Indentation settings are not working

陌路散爱 提交于 2021-01-29 13:50:43

问题


class CustomPrettyPrinter: DefaultPrettyPrinter() {
    @Throws(IOException::class)
    override fun writeObjectFieldValueSeparator(jg: JsonGenerator) {
        jg.writeRaw(": ")
    }

    override fun createInstance(): DefaultPrettyPrinter? {
        return CustomPrettyPrinter()
    }
}

fun generateJson() {
    val file = File("file.json")
    var jsonData = JSONParser().parse(FileReader(file)) as JSONObject
    val mapper = ObjectMapper()
    val indenter : DefaultPrettyPrinter.Indenter = DefaultIndenter("     ", DefaultIndenter.SYS_LF)
    val printer = CustomPrettyPrinter()
    printer.apply {
        indentArraysWith(indenter)
        indentObjectsWith(indenter)
    }

    mapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    val writer = mapper.writer(printer)
    val out = writer.writeValueAsString(jsonData);

    println(out)
    File("file.json").writeText(out)
}

In the above Kotlin code, if the Default pretty printer is used, the indentation works, but for the custom pretty printer, the indentation of 4 spaces is not working, only the default 2 is written in the file. Why?

来源:https://stackoverflow.com/questions/61952317/after-a-customprettyprinter-in-jackson-indentation-settings-are-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!