Serializer

classic Classic list List threaded Threaded
2 messages Options
javastuff.sam@gmail.com javastuff.sam@gmail.com
Reply | Threaded
Open this post in threaded view
|

Serializer

Hi

We have multiple off-heap caches defined.  We have implemented Binarylizable for each cache object. So far so good.

We have a requirement we have to store List or Map of Binarylizable objects in a cache. How do I attempt to write serializer for List and Map?

These List and Map are extended object with some extra logic on top of basic ArrayList or LinkedHashMap.

Thanks,
-Sam


dkarachentsev dkarachentsev
Reply | Threaded
Open this post in threaded view
|

Re: Serializer

Hi,

You may use your custom collections as is - they will be deserialized in correct type.
If you want to implement Binarylizable with additional serialization logic, you can use something like follows (but this approach will need intermediate object):

   private static class CustomList<T> extends ArrayList<T> implements Binarylizable {
        @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException {
            writer.rawWriter().writeObjectArray(toArray());
        }

        @Override public void readBinary(BinaryReader reader) throws BinaryObjectException {
            Object[] arr = reader.rawReader().readObjectArray();

            for (Object o : arr)
                add((T)o);
        }
    }

    private static class CustomMap<K, V> extends LinkedHashMap<K, V> implements Binarylizable {
        @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException {
            writer.rawWriter().writeMap(new HashMap<>(this));
        }

        @Override public void readBinary(BinaryReader reader) throws BinaryObjectException {
            putAll((Map<? extends K, ? extends V>)reader.rawReader().readMap());
        }

    }