-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix Enum
deserialization with JsonFormat.Shape.OBJECT
using both DELEGATING
and PROPERTIES
creator modes
#3851
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15e34c3
Fix
JooHyukKim 5b01c7c
add comment
JooHyukKim c5add48
Reduce cognitive complexity
JooHyukKim fb7c843
Synchronize JsonParser
JooHyukKim 8e38787
Remove transient keyword
JooHyukKim 3bc0de2
construct _propCreatorRef lazily
JooHyukKim 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 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.concurrent.atomic.AtomicReference; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
|
@@ -40,10 +41,12 @@ class FactoryBasedEnumDeserializer | |
|
||
/** | ||
* Lazily instantiated property-based creator. | ||
* Introduced in 2.8 and wrapped with {@link AtomicReference} in 2.15 | ||
* | ||
* @since 2.15 | ||
* | ||
* @since 2.8 | ||
*/ | ||
private transient PropertyBasedCreator _propCreator; | ||
private AtomicReference<PropertyBasedCreator> _propCreatorRef = new AtomicReference<>(null); | ||
|
||
public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType, | ||
ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps) | ||
|
@@ -132,18 +135,22 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx | |
// 30-Mar-2020, tatu: For properties-based one, MUST get JSON Object (before | ||
// 2.11, was just assuming match) | ||
if (_creatorProps != null) { | ||
if (!p.isExpectedStartObjectToken()) { | ||
if (p.isExpectedStartObjectToken()) { | ||
if (_propCreatorRef.get() == null) { | ||
_propCreatorRef.compareAndSet(null, | ||
PropertyBasedCreator.construct(ctxt, _valueInstantiator, _creatorProps, | ||
ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES))); | ||
} | ||
p.nextToken(); | ||
return deserializeEnumUsingPropertyBased(p, ctxt, _propCreatorRef.get()); | ||
} | ||
// If value cannot possibly be delegating-creator, | ||
if (!_valueInstantiator.canCreateFromString()) { | ||
final JavaType targetType = getValueType(ctxt); | ||
ctxt.reportInputMismatch(targetType, | ||
"Input mismatch reading Enum %s: properties-based `@JsonCreator` (%s) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.%s", | ||
ClassUtil.getTypeDescription(targetType), _factory, p.currentToken()); | ||
} | ||
if (_propCreator == null) { | ||
_propCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, _creatorProps, | ||
ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)); | ||
"Input mismatch reading Enum %s: properties-based `@JsonCreator` (%s) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.%s", | ||
ClassUtil.getTypeDescription(targetType), _factory, p.currentToken()); | ||
} | ||
p.nextToken(); | ||
return deserializeEnumUsingPropertyBased(p, ctxt, _propCreator); | ||
} | ||
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. Summary here,
|
||
|
||
// 12-Oct-2021, tatu: We really should only get here if and when String | ||
|
This file contains 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.
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.
Ok, I'll change this afterwards.... realized that for serializability of deserializer, better to use volatile after all.
Thank you for changes
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.
@cowtowncoder I can change it to volatile and handle it like before if you'd like?
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.
@JooHyukKim yes, basically I did that; no need to go back and forth. Sorry about confusion.
(the problem with JDK (de)serialization of JsonDeserializers is that we do not want to serialize fields like this, but in case of
AtomicReference
would need to make sure it's not-null ...volatile
is easier, and performance won't matter a lot here-- although to be completely honest I think lazy construction here is probably not a good idea. Could just create eagerly.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.
Sure, I will make a PR soon.·Oh you did already.
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.
Right, just the volatile part.