custom serializer issue

70 views
Skip to first unread message

Aaron Weber

unread,
Feb 5, 2016, 2:23:46 PM2/5/16
to MapDB
I'm trying to implement a simple serializer to use with my custom class.

I know my class can be serialized to JSON (I have done it elsewhere), so I thought this would be an easy way to go.  However, on startup, when it is referenced as a .valueSerializer() , I get:
java.lang.IllegalArgumentException: Not serializable: class com.x.y.ext.JSONMapSerializer$2
at org.mapdb.SerializerBase.assertSerializable(SerializerBase.java:1482)
at org.mapdb.DB.createHashMap(DB.java:415)
at org.mapdb.DB$HTreeMapMaker.make(DB.java:216)
at org.mapdb.DB$HTreeMapMaker.makeOrGet(DB.java:222)

Not sure why.  The actual code is as follows:
    public static final Serializer<Favorites> FAVORITES_SERIALIZER = new Serializer<Favorites>()
    {
        @Override
        public void serialize(DataOutput dataOutput, Favorites favorites)
                throws IOException
        {
            Gson gson = new Gson();
            dataOutput.writeUTF(gson.toJson(favorites));
        }

        @Override
        public Favorites deserialize(DataInput dataInput, int i)
                throws IOException
        {
            Gson gson = new Gson();
            return gson.fromJson(dataInput.readUTF(), Favorites.class);
        }

        @Override
        public int fixedSize()
        {
            return 0;
        }
    };


It's probably not tremendously fast, but I don't need it to be in my case...just need it to work. :)

Thanks for any hints/tips/tricks!
-AJ

Igor Castang

unread,
Feb 6, 2016, 9:50:14 AM2/6/16
to MapDB
a lot of versions of MapDB require the serializer class itself to be serializable. I forget if it's the case for the 2.0.X and 3.0.X branch, but I am pretty sure it's a need for any 1.0.X branch.
You should try to use a non anonymous class and make it implement serializable and try again.

regards

Jan Kotek

unread,
Feb 9, 2016, 12:51:11 PM2/9/16
to MapDB
Yes, that is correct. Your serializer should be standalone class and implement Serializable interface.
Reply all
Reply to author
Forward
0 new messages