问题
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