-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allow default enums with @JsonCreator
#4979
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
Merged
cowtowncoder
merged 11 commits into
FasterXML:2.19
from
dropofwill:unknown-with-creator-default-value
Mar 14, 2025
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
143b66f
Allow default enums with @JsonCreator
dropofwill db61eea
Merge branch '2.19' into unknown-with-creator-default-value
cowtowncoder 49f82aa
Merge branch '2.19' into unknown-with-creator-default-value
cowtowncoder 2fd467c
Merge branch '2.19' into unknown-with-creator-default-value
cowtowncoder 04f52ac
Remove extra character
cowtowncoder fb481b6
Add deprecated marker, re-order (to minimize diffs)
cowtowncoder 614fdb4
More deprecation markers etc
cowtowncoder 33cadef
Merge branch '2.19' into unknown-with-creator-default-value
cowtowncoder 0eef90e
Merge branch '2.19' into unknown-with-creator-default-value
cowtowncoder 4f84e9d
Minor tweak to handling
cowtowncoder 2304918
Update release notes
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.fasterxml.jackson.databind.deser.std; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
|
@@ -15,6 +16,7 @@ | |
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.type.LogicalType; | ||
import com.fasterxml.jackson.databind.util.ClassUtil; | ||
import com.fasterxml.jackson.databind.util.EnumResolver; | ||
|
||
/** | ||
* Deserializer that uses a single-String static factory method | ||
|
@@ -34,6 +36,7 @@ class FactoryBasedEnumDeserializer | |
protected final JsonDeserializer<?> _deser; | ||
protected final ValueInstantiator _valueInstantiator; | ||
protected final SettableBeanProperty[] _creatorProps; | ||
protected final Enum<?> _defaultValue; | ||
|
||
protected final boolean _hasArgs; | ||
|
||
|
@@ -45,7 +48,8 @@ class FactoryBasedEnumDeserializer | |
private transient volatile PropertyBasedCreator _propCreator; | ||
|
||
public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType, | ||
ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps) | ||
ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps, | ||
EnumResolver enumResolver) | ||
dropofwill marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
super(cls); | ||
_factory = f; | ||
|
@@ -56,6 +60,13 @@ public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType pa | |
_deser = null; | ||
_valueInstantiator = valueInstantiator; | ||
_creatorProps = creatorProps; | ||
_defaultValue = Objects.nonNull(enumResolver)? enumResolver.getDefaultValue() : null; | ||
} | ||
|
||
public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType, | ||
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. Needs to be marked |
||
ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps) | ||
{ | ||
this(cls, f, paramType, valueInstantiator, creatorProps, null); | ||
} | ||
|
||
/** | ||
|
@@ -70,6 +81,7 @@ public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f) | |
_deser = null; | ||
_valueInstantiator = null; | ||
_creatorProps = null; | ||
_defaultValue = null; | ||
} | ||
|
||
protected FactoryBasedEnumDeserializer(FactoryBasedEnumDeserializer base, | ||
|
@@ -80,6 +92,7 @@ protected FactoryBasedEnumDeserializer(FactoryBasedEnumDeserializer base, | |
_hasArgs = base._hasArgs; | ||
_valueInstantiator = base._valueInstantiator; | ||
_creatorProps = base._creatorProps; | ||
_defaultValue = base._defaultValue; | ||
|
||
_deser = deser; | ||
} | ||
|
@@ -202,6 +215,10 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx | |
if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) { | ||
return null; | ||
} | ||
|
||
if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)) { | ||
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. I'll move this before |
||
return _defaultValue; | ||
} | ||
// 12-Oct-2021, tatu: Should probably try to provide better exception since | ||
// we likely hit argument incompatibility... Or can this happen? | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.