Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map serialization ignores JsonSerialize of keys #316

Open
dcsobral opened this issue Feb 10, 2017 · 2 comments
Open

Map serialization ignores JsonSerialize of keys #316

dcsobral opened this issue Feb 10, 2017 · 2 comments

Comments

@dcsobral
Copy link

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.

@pjfanning
Copy link
Member

@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.

@cowtowncoder
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants