-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot parse map if the map contains 0 #15
Comments
I guess that this isn't possible to fix, this happens due to type loss, so the parser needs to do heuristics to figure out if it is a map or a array. The problem happens when you try to use |
It looks like @jclawson is inactive, so I guess this bug won't ever be fixed. In the meantime, I made a custom deserializer with a hacky fix for this issue. class FixedMapDeserializer : JsonDeserializer<Map<*, *>>() {
override fun deserialize(p0: JsonParser, p1: DeserializationContext): Map<*, *> {
val any = p0.readValueAsTree<TreeNode>()
if (any.isArray) {
val values = mutableMapOf<Any?, Any?>()
repeat(any.size()) {
values[it] = any.get(it)
}
return values
} else {
val values = mutableMapOf<Any?, Any?>()
any.fieldNames().forEach {
values[it] = any.get(it)
}
return values
}
}
} Then, on your HOCON Mapper val module = SimpleModule()
module.setDeserializerModifier(object: BeanDeserializerModifier() {
override fun modifyMapDeserializer(config: DeserializationConfig, type: MapType, beanDesc: BeanDescription, deserializer: JsonDeserializer<*>): JsonDeserializer<*> {
return FixedMapDeserializer()
}
})
mapper.registerModule(module) The deserializer is a very simple & quick fix: If the JSON input is a array but Jackson expects a class, it converts the JSON to a Map (first array entry = I didn't test it on every possible combination, but on my quick tests: It works pretty well! |
Output:
{1=abc}
Output:
This happens even if the
Map
is another type (Example:Map<String, String>
)This does NOT happen when parsing with lightbend/config:
Output:
Config(SimpleConfigObject({"0":"abc"}))
The text was updated successfully, but these errors were encountered: