Skip to content

Commit 10b0856

Browse files
updating examples to 1.3.5
1 parent fda2127 commit 10b0856

14 files changed

+962
-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/MemberInvokeOperator.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package examples;
2+
3+
import static org.ugp.serialx.Serializer.InvokeFunc;
4+
import static org.ugp.serialx.Serializer.indexOfNotInObj;
5+
import static org.ugp.serialx.Serializer.splitValues;
6+
7+
import java.lang.reflect.InvocationTargetException;
8+
9+
import org.ugp.serialx.JussSerializer;
10+
import org.ugp.serialx.converters.DataParser;
11+
import org.ugp.serialx.converters.ObjectConverter;
12+
13+
/**
14+
* This is example of more advanced parser! It can be used for calling non-static methods from objects via "->" operator!<br>
15+
* For example with this parser registered with {@link JussSerializer#JUSS_PARSERS} you can print out hello world in JUSS like <code>System::out->println "Hello world"</code><br>
16+
* Note: This is only for demonstration purposes and not a real feature so its not fully compatible with JUSS syntax so you will have to use () quiet often depending on where you put this parser!
17+
*
18+
* @author PETO
19+
*
20+
* @serial 1.3.5
21+
*/
22+
public class MemberInvokeOperator implements DataParser
23+
{
24+
@Override
25+
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
26+
{
27+
int index;
28+
if ((index = indexOfNotInObj(str, "->", false)) > 0)
29+
{
30+
Object obj = myHomeRegistry.parse(str.substring(0, index).trim(), args);
31+
String[] funcArgs = splitValues(str.substring(index+2).trim(), ' ');
32+
33+
try
34+
{
35+
return InvokeFunc(obj, funcArgs[0], ObjectConverter.parseAll(myHomeRegistry, funcArgs, 1, true, args));
36+
}
37+
catch (InvocationTargetException e)
38+
{
39+
throw new RuntimeException(e);
40+
}
41+
catch (Exception e2)
42+
{
43+
return null;
44+
}
45+
}
46+
return CONTINUE;
47+
}
48+
}

examples/Message.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package examples;
2+
3+
import org.ugp.serialx.SerializationDebugger;
4+
import org.ugp.serialx.protocols.SelfSerializable;
5+
6+
/**
7+
* Example of self-serializable object!
8+
* SelfSerializable objects can be serialized directly without necessity of having any {@link SerializationDebugger}, all you need to do is implement {@link SelfSerializable} interface and override {@link SelfSerializable#serialize()} method accordingly!
9+
*
10+
* @author PETO
11+
*
12+
* @see SelfSerializable
13+
*
14+
* @since 1.3.2
15+
*/
16+
public class Message implements SelfSerializable
17+
{
18+
public String str;
19+
public int date;
20+
21+
public Message(String str, int date)
22+
{
23+
this.str = str;
24+
this.date = date;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Message["+str+", "+date+"]";
30+
}
31+
32+
@Override
33+
public Object[] serialize()
34+
{
35+
return new Object[] {str, date};
36+
}
37+
}

examples/TryParser.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 static org.ugp.serialx.Serializer.indexOfNotInObj;
4+
5+
import org.ugp.serialx.converters.DataParser;
6+
7+
/**
8+
* This is another example of more "advanced" parser. This one allow you to use "try" keyword and catching exceptions!
9+
* Note: This is only for demonstration purposes and not a real feature so its not fully compatible with JUSS syntax so you will have to use () quiet often depending on where you put this parser!
10+
*
11+
* @author PETO
12+
*
13+
* @since 1.3.5
14+
*
15+
* @see MemberInvokeOperator
16+
*/
17+
public class TryParser implements DataParser
18+
{
19+
@Override
20+
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
21+
{
22+
if (indexOfNotInObj(str = str.trim(), "try") == 0)
23+
{
24+
try
25+
{
26+
return myHomeRegistry.parse(str.substring(3).trim(), false, new Class<?>[] {getClass()}, args);
27+
}
28+
catch (Exception e)
29+
{
30+
return e;
31+
}
32+
}
33+
return CONTINUE;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package examples.implementations;
2+
3+
import java.io.File;
4+
5+
import org.ugp.serialx.JussSerializer;
6+
import org.ugp.serialx.LogProvider;
7+
import org.ugp.serialx.converters.VariableConverter;
8+
9+
import examples.MemberInvokeOperator;
10+
import examples.TryParser;
11+
12+
/**
13+
* In this example we will create our very own simple scripting language by using {@link MemberInvokeOperator} and {@link TryParser}
14+
* together with {@link JussSerializer#JUSS_PARSERS_AND_OPERATORS}!
15+
* As you can see with SerialX capable of far more than parsing some JSON...
16+
* Note: This is primarily for demonstrational purposes and might not be suitable for production...
17+
*
18+
* @author PETO
19+
*
20+
* @since 1.3.5
21+
*/
22+
public class AdvancedParsersExample
23+
{
24+
public static void main(String[] args) throws Exception
25+
{
26+
//In this case JussSerializer acts as an interpreter for our custom scripting language.
27+
JussSerializer interpreter = new JussSerializer();
28+
29+
interpreter.setParsers(JussSerializer.JUSS_PARSERS_AND_OPERATORS); //Allowing usage of operators in our script!
30+
interpreter.getParsers().addAllAfter(VariableConverter.class, new TryParser(), new MemberInvokeOperator()); //Allowing method calls and try expressions in our script!
31+
32+
LogProvider.instance.setReThrowException(true); //This allows us to implement custom exception handling!
33+
34+
interpreter.LoadFrom(new File("src/examples/implementations/simpleScript.juss")); //Running our script from simpleScript.juss file!
35+
36+
//Printing the results of our script...
37+
//System.out.println(interpreter); //This is not necessary in this case!
38+
}
39+
}

0 commit comments

Comments
 (0)