dkunzler / esperandro

Easy SharedPreference Engine foR ANDROid
Other
180 stars 14 forks source link

Serializable problem #36

Closed MFlisar closed 8 years ago

MFlisar commented 8 years ago

Currently, if I use serializable lists, they are not working, as the generated code is wrapping a list in a container, that is NOT implementing serializable itself...

NOT Working: Using following does crash, as the wrapper class is not serializable:

ArrayList<String> existingSources();
boolean existingSources(ArrayList<String>existingSources);

working

OwnWrapper existingSources();
boolean existingSources(OwnWrapper  existingSources);

With OwnWrapper like following:

public static class OwnWrapper implements Serializable
{
    public ArrayList<String> sources;

    public OwnWrapper ()
    {
        sources = new ArrayList<>();
    }
}
dkunzler commented 8 years ago

Thanks. I will look into it!

dkunzler commented 8 years ago

Follow-Up question; What Serializer are you using? Something provided by me (gson or jackson) or something custom? I cannot reproduce it using Unit Tests locally. Neither gson nor jackson require Java Serializable Interface to serialize an Object to a json String.

MFlisar commented 8 years ago

My own implementation... Looks like following:

Setup

Esperandro.setSerializer(new Serializer() {
        @Override
        public String serialize(Object object) {
            return StringUtil.serialize(object);
        }

        @Override
        public <T> T deserialize(String serializedObject, Class<T> clazz) {
            return StringUtil.deserialize(serializedObject, clazz);
        }
    });

And my StringUtil class:

public static String serialize(Object object)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try
    {
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        oos.flush();
        oos.close();
    } catch (IOException e)
    {
        L.e(StringUtil.class, e);
    }
    return Base64.encodeToString(baos.toByteArray(), 0);
}

public static <T> T deserialize(String serializedObject, Class<T> clazz)
{
    if (serializedObject == null)
        return (T)null;

    byte [] data = Base64.decode(serializedObject, 0);
    Object o = null;
    try
    {
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
        o = ois.readObject();
        ois.close();
    }
    catch (IOException e)
    {
        L.e(StringUtil.class, e);
    }
    catch (ClassNotFoundException e) {
        L.e(StringUtil.class, e);
    }

    return (T)o;
}
dkunzler commented 8 years ago

Ok, I see. I will add the Serializable interface ASAP.

dkunzler commented 8 years ago

I just uploaded 2.3.0 to maven which fixes this problem. Should get synced in a few hours.

MFlisar commented 8 years ago

There's another problem:

Reading an uninitilised serialisable object fails, as adding the null value to the cache will fail with a java.lang.NullPointerException: key == null || value == null exception...