Skip to content

Commit 87ace49

Browse files
new examples
1 parent a263496 commit 87ace49

File tree

6 files changed

+465
-0
lines changed

6 files changed

+465
-0
lines changed

examples/Bar.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package examples;
2+
3+
import java.util.List;
4+
5+
public final class Bar extends Foo //Sample object that inheres
6+
{
7+
byte by0 = (byte) 142;
8+
short s0 = 555;
9+
double d2 = 5;
10+
Object sampleParent;
11+
12+
@Override
13+
public String toString()
14+
{
15+
return "Bar[" + a + " " + b + " " + c + " " + d + " " + f + " " + ch + " " + s + " " + nah + " " + l + " " + by0 + " " + s0 + " " + sampleParent+"]";
16+
}
17+
18+
public static class BarProtocol extends FooProtocol //Protocol to serialize Bar
19+
{
20+
@Override
21+
public Object[] serialize(Foo object)
22+
{
23+
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, "${$parent}" /*If serialized with JussSerializer this will try to get value of parent property from certain scope!*/};
24+
}
25+
26+
@SuppressWarnings("unchecked")
27+
@Override
28+
public Foo unserialize(Class<? extends Foo> objectClass, Object... args)
29+
{
30+
Bar f = new Bar();
31+
f.a = (int) args[0];
32+
f.b = (int) args[1];
33+
f.c = (int) args[2];
34+
f.d = (double) args[3];
35+
f.f = (float) args[4];
36+
f.ch = (char) args[5];
37+
f.s = (String) args[6];
38+
f.nah = (boolean) args[7];
39+
f.l = (List<Object>) args[8];
40+
f.by0 = (byte) args[9];
41+
f.s0 = (short) args[10];
42+
f.sampleParent = args[11];
43+
44+
return f;
45+
}
46+
47+
@Override
48+
public Class<? extends Foo> applicableFor()
49+
{
50+
return Bar.class;
51+
}
52+
}
53+
54+
public byte getBy0() {
55+
return by0;
56+
}
57+
58+
public void setBy0(byte by0) {
59+
this.by0 = by0;
60+
}
61+
62+
public short getS0() {
63+
return s0;
64+
}
65+
66+
public void setS0(short s0) {
67+
this.s0 = s0;
68+
}
69+
70+
public double getD2() {
71+
return d2;
72+
}
73+
74+
public void setD2(double d2) {
75+
this.d2 = d2;
76+
}
77+
78+
public Object getSampleParent() {
79+
return sampleParent;
80+
}
81+
82+
public void setSampleParent(Object sampleParent) {
83+
this.sampleParent = sampleParent;
84+
}
85+
}

examples/Foo.java

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package examples;
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 org.ugp.serialx.protocols.SerializationProtocol;
10+
11+
public class Foo //Sample object to be serialized using its protocol!
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<>(Arrays.asList(4, 5, 6d, new ArrayList<>(), "hi")));
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[] {object.a, object.b, object.c, object.d, object.f, object.ch, object.s, object.nah, object.l};
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+
69+
public int getA() {
70+
return a;
71+
}
72+
73+
public void setA(int a) {
74+
this.a = a;
75+
}
76+
77+
public int getB() {
78+
return b;
79+
}
80+
81+
public void setB(int b) {
82+
this.b = b;
83+
}
84+
85+
public int getC() {
86+
return c;
87+
}
88+
89+
public void setC(int c) {
90+
this.c = c;
91+
}
92+
93+
public double getD() {
94+
return d;
95+
}
96+
97+
public void setD(double d) {
98+
this.d = d;
99+
}
100+
101+
public float getF() {
102+
return f;
103+
}
104+
105+
public void setF(float f) {
106+
this.f = f;
107+
}
108+
109+
public char getCh() {
110+
return ch;
111+
}
112+
113+
public void setCh(char ch) {
114+
this.ch = ch;
115+
}
116+
117+
public String getS() {
118+
return s;
119+
}
120+
121+
public void setS(String s) {
122+
this.s = s;
123+
}
124+
125+
public boolean isNah() {
126+
return nah;
127+
}
128+
129+
public void setNah(boolean nah) {
130+
this.nah = nah;
131+
}
132+
133+
public List<Object> getL() {
134+
return l;
135+
}
136+
137+
public void setL(List<Object> l) {
138+
this.l = l;
139+
};
140+
141+
public static void a() {};
142+
}

