magro / kryo-serializers

More kryo serializers
Apache License 2.0
381 stars 120 forks source link

How can EnumMapSerializer work with nested EnumMaps? #90

Open whipit opened 6 years ago

whipit commented 6 years ago

This is a feature request, not a bug.

If I have a nested map like so EnumMap<K1, EnumMap<K2, V>>, how can the EnumMapSerializer be modified to work with it?

At present, it doesn't quite work since the value of the top-level map is (de)serialized as an object instead of as another EnumMap.

Mr14huashao commented 4 years ago

PR can't pass CI, but test case : @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testEnumMapNest() throws Exception { _kryo.register(HashSet.class); _kryo.register(Vipers.class); _kryo.register(Colors.class); _kryo.register(EnumMap.class, new EnumMapSerializer());

    final Set<String> mambaAka = new HashSet<String>();
    final Set<String> papaAka = new HashSet<>();
    mambaAka.add("Beatrix Kiddo");
    mambaAka.add("The Bride");
    papaAka.add("Good Job");
    papaAka.add("There is Great");

    _original.put(Vipers.BLACK_MAMBA, mambaAka);
    _original.put(Vipers.SIDEWINDER, papaAka);
    _newTempMap.put(Vipers.COPPERHEAD, _original);

    final File outputFile = File.createTempFile("input_file", "dat");
    try (final Output output = new Output(new FileOutputStream(outputFile))) {
        _kryo.writeObject(output, _newTempMap);
        output.flush();
        output.close();
        final Input input = new Input(new FileInputStream(outputFile));
        final EnumMap tEnumMap = _kryo.readObject(input, EnumMap.class);
        input.close();
        Assert.assertEquals(_newTempMap, tEnumMap);
    }
}