Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Multimap serializer doesn't work with non-String keys, even if it seems like they should. #13

Closed
stevenschlansker opened this issue Jan 2, 2013 · 10 comments

Comments

@stevenschlansker
Copy link
Contributor

Serializing a Multimap<K, ?> fails if K is any non-String type, even if intuitively it should work (e.g. K is a string-like type such as Enum, or K has an @JsonValue annotation).

I couldn't figure out how this is supposed to work, so I have included failing test cases for both the 1.9 branch and 2.x branch and hope that someone else who knows more about how Jackson's typing works can help out.

@pgelinas
Copy link
Member

pgelinas commented Jan 2, 2013

I'll take a look at this, but from memory it should be the same behaviour then Maps: you need a custom key deserializer for this to work. The @JsonValue annotation is ignored in this case and there might be a special case required for Enum (or a built-in key deserializer, not sure).

@stevenschlansker
Copy link
Contributor Author

Is there any particular reason for that? If you have a to/from String via @JsonValue and @JsonCreator it seems reasonable to expect Map-like types to use that. Or should there be a @JsonKeyValue annotation? Registering custom serializers is a bit of a pain when it feels like a simple annotation should do the job.

@pgelinas
Copy link
Member

pgelinas commented Jan 2, 2013

Indeed it would be, if I was right... I did a quick look and my memory isn't that good. These kind of cases should be handled. I'll take a good look at this instead of spouting nonsense.

@pgelinas
Copy link
Member

pgelinas commented Jan 2, 2013

Alright, I've looked at your test cases and here's what I got:

  • The @JsonValue case doesn't work for a simple Map type, so this isn't a guava related issue. This might be by design or a missing feature, I have no idea. However, you can make the case work by having a toString() method return the same thing the @JsonValue would (although this might not always be possible for some complex cases.)
  • I've fixed the Enum case, which was caused by defaulting to a String key type when unable to resolve generic type.

pgelinas pushed a commit that referenced this issue Jan 2, 2013
@pgelinas pgelinas closed this as completed Jan 2, 2013
@cowtowncoder
Copy link
Member

I think this is the related issue for core databind, wrt default key serializer supporting @JsonValue

FasterXML/jackson-databind#47

and as stated, would be a Very Good thing to support.

@stevenschlansker
Copy link
Contributor Author

Now that the upstream issue is resolved, should we reopen this one?

@cowtowncoder
Copy link
Member

Yup.

FWIW, I think key handling might now work without additional changes here. But a test would be good to have.

@cowtowncoder cowtowncoder reopened this Dec 14, 2014
@ias-dt
Copy link

ias-dt commented May 8, 2015

We have a Multimap with Enum keys.
MultimapBuilder.enumKeys(CloudProvider.class).arrayListValues().build()
The Jackson ObjectMapper has the SerializationFeature.WRITE_ENUMS_USING_TO_STRING set to false, as well as WRITE_ENUMS_USING_INDEX set to false.
We therefore would expect the keys of the Multimap to be serialized via Enum.name()
But instead they are serialized as Enum.toString()
(c.f. EnumMapSerializer)

@cowtowncoder
Copy link
Member

Could this be same as:

FasterXML/jackson-databind#749

NOTE: should only affect root values, but not values with Enum-key Multimaps.

@cowtowncoder
Copy link
Member

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

No branches or pull requests

4 participants