examples/GeneralExample.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package examples;
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 org.ugp.serialx.JussSerializer;
12+
import org.ugp.serialx.Scope;
13+
import org.ugp.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 using file. We will also create protocols for our objects as well as for already existing ones!
18+
* This example is also for benchmarking!
19+
*
20+
* @author PETO
21+
*
22+
* @since 1.0.0
23+
*/
24+
public class GeneralExample
25+
{
26+
public static void main(String[] args) throws Exception
27+
{
28+
//Protocol registration
29+
SerializationProtocol.REGISTRY.addAll(new Bar.BarProtocol(), new Foo.FooProtocol(), new SerializationProtocol<Random>() //Sample custom protocol to serialized Random.
30+
{ //Random will be serialized also without protocol via classic Java Base64 because it implements java.io.Serializable!
31+
@Override
32+
public Object[] serialize(Random object)
33+
{
34+
try
35+
{
36+
Field f = Random.class.getDeclaredField("seed");
37+
f.setAccessible(true);
38+
return new Object[] {((AtomicLong) f.get(object)).get()};
39+
}
40+
catch (Exception e)
41+
{
42+
e.printStackTrace();
43+
return new Object[] {-1};
44+
}
45+
}
46+
47+
@Override
48+
public Random unserialize(Class<? extends Random> objectClass, Object... args)
49+
{
50+
return new Random(((Number) args[0]).longValue());
51+
}
52+
53+
@Override
54+
public Class<? extends Random> applicableFor()
55+
{
56+
return Random.class;
57+
}
58+
});
59+
60+
File f = new File("test.juss"); //File to write and read from!
61+
62+
//Sample objects
63+
Random r = new Random();
64+
List<Object> list = new ArrayList<>();
65+
for (int i = 0; i < 8; i++)
66+
list.add(r.nextBoolean() ? r.nextInt(i+1) : r.nextBoolean());
67+
68+
HashMap<String, Object> vars = new HashMap<>(); //Variables to serialize
69+
vars.put("yourMom", "is heavier than sun...");
70+
vars.put("num", 6);
71+
72+
int[][] ints = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}};
73+
74+
//-------------------------------------------Serializing-------------------------------------------
75+
76+
JussSerializer serializer = new JussSerializer(vars); //Creating an instance of Serializer that will serialize objects using Juss! Serializer is instance of scope so it behaves like so!
77+
//Adding independent values Invokation of static members of this class (calling method "println" and obtaining "hello" field as argument!
78+
serializer.addAll("some string", 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(GeneralExample.class, "println", serializer.StaticMember(GeneralExample.class, "hello")));
79+
//This will insert an comment Another way to add variable except Map<String, Object> $ is used to obtain value from variable
80+
serializer.setGenerateComments(true); //Enabling comment generation
81+
82+
double t0 = System.nanoTime();
83+
serializer.SerializeTo(f); //Saving content of serializer to file (serializing)
84+
double t = System.nanoTime();
85+
System.out.println("Write: " + (t-t0)/1000000 + " ms"); //Write benchmark
86+
87+
//-------------------------------------------Deserializing-------------------------------------------
88+
89+
SerializationProtocol.REGISTRY.setActivityForAll(true); //Enabling all protocols, just in case...
90+
91+
JussSerializer deserializer = new JussSerializer(); //Creating instance of Serializer that will deserialize objects serialized in Juss (same class is responsible for serializing and deserializing)!
92+
deserializer.setParsers(JussSerializer.JUSS_PARSERS_AND_OPERATORS);
93+
deserializer.put("parent", "father"); //Setting global variables
94+
95+
t0 = System.nanoTime();
96+
deserializer.LoadFrom(f); //Loading content of file in to deserializer!
97+
t = System.nanoTime();
98+
System.out.println("Read: " + (t-t0)/1000000 + " ms"); //Read benchmark
99+
100+
//deserializer = (JussSerializer) deserializer.filter(obj -> obj != null); //This will filter away every null value and variable!
101+
102+
//Printing values and variables of scope!
103+
System.out.println(deserializer.variables());
104+
System.out.println(deserializer.values());
105+
}
106+
107+
//We can invoke static members in JUSS!
108+
public static String hello = "Hello world!";
109+
110+
public static void println(String str)
111+
{
112+
System.out.println(str);
113+
}
114+
}

examples/Message.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package examples;
2+
3+
import org.ugp.serialx.protocols.SelfSerializable;
4+
5+
/**
6+
* Example of self-serializable object!
7+
*
8+
* @author PETO
9+
*
10+
* @see SelfSerializable
11+
*
12+
* @since 1.3.2
13+
*/
14+
public class Message implements SelfSerializable
15+
{
16+
public String str;
17+
public int date;
18+
19+
public Message(String str, int date)
20+
{
21+
this.str = str;
22+
this.date = date;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Message["+str+", "+date+"]";
28+
}
29+
30+
@Override
31+
public Object[] serialize()
32+
{
33+
return new Object[] {str, date};
34+
}
35+
}

0 commit comments

Comments
 (0)