-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Custom key deserialiser registered for Object.class
in nested Map object is ignored when Map key type not defined
#4680
Comments
Have you tried |
@JooHyukKim in such case for the root level object the custom key deserialiser is going to work, but for the nested objects it's still going to be ignored. |
I think the problem is that unless you can provide tight type definition for nested Maps, key deserializer is looked up for type But I think you can register same If registering of key deserializer for |
String.class
is ignored when Map key type not defined
I see, so the input could be nested. Then what @cowtowncoder said above would suffice. |
@cowtowncoder I've tried FYI: if you look at the implementation of |
@devdanylo is right. on depth 2 it no longer works. @Test
void customKeyDeserializerShouldBeUsedWhenTypeNotDefined() throws Exception {
// GIVEN
var json = """
{
"name*": "Erik",
"address*": {
"city*": {
"id*": 1,
"name*": "Berlin"
},
"street*": "Elvirastr"
}
}
""";
var keySanitizationModule = new SimpleModule("key-sanitization");
keySanitizationModule.addKeyDeserializer(String.class, new KeyDeserializer() {
@Override
public String deserializeKey(String key, DeserializationContext ctxt) {
return key.replace("*", "_");
}
});
keySanitizationModule.addKeyDeserializer(Object.class, new KeyDeserializer() {
@Override
public Object deserializeKey(String key, DeserializationContext ctxt) {
throw new RuntimeException("Should be called, but never called");
}
});
var mapper = JsonMapper.builder().addModule(keySanitizationModule).build();
// WHEN
var result = mapper.readValue(json, new TypeReference<Map<String, Object>>() { });
// THEN
// depth 1 works as expected
Assertions.assertEquals("Erik", result.get("name_"));
// depth 2 does NOT work as expected
var addressMap = (Map<String, Object>) result.get("address_");
// null?? Fails here
Assertions.assertEquals("Elvirastr", addressMap.get("street_"));
var cityMap = (Map<String, Object>) addressMap.get("city_");
Assertions.assertEquals(1, cityMap.get("id_"));
Assertions.assertEquals("Berlin", cityMap.get("name_"));
} |
Ok. So it sounds like we have a bug here. I suspect it might be because nested |
hmmmm, I tried to come up with a fix, but failing. Any pointers u might want to share here? |
Ah . |
I just realized this issue may no longer be about |
Should we change issue title from
to something like below?
|
String.class
is ignored when Map key type not definedObject.class
in nested Map object is ignored when Map key type not defined
@JooHyukKim Thank you -- changed. |
Search before asking
Describe the bug
I'm working on a piece of software which accepts arbitrary JSON, flattens it, and then writes it to CSV. The main issue here is that we don't know the schema of the JSON—it can be anything. This, in turn, results in untyped deserialization.
Before starting processing of the parsed object, the field names must be sanitised (yes, it's crucial to sanitise the field names during the parsing phase). Unfortunately
UntypedObjectDeserializerNR
ignores the custom key deserialiser I'm trying to provide:I have 2 questions in regards of this behaviour:
Version Information
2.14.3
Reproduction
No response
Expected behavior
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: