Ability to ignore keys when deserializing maps
See original GitHub issueI’m using JSOG as my ID generator, and I have some classes that I’d like to serialize/deserialize that contain Maps of enums to other classes.
e.g.
@JsonIdentityInfo(generator = JSOGGenerator.class)
public class Game extends Event {
public enum Side {
home, visitor
}
private Map<Side, GameSide> sides;
public Map<Side, GameSide> getSides() {
return sides;
}
public void setSides(Map<Side, GameSide> sides) {
this.sides = sides;
}
}
public class GameSide {
// ...
}
Serialization works-I get an object that has the appropriate Side keys and GameSide values
However, when the client is serializing back to JSOG to send to the server, it treats the map like any other object and adds an @id
key with a string value to the map, e.g. "@id":"239"
.
Jackson chokes on this because 1. the key is not a valid enum value and 2. the value cannot be deserialized to a GameSide.
I tried registering a custom deserializer for the first issue
public static class Deserializer extends KeyDeserializer {
@Override
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException {
try {
return Side.valueOf(key);
} catch (Exception e) {
return null;
}
}
}
However returning null
does not exclude the key from the map and the GameSide
is still deserialized, which causes an error because the value is the @id
of the map. I need a way to say skip this key on the map in my KeyDeserializer or a property on the @JsonDeserialize
annotation (e.g. ignoreUnknownKeys
similar to ignoreUnknownProperties
for maps)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:12 (4 by maintainers)
Top GitHub Comments
@moodysalem Yeah, maybe filing a simple “support
@JsonIdentityInfo
for Maps” makes sense. May be tricky to support, but it’s good to list things that would be good have.@cowtowncoder I just reread through the issue, and the last ask was that
JsonIdentityInfo
annotations apply to the javaMap
classIn hindsight since I’m looking at this 1 year later, I would probably avoid serializing data structures with circular references altogether, but it did have some neat benefits (size of payload was significantly smaller, could transmit entire object graphs over the wire without flattening)
However, since Jackson supports serialization of object graphs, I think it makes sense to support
Map
too withJsonIdentityInfo
and add id properties when serializing maps and first checking for id properties when deserializing maps. If that makes sense as an action item for the ticket I can write a separate issue