-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[databind#4849] Allow standard defaultTyping with EnumSet<E>
#4857
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
Changes from all commits
5fe3099
ffc2726
149435b
a747657
dfe7612
edfe7ac
06d4b5b
a13be38
e36455b
54efe72
80a7b86
b8dcf64
274e8ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.Iterator; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
|
@@ -26,6 +27,15 @@ public class CollectionSerializer | |
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Flag that indicates that we may need to check for EnumSet dynamically | ||
* during serialization: problem being that we can't always do it statically. | ||
* But we can figure out when there is a possibility wrt type signature we get. | ||
* | ||
* @since 2.18.3 | ||
*/ | ||
private final boolean _maybeEnumSet; | ||
|
||
/* | ||
/********************************************************** | ||
/* Life-cycle | ||
|
@@ -38,22 +48,17 @@ public class CollectionSerializer | |
public CollectionSerializer(JavaType elemType, boolean staticTyping, TypeSerializer vts, | ||
JsonSerializer<Object> valueSerializer) { | ||
super(Collection.class, elemType, staticTyping, vts, valueSerializer); | ||
} | ||
|
||
/** | ||
* @deprecated since 2.6 | ||
*/ | ||
@Deprecated // since 2.6 | ||
public CollectionSerializer(JavaType elemType, boolean staticTyping, TypeSerializer vts, | ||
BeanProperty property, JsonSerializer<Object> valueSerializer) { | ||
// note: assumption is 'property' is always passed as null | ||
this(elemType, staticTyping, vts, valueSerializer); | ||
// Unfortunately we can't check for EnumSet statically (if type indicated it, | ||
// we'd have constructed `EnumSetSerializer` instead). But we can check that | ||
// element type could possibly be an Enum. | ||
_maybeEnumSet = elemType.isEnumType() || elemType.isJavaLangObject(); | ||
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For 3.x version, would it be worth the effort trying to find other way instead of this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is a way, sure, it would be great to get rid of this work-around. |
||
} | ||
|
||
public CollectionSerializer(CollectionSerializer src, | ||
BeanProperty property, TypeSerializer vts, JsonSerializer<?> valueSerializer, | ||
Boolean unwrapSingle) { | ||
super(src, property, vts, valueSerializer, unwrapSingle); | ||
_maybeEnumSet = src._maybeEnumSet; | ||
} | ||
|
||
@Override | ||
|
@@ -120,7 +125,9 @@ public void serializeContents(Collection<?> value, JsonGenerator g, SerializerPr | |
return; | ||
} | ||
PropertySerializerMap serializers = _dynamicSerializers; | ||
final TypeSerializer typeSer = _valueTypeSerializer; | ||
// [databind#4849]/[databind#4214]: need to check for EnumSet | ||
final TypeSerializer typeSer = (_maybeEnumSet && value instanceof EnumSet<?>) | ||
? null : _valueTypeSerializer; | ||
|
||
int i = 0; | ||
try { | ||
|
@@ -158,7 +165,9 @@ public void serializeContentsUsing(Collection<?> value, JsonGenerator g, Seriali | |
{ | ||
Iterator<?> it = value.iterator(); | ||
if (it.hasNext()) { | ||
TypeSerializer typeSer = _valueTypeSerializer; | ||
// [databind#4849]/[databind#4214]: need to check for EnumSet | ||
final TypeSerializer typeSer = (_maybeEnumSet && value instanceof EnumSet<?>) | ||
? null : _valueTypeSerializer; | ||
int i = 0; | ||
do { | ||
Object elem = it.next(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.