Skip to content

Commit da901c9

Browse files
Add files via upload
1 parent 50a18b1 commit da901c9

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package head;
2+
3+
import java.util.List;
4+
5+
import ugp.org.SerialX.Serializer;
6+
7+
public final class Bar extends Foo //Sample object that inheres
8+
{
9+
byte by0 = (byte) 142;
10+
short s0 = 555;
11+
double d2 = 5;
12+
Object sampleParent;
13+
14+
public Bar(Object... args) {
15+
// TODO Auto-generated constructor stub
16+
}
17+
18+
@Override
19+
public String toString()
20+
{
21+
return "Bar[" + a + " " + b + " " + c + " " + d + " " + f + " " + ch + " " + s + " " + nah + " " + l + " " + by0 + " " + s0 + "]";
22+
}
23+
24+
public static class BarProtocol extends FooProtocol //Protocol to serialize Bar
25+
{
26+
@Override
27+
public Object[] serialize(Foo object)
28+
{
29+
return new Object[] {object.a, object.b, object.c, object.d, object.f, object.ch, object.s, object.nah, object.l, ((Bar) object).by0, ((Bar) object).s0, Serializer.Code("$parent")};
30+
}
31+
32+
@SuppressWarnings("unchecked")
33+
@Override
34+
public Foo unserialize(Class<? extends Foo> objectClass, Object... args)
35+
{
36+
Bar f = new Bar();
37+
f.a = (int) args[0];
38+
f.b = (int) args[1];
39+
f.c = (int) args[2];
40+
f.d = (double) args[3];
41+
f.f = (float) args[4];
42+
f.ch = (char) args[5];
43+
f.s = (String) args[6];
44+
f.nah = (boolean) args[7];
45+
f.l = (List<Object>) args[8];
46+
f.by0 = (byte) args[9];
47+
f.s0 = (short) args[10];
48+
f.sampleParent = args[11];
49+
50+
return f;
51+
}
52+
53+
@Override
54+
public Class<? extends Foo> applicableFor()
55+
{
56+
return Bar.class;
57+
}
58+
}
59+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package head;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Random;
7+
import java.util.concurrent.CopyOnWriteArrayList;
8+
9+
import ugp.org.SerialX.Protocols.SerializationProtocol;
10+
11+
public class Foo //Sample object
12+
{
13+
int a = 8, b = 1, c = 456;
14+
double d = 5;
15+
float f = 1453.364564564132454654511324f;
16+
char ch = 'l';
17+
String s = "a";
18+
boolean nah = false;
19+
List<Object> l = new CopyOnWriteArrayList<Object>(Arrays.asList(6, 45,464654, 9.9, 56f));
20+
21+
public Foo()
22+
{
23+
l.add(6);
24+
l.add(9);
25+
l.add(13);
26+
l.add(new Random());
27+
l.add(new ArrayList<>(new ArrayList<>(new ArrayList<>(Arrays.asList(4, 5,6d)))));
28+
}
29+
30+
@Override
31+
public String toString()
32+
{
33+
return "Foo[" + a + " " + b + " " + c + " " + d + " " + f + " " + ch + " " + s + " " + nah + " " + l + "]";
34+
}
35+
36+
public static class FooProtocol extends SerializationProtocol<Foo> //Protocol to serialize Foo
37+
{
38+
@Override
39+
public Object[] serialize(Foo object)
40+
{
41+
return new Object[] {};
42+
}
43+
44+
@SuppressWarnings("unchecked")
45+
@Override
46+
public Foo unserialize(Class<? extends Foo> objectClass, Object... args)
47+
{
48+
Foo f = new Foo();
49+
f.a = (int) args[0];
50+
f.b = (int) args[1];
51+
f.c = (int) args[2];
52+
f.d = (double) args[3];
53+
f.f = (float) args[4];
54+
f.ch = (char) args[5];
55+
f.s = (String) args[6];
56+
f.nah = (boolean) args[7];
57+
f.l = (List<Object>) args[8];
58+
59+
return f;
60+
}
61+
62+
@Override
63+
public Class<? extends Foo> applicableFor()
64+
{
65+
return Foo.class;
66+
}
67+
};
68+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)