Enum is being Serialized withType, but for Deserializing the enum is not.

346 views
Skip to first unread message

Tyler Williams

unread,
Jul 9, 2021, 5:56:01 PM7/9/21
to jackson-user
"testTypes": [
"java.util.HashSet",
[
[
"com.company.core.TestType",
"SCREENING_1"
],
[
"com.company.core.TestType",
"SCREENING_2"
],
[
"com.company.core.TestType",
"SCREENING_3"
]
]
],

The above is a serialized HashSet using the As.Property typing on the ObjectMapper. I believe this is correct behavior because these enums are in the Set and this is what As.Property does in this situation.
The problem I'm having is that the same ObjectMapper that wrote that JSON can't deserialize it.
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `com.company.core.TestType` from String "com.company.core.TestType": not one of the values accepted for Enum class: [SCREENING_1, SCREENING_2, SCREENING_3]
 at [Source: (byte[])"["java.util.ArrayList",[{"@class":"com.company.ContainerType","id":1,"testTypes":["java.util.HashSet",[["company.core.TestType","SCREENING_1"],["company.core.TestType","SCREENING_2"],["company.core.TestType","SCREENING_3"]]],"shipDestinationTypes":["java.util.HashSet",[["com.company.mod"[truncated 19402 bytes]; line: 1, column: 229] (through reference chain: java.util.ArrayList[0]->com.company.ContainerType["testTypes"]->java.util.HashSet[0])
com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1851)
com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1079)
com.fasterxml.jackson.databind.deser.std.EnumDeserializer._deserializeAltString(EnumDeserializer.java:327)
com.fasterxml.jackson.databind.deser.std.EnumDeserializer._fromString(EnumDeserializer.java:214)
com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:188)
com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeWrappedValue(StdDeserializer.java:368)
com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromArray(StdDeserializer.java:215)
com.fasterxml.jackson.databind.deser.std.EnumDeserializer._deserializeOther(EnumDeserializer.java:337)
com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:201)
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:347)

My guess from looking at this stuff through the debugger is that the SimpleType of the enum doesn't create a typedeseriealizer and thus we just deserialize the enum by calling EnumDeserializer.deserialize() instead of EnumDeserializer.deserializeWithType(). Is there a configuration that I'm missing?

Tatu Saloranta

unread,
Jul 9, 2021, 6:13:08 PM7/9/21
to jackson-user
I think you are correct on most points, but to know for sure I'd need
to see the actual type definition (not description, definition),
as well as specific code being used for writing and reading.

My guess is that there is a discrepancy between type information
available during reading and writing.

-+ Tatu +-

Tyler Williams

unread,
Jul 9, 2021, 7:08:12 PM7/9/21
to jackson-user
I'm not sure what you mean by "actual type definition".

The specific code being used for writing would be the PersistentCollectionSerializer, which, from what I can gather, converts the value to be serialized into a java.util version and then sends that to an instance of CollectionSerializer. The ObjectMapper's encoding is set by jackson-security modules for spring security. There are no custom serializers or deserializers for this enum or the "ContainerType" object.

Tyler

Tatu Saloranta

unread,
Jul 9, 2021, 9:03:26 PM7/9/21
to jackson-user
On Fri, Jul 9, 2021 at 4:08 PM Tyler Williams <tyler_w...@sbcglobal.net> wrote:
I'm not sure what you mean by "actual type definition".

The specific code being used for writing would be the PersistentCollectionSerializer, which, from what I can gather, converts the value to be serialized into a java.util version and then sends that to an instance of CollectionSerializer. The ObjectMapper's encoding is set by jackson-security modules for spring security. There are no custom serializers or deserializers for this enum or the "ContainerType" object.


What I mean is I'd need a reproduction, including ContainterType.
Sounds like Enum is simple on its own, but code to read and write (actual code, not description) would be needed.
And from `PersistentCollectionSerializer` it sounds like this is for Hibernate types, handled by jackson-datatype-hibernate[version].

From your description I think the use case should be fine (and would pass if using plain old java.util.ArrayList for example) but I suspect that Hibernate Collection handling (by Jackson module) is where there is something missing, compared to how standard CollectionSerializer/-Deserializer does things. This would relate to polymorphic handling of values.

-+ 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 view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/bfb5069a-9aba-4641-ac1e-5d3109735e1an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages