-
Notifications
You must be signed in to change notification settings - Fork 55
Add deserialization support for Table<R, C, V> #163
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 8 commits into
FasterXML:2.19
from
Abhishekkr3003:table-deserializer
Nov 5, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d8442de
adds support for deserialization of hash-based table
Abhishekkr3003 b2821c0
Merge branch '2.19' into table-deserializer
cowtowncoder c50a4d0
adds support for deserialization of immutable and tree based implemen…
Abhishekkr3003 8256232
uses MapLikeType instead of JavaType
Abhishekkr3003 015ed9c
Minor clean up
cowtowncoder 3d2fce0
Minor clean up
cowtowncoder a1d943e
Last minor tweaks
cowtowncoder d692ffe
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
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
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
44 changes: 44 additions & 0 deletions
44
...ain/java/com/fasterxml/jackson/datatype/guava/deser/table/HashBasedTableDeserializer.java
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.table; | ||
|
||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.KeyDeserializer; | ||
import com.fasterxml.jackson.databind.deser.NullValueProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.type.MapLikeType; | ||
import com.google.common.collect.HashBasedTable; | ||
|
||
/** | ||
* Provides deserialization for the Guava HashBasedTable class. | ||
* | ||
* @author Abhishekkr3003 | ||
*/ | ||
public class HashBasedTableDeserializer | ||
extends MutableTableDeserializer<HashBasedTable<Object, Object, Object>> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public HashBasedTableDeserializer(MapLikeType type) { | ||
super(type); | ||
} | ||
|
||
public HashBasedTableDeserializer(MapLikeType type, KeyDeserializer rowDeserializer, | ||
KeyDeserializer columnDeserializer, TypeDeserializer elementTypeDeserializer, | ||
JsonDeserializer<?> elementDeserializer, NullValueProvider nvp) { | ||
super(type, rowDeserializer, columnDeserializer, elementTypeDeserializer, | ||
elementDeserializer, nvp | ||
); | ||
} | ||
|
||
@Override | ||
protected HashBasedTable<Object, Object, Object> createTable() { | ||
return HashBasedTable.create(); | ||
} | ||
|
||
@Override | ||
protected JsonDeserializer<?> _createContextual(MapLikeType type, | ||
KeyDeserializer rowDeserializer, | ||
KeyDeserializer columnDeserializer, TypeDeserializer typeDeserializer, | ||
JsonDeserializer<?> elementDeserializer, NullValueProvider nvp) { | ||
return new HashBasedTableDeserializer(type, | ||
rowDeserializer, columnDeserializer, typeDeserializer, elementDeserializer, nvp); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...ain/java/com/fasterxml/jackson/datatype/guava/deser/table/ImmutableTableDeserializer.java
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.table; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.KeyDeserializer; | ||
import com.fasterxml.jackson.databind.deser.NullValueProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.type.MapLikeType; | ||
import com.fasterxml.jackson.databind.util.AccessPattern; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableTable; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Provides deserialization for the Guava ImmutableTable class. | ||
* | ||
* @author Abhishekkr3003 | ||
*/ | ||
public class ImmutableTableDeserializer | ||
extends TableDeserializer<ImmutableTable<Object, Object, Object>> { | ||
private static final long serialVersionUID = 2L; | ||
|
||
public ImmutableTableDeserializer(MapLikeType type) { | ||
super(type); | ||
} | ||
|
||
protected ImmutableTableDeserializer(MapLikeType type, KeyDeserializer rowDeserializer, | ||
KeyDeserializer colDeserializer, JsonDeserializer<?> valueDeserializer, | ||
TypeDeserializer valueTypeDeserializer, NullValueProvider nuller) { | ||
super(type, rowDeserializer, colDeserializer, valueTypeDeserializer, valueDeserializer, | ||
nuller | ||
); | ||
} | ||
|
||
@Override | ||
public AccessPattern getEmptyAccessPattern() { | ||
// immutable, hence: | ||
return AccessPattern.CONSTANT; | ||
} | ||
|
||
|
||
@Override | ||
public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException { | ||
return ImmutableMap.of(); | ||
} | ||
|
||
protected ImmutableTable.Builder<Object, Object, Object> createBuilder() { | ||
return ImmutableTable.builder(); | ||
} | ||
|
||
@Override | ||
public ImmutableTable<Object, Object, Object> deserialize(JsonParser p, | ||
DeserializationContext ctxt) throws IOException { | ||
ImmutableTable.Builder<Object, Object, Object> table = createBuilder(); | ||
|
||
JsonToken currToken = p.currentToken(); | ||
if (currToken != JsonToken.FIELD_NAME && currToken != JsonToken.END_OBJECT) { | ||
expect(p, JsonToken.START_OBJECT); | ||
currToken = p.nextToken(); | ||
} | ||
|
||
for (; currToken == JsonToken.FIELD_NAME; currToken = p.nextToken()) { | ||
final Object rowKey; | ||
if (_rowDeserializer != null) { | ||
rowKey = _rowDeserializer.deserializeKey(p.currentName(), ctxt); | ||
} else { | ||
rowKey = p.currentName(); | ||
} | ||
|
||
p.nextToken(); | ||
expect(p, JsonToken.START_OBJECT); | ||
|
||
for ( | ||
currToken = p.nextToken(); currToken == JsonToken.FIELD_NAME; | ||
currToken = p.nextToken()) { | ||
final Object colKey; | ||
if (_colDeserializer != null) { | ||
colKey = _colDeserializer.deserializeKey(p.currentName(), ctxt); | ||
} else { | ||
colKey = p.currentName(); | ||
} | ||
|
||
p.nextToken(); | ||
|
||
final Object value; | ||
if (p.currentToken() == JsonToken.VALUE_NULL) { | ||
if (_skipNullValues) { | ||
continue; | ||
} | ||
value = _nullProvider.getNullValue(ctxt); | ||
} else if (_valueTypeDeserializer != null) { | ||
value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer); | ||
} else { | ||
value = _valueDeserializer.deserialize(p, ctxt); | ||
} | ||
table.put(rowKey, colKey, value); | ||
} | ||
expect(p, JsonToken.END_OBJECT); | ||
} | ||
return table.build(); | ||
} | ||
|
||
@Override | ||
protected JsonDeserializer<?> _createContextual(MapLikeType type, | ||
KeyDeserializer rowDeserializer, | ||
KeyDeserializer columnDeserializer, TypeDeserializer valueTypeDeserializer, | ||
JsonDeserializer<?> valueDeserializer, NullValueProvider nullValueProvider) { | ||
return new ImmutableTableDeserializer(type, rowDeserializer, columnDeserializer, | ||
valueDeserializer, valueTypeDeserializer, nullValueProvider | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
@cowtowncoder, should we use MapLikeType/MapLikeDeserializer or not? What I found is if we keep it MapLikeType, then we have to use Type something like
Map<R, Map<C, V>>
, and then we'll have KeyDeserializer for RowKey and JsonDeserializer for ColumnValueMap, and then while deserializing theTable<R, C, V>
we'll have to deserializeMap<C, V>
first and then have to put eachC, V
inTable<R, C, V>
forR
, which is inefficient in my opinion. Contrarily, if we don't use MapLikeType and keep it JavaType (Using Bean Deserializer), we can deserialize directly toTable<R, C, V>
.What do you think about this?
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.
Hmmmh. Tough question. I think that:
MapLikeType
(to help Jackson databind be somewhat aware of type's semantic)MapLikeDeserializer
, if that is possibleIf (2) can't be done for some reason (I may be overlooking something), I'm ok in change to make Tables not be considered "Map-like". This will add some work on (de)serialization handling, possibly.
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 have used
MapLikeType
without using any other MapLikeType deserialization arguments, as if we use elementDeserializer, then we might have to first deserialize toMap<C, V>
and then have to put inTable<R,C,V>
, since we cannot getC
's deserializer andV
's deserializer separately fromJsonDeserializer<?> elementDeserializer
of this API:Well, this is from my limited knowledge of this codebase; please do correct me if I'm missing something here.
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.
That sounds good -- hoping to review this in near future, just having bit of overload with everything going on. But thank you again for this contribution!
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.
Thanks for the update! I completely understand, and no worries at all. Take your time. I’m happy to contribute and look forward to any feedback whenever you’re able to review. Thanks again!