Created
February 21, 2018 14:15
-
-
Save shtratos/f0a81515d19b858dafb71e86b62cb474 to your computer and use it in GitHub Desktop.
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
import com.fasterxml.jackson.annotation.JsonAnySetter; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.KeyDeserializer; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import com.google.common.collect.Interner; | |
import com.google.common.collect.Interners; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class JacksonLongPool { | |
public static void main(String[] args) throws IOException { | |
final String map1 = "{\"1\": \"Hello\", \"10000000\": \"world!\"}"; | |
final String map2 = "{\"1\": \"You\", \"10000000\": \"rock!\"}"; | |
System.out.println("-- Standard deserialization --"); | |
standardDeserialization(map1, map2); | |
System.out.println("-------------------------------------"); | |
System.out.println("-- AnySetter deserialization --"); | |
anySetterDeserialization(map1, map2); | |
System.out.println("-------------------------------------"); | |
System.out.println("-- Custom KeyDeserializer deserialization --"); | |
customKeyDeserialization(map1, map2); | |
System.out.println("-------------------------------------"); | |
System.out.println("-- Annotation-based KeyDeserializer deserialization --"); | |
annotationBasedCustomKeyDeserialization(map1, map2); | |
System.out.println("-------------------------------------"); | |
} | |
private static void standardDeserialization(String map1, String map2) throws IOException { | |
final ObjectMapper mapper = new ObjectMapper(); | |
final TypeReference<Map<Long, String>> typeRef = new TypeReference<Map<Long, String>>() {}; | |
final Map<Long, String> deserializedMap1 = mapper.readValue(map1, typeRef); | |
final Map<Long, String> deserializedMap2 = mapper.readValue(map2, typeRef); | |
printMap(deserializedMap1); | |
printMap(deserializedMap2); | |
} | |
static class CustomLongPoolingMap { | |
private static final Interner<Long> LONG_POOL = Interners.newStrongInterner(); | |
private final Map<Long, String> map = new HashMap<>(); | |
@JsonAnySetter | |
public void addEntry(String key, String value) { | |
map.put(LONG_POOL.intern(Long.parseLong(key)), value); | |
} | |
public Map<Long, String> getMap() { | |
return map; | |
} | |
} | |
private static void anySetterDeserialization(String map1, String map2) throws IOException { | |
final ObjectMapper mapper = new ObjectMapper(); | |
final Map<Long, String> deserializedMap1 = mapper.readValue(map1, CustomLongPoolingMap.class).getMap(); | |
final Map<Long, String> deserializedMap2 = mapper.readValue(map2, CustomLongPoolingMap.class).getMap(); | |
printMap(deserializedMap1); | |
printMap(deserializedMap2); | |
} | |
public static class MyCustomKeyDeserializer extends KeyDeserializer { | |
private static final Interner<Long> LONG_POOL = Interners.newStrongInterner(); | |
@Override | |
public Long deserializeKey(String key, DeserializationContext ctxt) { | |
return LONG_POOL.intern(Long.parseLong(key)); | |
} | |
} | |
private static void customKeyDeserialization(String map1, String map2) throws IOException { | |
final SimpleModule module = new SimpleModule(); | |
module.addKeyDeserializer(Long.class, new MyCustomKeyDeserializer()); | |
final ObjectMapper mapper = new ObjectMapper().registerModule(module); | |
final TypeReference<Map<Long, String>> typeRef = new TypeReference<Map<Long, String>>() {}; | |
final Map<Long, String> deserializedMap1 = mapper.readValue(map1, typeRef); | |
final Map<Long, String> deserializedMap2 = mapper.readValue(map2, typeRef); | |
printMap(deserializedMap1); | |
printMap(deserializedMap2); | |
} | |
static class MapWrapper { | |
@JsonDeserialize(keyUsing = MyCustomKeyDeserializer.class) | |
private Map<Long, String> map1; | |
@JsonDeserialize(keyUsing = MyCustomKeyDeserializer.class) | |
private Map<Long, String> map2; | |
} | |
private static void annotationBasedCustomKeyDeserialization(String map1, String map2) throws IOException { | |
final ObjectMapper mapper = new ObjectMapper(); | |
final String json = "{\"map1\": " + map1 + ", \"map2\": " + map2 + "}"; | |
final MapWrapper wrapper = mapper.readValue(json, MapWrapper.class); | |
final Map<Long, String> deserializedMap1 = wrapper.map1; | |
final Map<Long, String> deserializedMap2 = wrapper.map2; | |
printMap(deserializedMap1); | |
printMap(deserializedMap2); | |
} | |
private static void printMap(Map<Long, String> longStringMap) { | |
longStringMap.forEach((Long k, String v) -> { | |
System.out.printf("key object id %d \t %s -> %s %n", System.identityHashCode(k), k, v); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment