|
| 1 | +package head; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Random; |
| 9 | +import java.util.concurrent.atomic.AtomicLong; |
| 10 | + |
| 11 | +import ugp.org.SerialX.Serializer; |
| 12 | +import ugp.org.SerialX.Protocols.SerializationProtocol; |
| 13 | + |
| 14 | +public class Main |
| 15 | +{ |
| 16 | + public static void main(String[] args) |
| 17 | + { |
| 18 | + //Protocol registration |
| 19 | + Serializer.PROTOCOL_REGISTRY.addAll(new Bar.BarProtocol(), new Foo.FooProtocol(), new SerializationProtocol<Random>() //Sample custom protocol to serialized Random. |
| 20 | + { //Random will be serialized also without protocol via classic Java Base64 because it implements java.io.Serializable! |
| 21 | + @Override |
| 22 | + public Object[] serialize(Random object) |
| 23 | + { |
| 24 | + long seed = 0; |
| 25 | + try |
| 26 | + { |
| 27 | + Field f = Random.class.getDeclaredField("seed"); |
| 28 | + f.setAccessible(true); |
| 29 | + seed = ((AtomicLong) f.get(object)).get(); |
| 30 | + } |
| 31 | + catch (Exception e) |
| 32 | + { |
| 33 | + e.printStackTrace(); |
| 34 | + } |
| 35 | + return new Object[] {seed}; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Random unserialize(Class<? extends Random> objectClass, Object... args) |
| 40 | + { |
| 41 | + return new Random((long) args[0]); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Class<? extends Random> applicableFor() |
| 46 | + { |
| 47 | + return Random.class; |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + File f = new File("./test.srlx"); |
| 52 | + |
| 53 | + //Sample objects |
| 54 | + Random r = new Random(); |
| 55 | + List<Object> list = new ArrayList<>(); |
| 56 | + for (int i = 0; i < 10; i++) |
| 57 | + list.add(r.nextBoolean() ? r.nextInt(i+1) : r.nextBoolean()); |
| 58 | + int[] intArr = {1, 2, 3, 4}; |
| 59 | + |
| 60 | + HashMap<String, Object> vars = new HashMap<>(); //Variables to serialize |
| 61 | + vars.put("yourMom", "is heavier than sun..."); |
| 62 | + vars.put("num", 6); |
| 63 | + |
| 64 | + Serializer.generateComments = true; //Enabling comment generation |
| 65 | + |
| 66 | + Serializer.globalVariables.put("parent", "father"); //Setting global variables |
| 67 | + |
| 68 | + Serializer.PROTOCOL_REGISTRY.GetProtocolFor(String.class).setActive(false); //Disabling a string protocol. This will force Serializer to serialize string with regular Java Base64 because String implements java.io.Serializable! |
| 69 | + Serializer.SerializeTo(f, vars, "145asaa4144akhdgj31hahaXDDLol", r, list, Serializer.Comment("Size of array"), Serializer.Var("arrSize", list.size()), new Bar(), 1, 2.2, 3, 'A', true, false, null, intArr, Serializer.Code("$num")); //Saving to file (serializing) |
| 70 | + //This will insert an comment Another way to add variable except Map<String, Object> $ is used to obtain value from variable |
| 71 | + |
| 72 | + Serializer.PROTOCOL_REGISTRY.setActivityForAll(true); //Enabling all protocols |
| 73 | + System.out.println(Serializer.LoadFrom(f)); //Loading from file |
| 74 | + System.out.println(Serializer.LoadVariablesFrom(f)); //Loading variables from file |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments