Serializers can't be shared between keys and values any longer?

23 views
Skip to first unread message

Benson Margulies

unread,
Sep 18, 2015, 8:50:06 AM9/18/15
to jackso...@googlegroups.com
In moving some code from 2.4.x to 2.6.x, I got dinged by a customer
serializer for an enum that called
jsonGenerator.writeString(languageCode.ISO639_3());.

So I really need two classes that differ only in how they write?

languageCodeKey(com.basistech.rosette.dm.json.plain.EnumModuleTest)
Time elapsed: 0.007 sec <<< ERROR!
com.fasterxml.jackson.core.JsonGenerationException: Can not write a
string, expecting field name
at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1649)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._verifyValueWrite(WriterBasedJsonGenerator.java:775)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeString(WriterBasedJsonGenerator.java:329)

Tatu Saloranta

unread,
Sep 18, 2015, 1:33:35 PM9/18/15
to jackso...@googlegroups.com
I may be wrong, but I think that the code may have been relying on a flaw in JsonGenerator, where code did not verify proper sequencing of FIELD_NAMEs and matching values.
I realize that from user perspective it is a semantic change, but the intent was never to allow writing of sequences of values within JSON Object. The missing check was making it more difficult to catch other broken usage that resulted in invalid JSON being written without exception to caller.

So, yes, there is a difference between value and key serializers, from the earlier Jackson version.
This is not an ideal situation (if I was writing Jackson from Scratch, I'd make JsonSerializer have separate method), but it has existed from the beginning.

One possible fix might be to allow use of `@JsonValue` for key serialization. I don't remember if this is already done for case where return value is `String`. If not, I would be happy to work on this as an RFE.
Another thing that might help is that `toString()` may be used for key serialization as well, depending on SerializationFeature.WRITE_ENUMS_USING_TO_STRING.

-+ Tatu +-



--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages