|
| 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.Scope; |
| 12 | +import ugp.org.SerialX.Serializer; |
| 13 | +import ugp.org.SerialX.protocols.SerializationProtocol; |
| 14 | + |
| 15 | +/** |
| 16 | + * This example is overview of general SerialX API functionalities! |
| 17 | + * We will look at how to serialize and deserialize objects in to file. We will also create protocols for our objects as well as for already existing ones! |
| 18 | + * |
| 19 | + * @author PETO |
| 20 | + * |
| 21 | + * @since 1.0.0 |
| 22 | + */ |
| 23 | +public class SimpleExampleOfUse |
| 24 | +{ |
| 25 | + public static void main(String[] args) throws Exception |
| 26 | + { |
| 27 | + //Protocol registration |
| 28 | + SerializationProtocol.REGISTRY.addAll(new Bar.BarProtocol(), new Foo.FooProtocol(), new SerializationProtocol<Random>() //Sample custom protocol to serialized Random. |
| 29 | + { //Random will be serialized also without protocol via classic Java Base64 because it implements java.io.Serializable! |
| 30 | + @Override |
| 31 | + public Object[] serialize(Random object) |
| 32 | + { |
| 33 | + try |
| 34 | + { |
| 35 | + Field f = Random.class.getDeclaredField("seed"); |
| 36 | + f.setAccessible(true); |
| 37 | + return new Object[] {((AtomicLong) f.get(object)).get()}; |
| 38 | + } |
| 39 | + catch (Exception e) |
| 40 | + { |
| 41 | + e.printStackTrace(); |
| 42 | + return new Object[] {-1}; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Random unserialize(Class<? extends Random> objectClass, Object... args) |
| 48 | + { |
| 49 | + return new Random((long) args[0]); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Class<? extends Random> applicableFor() |
| 54 | + { |
| 55 | + return Random.class; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + File f = new File("./test.juss"); //File to write and read from! |
| 60 | + |
| 61 | + //Sample objects |
| 62 | + Random r = new Random(); |
| 63 | + List<Object> list = new ArrayList<>(); |
| 64 | + for (int i = 0; i < 10; i++) |
| 65 | + list.add(r.nextBoolean() ? r.nextInt(i+1) : r.nextBoolean()); |
| 66 | + |
| 67 | + HashMap<String, Object> vars = new HashMap<>(); //Variables to serialize |
| 68 | + vars.put("yourMom", "is heavier than sun..."); |
| 69 | + vars.put("num", 6); |
| 70 | + |
| 71 | + int[][] ints = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}}; |
| 72 | + |
| 73 | + Serializer.generateComments = true; //Enabling comment generation |
| 74 | + |
| 75 | + Serializer.globalVariables.put("parent", "father"); //Setting global variables |
| 76 | + |
| 77 | + double t0 = System.nanoTime(); //Invokation of static members of this class (calling method "println" and obtaining "hello" field as argument! |
| 78 | + 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, ints, Serializer.Code("$num"), new Scope(), Serializer.StaticMember(SimpleExampleOfUse.class, "println", Serializer.StaticMember(SimpleExampleOfUse.class, "hello"))); //Saving to file (serializing) |
| 79 | + double t = System.nanoTime(); //This will insert an comment Another way to add variable except Map<String, Object> $ is used to obtain value from variable |
| 80 | + System.out.println("Write: " + (t-t0)/1000000); |
| 81 | + |
| 82 | + SerializationProtocol.REGISTRY.setActivityForAll(true); //Enabling all protocols, just in case |
| 83 | + t0 = System.nanoTime(); |
| 84 | + Scope scope = Serializer.LoadFrom(f); //Loading scope with variables and values from file! |
| 85 | + t = System.nanoTime(); |
| 86 | + System.out.println("Read: " + (t-t0)/1000000); |
| 87 | + |
| 88 | + scope = scope.filter(obj -> obj != null); //This will filter away every null value and variable! |
| 89 | + |
| 90 | + //Printing values and variables of scope!! |
| 91 | + System.out.println(scope.toVarMap()); |
| 92 | + System.out.println(scope.toValList()); |
| 93 | + } |
| 94 | + |
| 95 | + //We can invoke static things in JUSS! |
| 96 | + public static String hello = "Hello world!"; |
| 97 | + |
| 98 | + public static void println(String str) |
| 99 | + { |
| 100 | + System.out.println(str); |
| 101 | + } |
| 102 | +} |
0 commit comments