jackson-module-scala icon indicating copy to clipboard operation
jackson-module-scala copied to clipboard

Map serialization ignores JsonSerialize of keys

Open dcsobral opened this issue 8 years ago • 2 comments

Given a Map[K,V], type K's @JsonSerialize annotation is ignored, though type V's isn't. The problem doesn't happen with Seq[V].

Note that by K and V I mean any type at all -- invert the map to Map[V,K], and now V's serializer is ignored but K isn't.

Maybe it has something to do with keys in JSON being strings? This is happening to me even though the specialized serializer I'm using does produce strings as serialization, though.

dcsobral avatar Feb 10 '17 08:02 dcsobral

@cowtowncoder I think this is supported if done the right way - we have a jackson-module-scala test case that tests this class

case class KeySerializerMap(
  @(JsonSerialize @getter)(keyUsing = classOf[TupleKeySerializer])
  keySerializerMap: Map[(String,String),Int])

So the 'keyUsing' param always you to specify the serializer for the keys of the map.

pjfanning avatar Sep 03 '21 18:09 pjfanning

Correct @pjfanning : serializers and deserializers used with Map keys are separate from "regular" value (de)serializers and are both:

  1. Registered separately (there's a smaller set of default types supported, too)
  2. Use different property in case of per-class or per-property annotations, as in your example

Difference is historical, but since JSON Object keys MUST be Strings, KeyDeserializer is separate implementation type (for serialization JsonSerializer is used but other constraints apply); and from this their handling is quite distinct.

cowtowncoder avatar Sep 04 '21 21:09 cowtowncoder