question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to ignore keys when deserializing maps

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Apr 24, 2018

@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.

0reactions
moodysalemcommented, Apr 24, 2018

@cowtowncoder I just reread through the issue, and the last ask was that JsonIdentityInfo annotations apply to the java Map class

In 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 with JsonIdentityInfo 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing non-string map keys with Jackson - Stack Overflow
After a day of searching, I came across a simpler way of doing it based on this question. The solution was to add...
Read more >
Jackson - Working with Maps and nulls - Baeldung
How to serialize Maps with a null key or null values using Jackson. ... in Map object serialized through this mapper is going...
Read more >
Definitive Guide to Jackson ObjectMapper - Serialize and ...
In this detailed guide - learn everything you need to know about ObjectMapper. Convert JSON to and from Java POJOs, implement custom ...
Read more >
How do you deserialize json properties that are reserved ...
To add to the 2 ways in the accepted answer, here is a third way making use of Apex maps that allow any...
Read more >
The Serializer Component (Symfony Docs)
Deserializing in an Existing Object. Context; Attributes Groups; Selecting Specific Attributes; Ignoring Attributes. Option 1: Using @Ignore Annotation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found