Skip to content
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

Add deserialization support for Table<R, C, V> #1

Closed
cowtowncoder opened this issue Mar 30, 2016 · 5 comments · Fixed by #163
Closed

Add deserialization support for Table<R, C, V> #1

cowtowncoder opened this issue Mar 30, 2016 · 5 comments · Fixed by #163

Comments

@cowtowncoder
Copy link
Member

(note: moved from FasterXML/jackson-datatype-guava#11)

The Guava Table<R, C, V> data structure is a convenience wrapper around a Map<R, Map<C, V>>. This form can be easily serialized and deserialized. Here's the basic code I'm using for that:

    public static class TableSerializer extends JsonSerializer<Table<?, ?, ?>> {
        @Override
        public void serialize(final Table<?, ?, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException,
                    JsonProcessingException {
            jgen.writeObject(value.rowMap());
        }


        @Override
        public Class<Table<?, ?, ?>> handledType() {
            return (Class)Table.class;
        }
    } // end class TableSerializer

    public static class TableDeserializer extends JsonDeserializer<Table<?, ?, ?>> {
        @Override
        public Table<?, ?, ?> deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
            final ImmutableTable.Builder<Object, Object, Object> tableBuilder = ImmutableTable.builder();
            final Map<Object, Map<Object, Object>> rowMap = jp.readValueAs(Map.class);
            for (final Map.Entry<Object, Map<Object, Object>> rowEntry : rowMap.entrySet()) {
                final Object rowKey = rowEntry.getKey();
                for (final Map.Entry<Object, Object> cellEntry : rowEntry.getValue().entrySet()) {
                    final Object colKey = cellEntry.getKey();
                    final Object val = cellEntry.getValue();
                    tableBuilder.put(rowKey, colKey, val);
                }
            }
            return tableBuilder.build();
        }
    } // end class TableDeserializer

I have not tested if this correctly supports contextual keys or type (de)serialization. I looked at MultimapSerializer and MultimapDeserializer, and they are much more complicated even though a Multimap<K, V> is just a convenience wrapper around Map<K, Collection<V>>, so maybe this is too simplistic?

Notably, this implementation always deserializes to an ImmutableTable. Heuristics could be used to determine what Table implementation to reconstruct, e.g., if every row, column value is non-null, could create ArrayTable. HashBasedTable would be a good mutable default.

@cowtowncoder
Copy link
Member Author

@cowtowncoder
Copy link
Member Author

Also note that Jackson 2.7 has serialization support, but NOT yet deserialization -- this is why issue is still open.

@cowtowncoder
Copy link
Member Author

@michaelhixson @hyandell just FYI that Guava has moved; still hoping to eventually get to support Tables fully, but haven't had time to work on merging any further.

@lrlinde
Copy link

lrlinde commented Aug 4, 2016

@cowtowncoder, I asked in this other PR
FasterXML/jackson-datatype-guava#11
But I see this was duplicating it, so I'll ask here! :p

Is there any news on this? We'll working around this issue in our project and would really appreciate fully support for Tables.

@cowtowncoder
Copy link
Member Author

@lrlinde at this point I would need a new PR for this project for deserialization support. I don't have time to work on this myself.

@cowtowncoder cowtowncoder changed the title (guava) Add support for Table<R, C, V> (guava) Add deserialization support for Table<R, C, V> Jul 28, 2017
renjanmenon pushed a commit to renjanmenon/jackson-datatypes-collections that referenced this issue Oct 28, 2022
chore(docs): README.md for jackson-datatype-eclipse-collections.
@cowtowncoder cowtowncoder changed the title (guava) Add deserialization support for Table<R, C, V> Add deserialization support for Table<R, C, V> Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants