serialize jsonvalue annotation java serialization jackson deserialization

java - jsonvalue - objectmapper jackson



Serializar/Deserializar mapa personalizado<Key, Object> en Jackson (0)

Tengo un mapa bastante simple que quiero serializar y deserializar en Jackson, pero no puedo hacerlo funcionar.

He probado lo siguiente:

@JsonSerialize(keyUsing=TurnKeySerializer.class) @JsonDeserialize(keyUsing = TurnKeyDeserializer.class) Map<TurnKey, PlayerTurn> publicTurns = new TreeMap<>(); @JsonIgnoreProperties(ignoreUnknown = true) @Data //Creates Getter/Setter etc public class TurnKey implements Comparable<TurnKey> { private final int turnNumber; private final String username; public TurnKey(int turnNumber, String username) { this.turnNumber = turnNumber; this.username = username; } @Override public int compareTo(TurnKey o) { int v = Integer.valueOf(turnNumber).compareTo(o.getTurnNumber()); if (v != 0) { return v; } return username.compareTo(o.getUsername()); } @Override public String toString() { return "{" + "turnNumber:" + turnNumber + ", username:''" + username + "''" + "}"; } public class TurnKeySerializer extends JsonSerializer<TurnKey> { @Override public void serialize(TurnKey value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { if (null == value) { throw new IOException("Could not serialize object to json, input object to serialize is null"); } StringWriter writer = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(writer, value); gen.writeFieldName(writer.toString()); } } public class TurnKeyDeserializer extends KeyDeserializer { private static final ObjectMapper mapper = new ObjectMapper(); @Override public TurnKey deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException { return mapper.readValue(key, TurnKey.class); } }

Pero recibo una excepción

com.fasterxml.jackson.databind.JsonMappingException: no se puede deserializar la instancia de java.util.LinkedHashMap con el token START_ARRAY