You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wrote a custom generator to truncate text fields past a certain length but it doesn't seem to work on data class fields.
I'm using Kotlin with Klogger and have a function like this that allows me to pass a lambda that builds a map and allows me to set key, value pairs as Markers:
fun KLogger.debug(msg: () -> Any?, extra: (MutableMap<String, Any?>) -> Unit) {
if (isDebugEnabled) {
val entries = mutableMapOf<String, Any?>().apply { extra(this) }
debug(appendEntries(entries), msg)
}
}
To test this, I wrote a custom serializer for a NestedObject data class, a custom JsonFactory to add that serializer, and a custom JsonGenerator to do the actual truncation:
class CustomJsonGeneratorDecorator : JsonGeneratorDecorator {
override fun decorate(generator: JsonGenerator) = CappedFieldJsonGenerator(generator)
}
class CappedFieldJsonGenerator(delegate: JsonGenerator) : JsonGeneratorDelegate(delegate) {
companion object {
private const val MAX_FIELD_LENGTH = 3
}
override fun writeRawUTF8String(text: ByteArray?, offset: Int, length: Int) {
super.writeRawUTF8String(text, offset, min(MAX_FIELD_LENGTH, length))
}
override fun writeUTF8String(text: ByteArray, offset: Int, length: Int) {
super.writeUTF8String(text, offset, min(MAX_FIELD_LENGTH, length))
}
override fun writeString(reader: Reader, len: Int) {
super.writeString(reader, min(MAX_FIELD_LENGTH, len))
}
override fun writeString(text: CharArray, offset: Int, len: Int) {
super.writeString(text, offset, min(MAX_FIELD_LENGTH, len))
}
override fun writeString(text: SerializableString) {
this.writeString(text.value)
}
override fun writeString(text: String) {
super.writeString(text.substring(0, min(MAX_FIELD_LENGTH, text.length)))
}
}
data class NestedObject(val name: String = "nested")
data class TestObject(
val name: String = "hello",
val nested: NestedObject = NestedObject(),
)
class TestObjectSerializer @JvmOverloads constructor(t: Class<NestedObject>? = null) :
StdSerializer<NestedObject>(t) {
override fun serialize(value: NestedObject, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeStringField("name", "serialized")
gen.writeStringField("class", gen::class.java.name)
gen.writeEndObject()
}
}
class CustomJsonFactoryDecorator : JsonFactoryDecorator {
override fun decorate(factory: JsonFactory): JsonFactory {
val mapper = factory.codec as ObjectMapper
val module = SimpleModule()
module.addSerializer(NestedObject::class.java, TestObjectSerializer())
mapper.registerModule(module)
return factory
}
}
As you can see, the truncate logic works on all fields (including plain text markers) except for those on the TestObject and those on the NestedObject with a custom serializer. It seems the custom serializer is getting UTF8JsonGenerator instead of my CappedFieldJsonGenerator.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I wrote a custom generator to truncate text fields past a certain length but it doesn't seem to work on data class fields.
I'm using Kotlin with Klogger and have a function like this that allows me to pass a lambda that builds a map and allows me to set key, value pairs as Markers:
To test this, I wrote a custom serializer for a
NestedObject
data class, a customJsonFactory
to add that serializer, and a customJsonGenerator
to do the actual truncation:When I call the following log:
I get the following result:
As you can see, the truncate logic works on all fields (including plain text markers) except for those on the
TestObject
and those on theNestedObject
with a custom serializer. It seems the custom serializer is gettingUTF8JsonGenerator
instead of myCappedFieldJsonGenerator
.Any ideas on how to fix this?
Beta Was this translation helpful? Give feedback.
All reactions