-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix #1419: allow Map.Entry shape/format overrides #5397
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
Draft
cowtowncoder
wants to merge
9
commits into
3.x
Choose a base branch
from
tatu-claude/3.1/1419-map-entry-shape-override
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−26
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f787c45
Fix #1419: allow Map.Entry shape/format overrides
cowtowncoder 2345eb4
Minor tweaking
cowtowncoder 3aea1ff
Merge branch '3.x' into tatu-claude/3.1/1419-map-entry-shape-override
cowtowncoder d972576
Merge branch '3.x' into tatu-claude/3.1/1419-map-entry-shape-override
cowtowncoder 18d5c93
Streamline
cowtowncoder d10fa7b
...
cowtowncoder 9f8a825
...
cowtowncoder b519787
...
cowtowncoder fc9f49d
Merge branch '3.x' into tatu-claude/3.1/1419-map-entry-shape-override
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import java.util.*; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
|
|
||
| import tools.jackson.core.*; | ||
| import tools.jackson.databind.*; | ||
| import tools.jackson.databind.annotation.JacksonStdImpl; | ||
|
|
@@ -43,6 +45,14 @@ public class MapEntryDeserializer | |
| */ | ||
| protected final TypeDeserializer _valueTypeDeserializer; | ||
|
|
||
| /** | ||
| * Flag set when we should deserialize as POJO with "key" and "value" | ||
| * properties, instead of the default Map.Entry format. | ||
| * | ||
| * @since 3.1 (for [databind#1419]) | ||
| */ | ||
| protected final boolean _deserializeAsPOJO; | ||
|
|
||
| /* | ||
| /********************************************************************** | ||
| /* Life-cycle | ||
|
|
@@ -52,6 +62,13 @@ public class MapEntryDeserializer | |
| public MapEntryDeserializer(JavaType type, | ||
| KeyDeserializer keyDeser, ValueDeserializer<Object> valueDeser, | ||
| TypeDeserializer valueTypeDeser) | ||
| { | ||
| this(type, keyDeser, valueDeser, valueTypeDeser, false); | ||
| } | ||
|
|
||
| protected MapEntryDeserializer(JavaType type, | ||
| KeyDeserializer keyDeser, ValueDeserializer<Object> valueDeser, | ||
| TypeDeserializer valueTypeDeser, boolean deserializeAsPOJO) | ||
| { | ||
| super(type); | ||
| if (type.containedTypeCount() != 2) { // sanity check | ||
|
|
@@ -60,18 +77,7 @@ public MapEntryDeserializer(JavaType type, | |
| _keyDeserializer = keyDeser; | ||
| _valueDeserializer = valueDeser; | ||
| _valueTypeDeserializer = valueTypeDeser; | ||
| } | ||
|
|
||
| /** | ||
| * Copy-constructor that can be used by sub-classes to allow | ||
| * copy-on-write styling copying of settings of an existing instance. | ||
| */ | ||
| protected MapEntryDeserializer(MapEntryDeserializer src) | ||
| { | ||
| super(src); | ||
| _keyDeserializer = src._keyDeserializer; | ||
| _valueDeserializer = src._valueDeserializer; | ||
| _valueTypeDeserializer = src._valueTypeDeserializer; | ||
| _deserializeAsPOJO = deserializeAsPOJO; | ||
| } | ||
|
|
||
| protected MapEntryDeserializer(MapEntryDeserializer src, | ||
|
|
@@ -82,6 +88,7 @@ protected MapEntryDeserializer(MapEntryDeserializer src, | |
| _keyDeserializer = keyDeser; | ||
| _valueDeserializer = valueDeser; | ||
| _valueTypeDeserializer = valueTypeDeser; | ||
| _deserializeAsPOJO = src._deserializeAsPOJO; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -92,7 +99,6 @@ protected MapEntryDeserializer(MapEntryDeserializer src, | |
| protected MapEntryDeserializer withResolved(KeyDeserializer keyDeser, | ||
| TypeDeserializer valueTypeDeser, ValueDeserializer<?> valueDeser) | ||
| { | ||
|
|
||
| if ((_keyDeserializer == keyDeser) && (_valueDeserializer == valueDeser) | ||
| && (_valueTypeDeserializer == valueTypeDeser)) { | ||
| return this; | ||
|
|
@@ -117,10 +123,27 @@ public LogicalType logicalType() { | |
| * Method called to finalize setup of this deserializer, | ||
| * when it is known for which property deserializer is needed for. | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public ValueDeserializer<?> createContextual(DeserializationContext ctxt, | ||
| BeanProperty property) | ||
| { | ||
| // [databind#1419]: Check if property has @JsonFormat(shape=POJO) | ||
| boolean deserializeAsPOJO = _deserializeAsPOJO; | ||
| if (property != null) { | ||
| JsonFormat.Value format = property.findPropertyFormat(ctxt.getConfig(), Map.Entry.class); | ||
|
|
||
| switch (format.getShape()) { | ||
| case NATURAL: | ||
| deserializeAsPOJO = false; | ||
| break; | ||
| case POJO: | ||
| deserializeAsPOJO = true; | ||
| break; | ||
| default: // fall through | ||
| } | ||
| } | ||
|
|
||
| KeyDeserializer kd = _keyDeserializer; | ||
| if (kd == null) { | ||
| kd = ctxt.findKeyDeserializer(_containerType.containedType(0), property); | ||
|
|
@@ -141,7 +164,13 @@ public ValueDeserializer<?> createContextual(DeserializationContext ctxt, | |
| if (vtd != null) { | ||
| vtd = vtd.forProperty(property); | ||
| } | ||
| return withResolved(kd, vtd, vd); | ||
|
|
||
| MapEntryDeserializer deser = withResolved(kd, vtd, vd); | ||
| if (deserializeAsPOJO != _deserializeAsPOJO) { | ||
| return new MapEntryDeserializer(_containerType, kd, | ||
| (ValueDeserializer<Object>) vd, vtd, deserializeAsPOJO); | ||
| } | ||
| return deser; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -174,6 +203,11 @@ public ValueDeserializer<Object> getContentDeserializer() { | |
| public Map.Entry<Object,Object> deserialize(JsonParser p, DeserializationContext ctxt) | ||
| throws JacksonException | ||
| { | ||
| // [databind#1419]: If deserializing as POJO with "key" and "value" properties | ||
| if (_deserializeAsPOJO) { | ||
| return _deserializeAsPOJO(p, ctxt); | ||
| } | ||
|
|
||
| // Ok: must point to START_OBJECT, PROPERTY_NAME or END_OBJECT | ||
| JsonToken t = p.currentToken(); | ||
| if (t == JsonToken.START_OBJECT) { | ||
|
|
@@ -232,6 +266,76 @@ public Map.Entry<Object,Object> deserialize(JsonParser p, DeserializationContext | |
| return new AbstractMap.SimpleEntry<Object,Object>(key, value); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to deserialize Map.Entry as POJO with "key" and "value" properties. | ||
| * | ||
| * @since 3.1 (for [databind#1419]) | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| protected Map.Entry<Object,Object> _deserializeAsPOJO(JsonParser p, DeserializationContext ctxt) | ||
| throws JacksonException | ||
| { | ||
| JsonToken t = p.currentToken(); | ||
| if (t == JsonToken.START_OBJECT) { | ||
| t = p.nextToken(); | ||
| } else if (t != JsonToken.PROPERTY_NAME && t != JsonToken.END_OBJECT) { | ||
| if (t == JsonToken.START_ARRAY) { | ||
| return _deserializeFromArray(p, ctxt); | ||
| } | ||
| return (Map.Entry<Object,Object>) ctxt.handleUnexpectedToken(getValueType(ctxt), p); | ||
| } | ||
|
|
||
| final KeyDeserializer keyDes = _keyDeserializer; | ||
| final ValueDeserializer<Object> valueDes = _valueDeserializer; | ||
| final TypeDeserializer typeDeser = _valueTypeDeserializer; | ||
|
|
||
| Object key = null; | ||
| Object value = null; | ||
|
|
||
| // Read properties "key" and "value" | ||
| while (t == JsonToken.PROPERTY_NAME) { | ||
| String propName = p.currentName(); | ||
| t = p.nextToken(); // move to value | ||
|
|
||
| if ("key".equals(propName)) { | ||
| // Deserialize key | ||
| if (t == JsonToken.VALUE_NULL) { | ||
| key = keyDes.deserializeKey(null, ctxt); | ||
| } else if (t.isScalarValue()) { | ||
| key = keyDes.deserializeKey(p.getString(), ctxt); | ||
| } else { | ||
| ctxt.reportInputMismatch(this, | ||
| "Can not deserialize Map.Entry key from non-scalar JSON value"); | ||
| } | ||
| } else if ("value".equals(propName)) { | ||
| // Deserialize value | ||
| try { | ||
| if (t == JsonToken.VALUE_NULL) { | ||
| value = valueDes.getNullValue(ctxt); | ||
| } else if (typeDeser == null) { | ||
| value = valueDes.deserialize(p, ctxt); | ||
| } else { | ||
| value = valueDes.deserializeWithType(p, ctxt, typeDeser); | ||
| } | ||
| } catch (Exception e) { | ||
| wrapAndThrow(ctxt, e, Map.Entry.class, propName); | ||
| } | ||
| } else { | ||
| // Unknown property: check if we should fail or skip | ||
| handleUnknownProperty(p, ctxt, _containerType.getRawClass(), propName); | ||
| } | ||
|
|
||
| t = p.nextToken(); // move to next property or END_OBJECT | ||
| } | ||
|
|
||
|
Member
Author
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. Missing checks for missing (absent) "key" or "value"... |
||
| if (t != JsonToken.END_OBJECT) { | ||
| ctxt.reportInputMismatch(this, | ||
| "Problem binding JSON into Map.Entry: unexpected content: "+t); | ||
| } | ||
|
|
||
| return new AbstractMap.SimpleEntry<Object,Object>(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public Map.Entry<Object,Object> deserialize(JsonParser p, DeserializationContext ctxt, | ||
| Map.Entry<Object,Object> result) throws JacksonException | ||
|
|
||
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.
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.
This is actually wrong:
KeyDeserializeris only good for JSON Object property names; here we need a properValueDeserializer...