Last active
December 10, 2015 20:18
-
-
Save electrum/4487351 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
package javatest; | |
import com.google.common.collect.ImmutableMap; | |
import org.codehaus.jackson.JsonGenerator; | |
import org.codehaus.jackson.annotate.JsonCreator; | |
import org.codehaus.jackson.annotate.JsonProperty; | |
import org.codehaus.jackson.map.DeserializationContext; | |
import org.codehaus.jackson.map.JsonSerializer; | |
import org.codehaus.jackson.map.KeyDeserializer; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.map.SerializerProvider; | |
import org.codehaus.jackson.map.annotate.JsonDeserialize; | |
import org.codehaus.jackson.map.annotate.JsonSerialize; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.Map; | |
public final class Jackson1TestMapKeys | |
{ | |
public static class CustomKey | |
{ | |
private final int id; | |
public CustomKey(int id) {this.id = id;} | |
public int getId() | |
{ | |
return id; | |
} | |
@Override | |
public boolean equals(Object o) | |
{ | |
if (o instanceof CustomKey) { | |
return id == ((CustomKey) o).id; | |
} | |
return false; | |
} | |
@Override | |
public int hashCode() | |
{ | |
return id; | |
} | |
} | |
public static class Model | |
{ | |
private final Map<CustomKey, String> map; | |
@JsonCreator | |
public Model(@JsonProperty("map") @JsonDeserialize(keyUsing = CustomKeyDeserializer.class) Map<CustomKey, String> map) | |
{ | |
this.map = ImmutableMap.copyOf(map); | |
} | |
@JsonProperty | |
@JsonSerialize(keyUsing = CustomKeySerializer.class) | |
public Map<CustomKey, String> getMap() | |
{ | |
return map; | |
} | |
} | |
public static void main(String[] args) | |
throws Exception | |
{ | |
Model original = new Model(Collections.singletonMap(new CustomKey(123), "test")); | |
ObjectMapper mapper = new ObjectMapper(); | |
String json = mapper.writeValueAsString(original); | |
Model deserialized = mapper.readValue(json, Model.class); | |
System.out.println(json); | |
System.out.println(original.getMap().equals(deserialized.getMap())); | |
} | |
private static class CustomKeySerializer | |
extends JsonSerializer<CustomKey> | |
{ | |
@Override | |
public void serialize(CustomKey value, JsonGenerator jgen, SerializerProvider provider) | |
throws IOException | |
{ | |
jgen.writeFieldName(String.valueOf(value.getId())); | |
} | |
} | |
private static class CustomKeyDeserializer | |
extends KeyDeserializer | |
{ | |
@Override | |
public CustomKey deserializeKey(String key, DeserializationContext ctxt) | |
throws IOException | |
{ | |
return new CustomKey(Integer.valueOf(key)); | |
} | |
} | |
} |
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
package javatest; | |
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.JsonSerializer; | |
import com.fasterxml.jackson.databind.KeyDeserializer; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializerProvider; | |
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
import com.google.common.collect.ImmutableMap; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.Map; | |
public final class Jackson2TestMapKeys | |
{ | |
public static final class CustomKey | |
{ | |
private final int id; | |
public CustomKey(int id) {this.id = id;} | |
public int getId() | |
{ | |
return id; | |
} | |
@Override | |
public boolean equals(Object o) | |
{ | |
if (o instanceof CustomKey) { | |
return id == ((CustomKey) o).id; | |
} | |
return false; | |
} | |
@Override | |
public int hashCode() | |
{ | |
return id; | |
} | |
} | |
public static class Model | |
{ | |
private final Map<CustomKey, String> map; | |
@JsonCreator | |
public Model(@JsonProperty("map") @JsonDeserialize(keyUsing = CustomKeyDeserializer.class) Map<CustomKey, String> map) | |
{ | |
this.map = ImmutableMap.copyOf(map); | |
} | |
@JsonProperty | |
@JsonSerialize(keyUsing = CustomKeySerializer.class) | |
public Map<CustomKey, String> getMap() | |
{ | |
return map; | |
} | |
} | |
public static void main(String[] args) | |
throws Exception | |
{ | |
Model original = new Model(Collections.singletonMap(new CustomKey(123), "test")); | |
ObjectMapper mapper = new ObjectMapper(); | |
String json = mapper.writeValueAsString(original); | |
Model deserialized = mapper.readValue(json, Model.class); | |
System.out.println(json); | |
System.out.println(original.getMap().equals(deserialized.getMap())); | |
} | |
private static class CustomKeySerializer | |
extends JsonSerializer<CustomKey> | |
{ | |
@Override | |
public void serialize(CustomKey value, JsonGenerator jgen, SerializerProvider provider) | |
throws IOException | |
{ | |
jgen.writeFieldName(String.valueOf(value.getId())); | |
} | |
} | |
private static class CustomKeyDeserializer | |
extends KeyDeserializer | |
{ | |
@Override | |
public CustomKey deserializeKey(String key, DeserializationContext ctxt) | |
throws IOException | |
{ | |
return new CustomKey(Integer.valueOf(key)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment