jackson-datatypes-collections icon indicating copy to clipboard operation
jackson-datatypes-collections copied to clipboard

(guava) Add deserialization support for Table<R, C, V>

Open cowtowncoder opened this issue 9 years ago • 5 comments

(note: moved from https://github.com/FasterXML/jackson-datatype-guava/issues/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 avatar Mar 30 '16 02:03 cowtowncoder

Note: existing PRs from the old project:

  • https://github.com/FasterXML/jackson-datatype-guava/pull/62
  • https://github.com/FasterXML/jackson-datatype-guava/pull/73

cowtowncoder avatar Mar 30 '16 02:03 cowtowncoder

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

cowtowncoder avatar Mar 30 '16 02:03 cowtowncoder

@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.

cowtowncoder avatar May 13 '16 22:05 cowtowncoder

@cowtowncoder, I asked in this other PR https://github.com/FasterXML/jackson-datatype-guava/issues/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.

lrlinde avatar Aug 04 '16 10:08 lrlinde

@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 avatar Aug 09 '16 05:08 cowtowncoder