Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default serialiser without dependencies #50

Closed
MFlisar opened this issue Feb 8, 2018 · 2 comments
Closed

Default serialiser without dependencies #50

MFlisar opened this issue Feb 8, 2018 · 2 comments

Comments

@MFlisar
Copy link

MFlisar commented Feb 8, 2018

could you add a default serialiser that does not depend on anything special? No jackson and no gson?

I usually use following in all my projects:

public class SerializationUtil {

	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(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(e);
		} catch (ClassNotFoundException e) {
			L.e(e);
		}

		return (T) o;
	}
}

This could be packed into a small extension...

@dkunzler
Copy link
Owner

I'm not a huge fan of ObjectStream serialisation. I am willing to add some serializer without dependencies if you have a good idea for an implementation, but as you have seen in your other issue this is not very robust. JSON for example would also survive changes in fields of classes without runtime error.

@MFlisar
Copy link
Author

MFlisar commented Feb 17, 2018

I don't have an idea for a more simply serialisation currently, but I understand your concerns and it make sense to me. It's just two functions though, I have them in my local core library I use in all my projects, so I'm fine with this. Was just a suggestion

@MFlisar MFlisar closed this as completed Feb 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants