Created
March 11, 2013 06:33
-
-
Save kashyapp/5132236 to your computer and use it in GitHub Desktop.
Jackson Custom Deserializer - pick up only keys of map and return as list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @JsonDeserialize(contentUsing = Foo.class) | |
| private static class Permissions extends HashMap<String, List<String>> { | |
| } | |
| private static class Foo extends StdDeserializer<Collection<String>> { | |
| protected Foo() { | |
| super(Collection.class); | |
| } | |
| @Override | |
| public Collection<String> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { | |
| switch (jp.getCurrentToken()) { | |
| case START_OBJECT: { | |
| JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(ctxt.constructType(Map.class)); | |
| Map<String, Object> map = (Map<String, Object>) deserializer.deserialize(jp, ctxt); | |
| return map.keySet(); | |
| } | |
| case START_ARRAY: { | |
| JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(ctxt.constructType(List.class)); | |
| List<String> list = (List<String>) deserializer.deserialize(jp, ctxt); | |
| return list; | |
| } | |
| default: | |
| throw ctxt.mappingException("Expected to see an object or array, found " + jp.getCurrentToken()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment