Jackson Key Serialization with Circular Links

I am trying to serialize a HashMap from Object to Strings, but the specific object has a reference to the current class leading to infinite recursion, which does not seem to be resolved using the usual JsonIdentifyInfo annotation. Here is an example:

public class CircularKey { public void start() throws IOException { ObjectMapper mapper = new ObjectMapper(); Cat cat = new Cat(); // Encode String json = mapper.writeValueAsString(cat); System.out.println(json); // Decode Cat cat2 = mapper.readValue(json, Cat.class); System.out.println(mapper.writeValueAsString(cat2)); } } @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") class Mouse { int id; @JsonProperty Cat cat; } @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") class Cat { int id; @JsonSerialize(keyUsing = MouseMapKeySerializer.class) @JsonDeserialize(keyUsing = MouseMapKeyDeserializer.class) @JsonProperty HashMap<Mouse, String> status = new HashMap<Mouse, String>(); public Cat() { Mouse m = new Mouse(); m.cat = this; status.put(m, "mike"); } } 

Here's the serializer / deserializer for the key:

 class MouseMapKeySerializer extends JsonSerializer<Mouse> { static ObjectMapper mapper = new ObjectMapper(); @Override public void serialize(Mouse value, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException { String json = mapper.writeValueAsString(value); generator.writeFieldName(json); } } class MouseMapKeyDeserializer extends KeyDeserializer { static ObjectMapper mapper = new ObjectMapper(); @Override public Mouse deserializeKey(String c, DeserializationContext ctx) throws IOException, JsonProcessingException { return mapper.readValue(c, Mouse.class); } } 

If I switch the map to HashMap (String, Object), it works, but I can’t change the original display. Any ideas?

+4
source share
1 answer

It looks like you found your answer at http://jackson-users.ning.com/forum/topics/serializing-hashmap-with-object-key-and-recursion . This is not possible because:

Complex keys are complex and this is not a random case that I have ever considered. Again, there is nothing that specifically prevents the use of standard components; The main problem was only in restrictions than JSON (should have String-value, JsonParser / JsonGenerator expands keys as different tokens). There is no explicit support for either polymorphic types or object identifiers for Object keys. Standard serializers / deserializers are generally relatively simple types that can be easily and reliably converted to / from strings; numbers, dates, UUID.

So: unlike value handlers, where a modular design (with a separation of TypeSerializer / JsonSerializer) makes sense, I think you need to have custom (de) serializers handle all aspects. You should be able to use code from existing value serializers (de), type serializers (de), but not the classes themselves.

Your use case sounds interesting, but for better or worse it pretty much pushes the envelope. :-)

0
source

Source: https://habr.com/ru/post/1499247/


All Articles