import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.math.BigInteger; import java.util.Arrays; import java.util.stream.Collectors; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface DontSerialize { } class CheckIt { CheckIt() { X = 3; Y = 4; } CheckIt(int x, int y) { X = x; Y = y; } public int X; @DontSerialize public int Y; } public class Serializer { public static void serialize(Object o, OutputStream os) throws IOException, IllegalArgumentException, IllegalAccessException { ObjectOutputStream oos = new ObjectOutputStream(os); if (o.getClass() == BigInteger.class) { oos.writeObject(((BigInteger)o).toByteArray()); //} else if (o.getClass().isPrimitive()) { } else { Field[] fs = Arrays.stream(o.getClass().getDeclaredFields()).filter((f) -> !f.isAnnotationPresent(DontSerialize.class)).collect(Collectors.toList()).toArray(new Field[]{}); /*Field[] fs = o.getClass().getFields(); int len = 0; for (Field f : fs) { if (!f.isAnnotationPresent(DontSerialize.class)) { len++; } } oos.writeInt(len);*/ oos.writeInt(fs.length); for (Field f : fs) { //if (!f.isAnnotationPresent(DontSerialize.class)) { oos.writeObject(f.getName()); boolean bacc = f.isAccessible(); if (!bacc) f.setAccessible(true); oos.writeObject(f.get(o)); if (!bacc) f.setAccessible(false); //} } } } public static Object deserialize(Class c, InputStream is) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException { ObjectInputStream ois = new ObjectInputStream(is); if (c == BigInteger.class) { return new BigInteger((byte[])ois.readObject()); } else { Object o = c.newInstance(); int len = ois.readInt(); while (len != 0) { Field f = c.getDeclaredField((String)ois.readObject()); boolean bacc = f.isAccessible(); if (!bacc) f.setAccessible(true); if (!Modifier.isFinal(f.getModifiers())) f.set(o, ois.readObject()); if (!bacc) f.setAccessible(false); len--; } return o; } } public static void main(String [] args) throws IllegalArgumentException, IllegalAccessException, IOException, ClassNotFoundException, InstantiationException, NoSuchFieldException, SecurityException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); serialize(new java.awt.Point(3, 4), baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); java.awt.Point p = (java.awt.Point)deserialize(java.awt.Point.class, bais); System.out.println(p.toString()); baos.reset(); serialize(new BigInteger("314159265359"), baos); bais = new ByteArrayInputStream(baos.toByteArray()); BigInteger b = (BigInteger)deserialize(BigInteger.class, bais); System.out.println(b.toString()); baos.reset(); serialize(new CheckIt(1, 2), baos); bais = new ByteArrayInputStream(baos.toByteArray()); CheckIt ci = (CheckIt)deserialize(CheckIt.class, bais); System.out.println(ci.X + " " + ci.Y); } }