Add custom JSON pretty printer

This produces identical output to the Firefox pretty printer.

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent bdab767ca7
commit 3ff6cd2d02
  1. 24
      json/src/main/kotlin/org/openrs2/json/JsonPrettyPrinter.kt
  2. 3
      json/src/main/kotlin/org/openrs2/json/ObjectMapperProvider.kt

@ -0,0 +1,24 @@
package org.openrs2.json
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
public class JsonPrettyPrinter : DefaultPrettyPrinter() {
init {
indentArraysWith(UNIX_INDENT)
indentObjectsWith(UNIX_INDENT)
}
override fun createInstance(): DefaultPrettyPrinter {
return JsonPrettyPrinter()
}
override fun writeObjectFieldValueSeparator(g: JsonGenerator) {
g.writeRaw(": ")
}
private companion object {
private val UNIX_INDENT = DefaultIndenter(" ", "\n")
}
}

@ -3,6 +3,7 @@ package org.openrs2.json
import com.fasterxml.jackson.databind.Module
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import javax.inject.Inject
import javax.inject.Provider
@ -14,6 +15,8 @@ public class ObjectMapperProvider @Inject constructor(
return ObjectMapper()
.registerKotlinModule()
.registerModules(modules)
.enable(SerializationFeature.INDENT_OUTPUT)
.setDefaultPrettyPrinter(JsonPrettyPrinter())
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
}
}

Loading…
Cancel
Save