Skip to content

Commit

Permalink
updating examples to 1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyProgrammer committed Aug 30, 2022
1 parent fda2127 commit 10b0856
Show file tree
Hide file tree
Showing 14 changed files with 962 additions and 0 deletions.
85 changes: 85 additions & 0 deletions examples/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package examples;

import java.util.List;

public final class Bar extends Foo //Sample object that inheres
{
byte by0 = (byte) 142;
short s0 = 555;
double d2 = 5;
Object sampleParent;

@Override
public String toString()
{
return "Bar[" + a + " " + b + " " + c + " " + d + " " + f + " " + ch + " " + s + " " + nah + " " + l + " " + by0 + " " + s0 + " " + sampleParent+"]";
}

public static class BarProtocol extends FooProtocol //Protocol to serialize Bar
{
@Override
public Object[] serialize(Foo object)
{
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!*/};
}

@SuppressWarnings("unchecked")
@Override
public Foo unserialize(Class<? extends Foo> objectClass, Object... args)
{
Bar f = new Bar();
f.a = (int) args[0];
f.b = (int) args[1];
f.c = (int) args[2];
f.d = (double) args[3];
f.f = (float) args[4];
f.ch = (char) args[5];
f.s = (String) args[6];
f.nah = (boolean) args[7];
f.l = (List<Object>) args[8];
f.by0 = (byte) args[9];
f.s0 = (short) args[10];
f.sampleParent = args[11];

return f;
}

@Override
public Class<? extends Foo> applicableFor()
{
return Bar.class;
}
}

public byte getBy0() {
return by0;
}

public void setBy0(byte by0) {
this.by0 = by0;
}

public short getS0() {
return s0;
}

public void setS0(short s0) {
this.s0 = s0;
}

public double getD2() {
return d2;
}

public void setD2(double d2) {
this.d2 = d2;
}

public Object getSampleParent() {
return sampleParent;
}

public void setSampleParent(Object sampleParent) {
this.sampleParent = sampleParent;
}
}
142 changes: 142 additions & 0 deletions examples/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

import org.ugp.serialx.protocols.SerializationProtocol;

public class Foo //Sample object to be serialized using its protocol!
{
int a = 8, b = 1, c = 456;
double d = 5;
float f = 1453.364564564132454654511324f;
char ch = 'l';
String s = "a";
boolean nah = false;
List<Object> l = new CopyOnWriteArrayList<Object>(Arrays.asList(6, 45, 464654, 9.9, 56f));

public Foo()
{
l.add(6);
l.add(9);
l.add(13);
l.add(new Random());
l.add(new ArrayList<>(Arrays.asList(4, 5, 6d, new ArrayList<>(), "hi")));
}

@Override
public String toString()
{
return "Foo[" + a + " " + b + " " + c + " " + d + " " + f + " " + ch + " " + s + " " + nah + " " + l + "]";
}

public static class FooProtocol extends SerializationProtocol<Foo> //Protocol to serialize Foo
{
@Override
public Object[] serialize(Foo object)
{
return new Object[] {object.a, object.b, object.c, object.d, object.f, object.ch, object.s, object.nah, object.l};
}

@SuppressWarnings("unchecked")
@Override
public Foo unserialize(Class<? extends Foo> objectClass, Object... args)
{
Foo f = new Foo();
f.a = (int) args[0];
f.b = (int) args[1];
f.c = (int) args[2];
f.d = (double) args[3];
f.f = (float) args[4];
f.ch = (char) args[5];
f.s = (String) args[6];
f.nah = (boolean) args[7];
f.l = (List<Object>) args[8];

return f;
}

@Override
public Class<? extends Foo> applicableFor()
{
return Foo.class;
}
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int getB() {
return b;
}

public void setB(int b) {
this.b = b;
}

public int getC() {
return c;
}

public void setC(int c) {
this.c = c;
}

public double getD() {
return d;
}

public void setD(double d) {
this.d = d;
}

public float getF() {
return f;
}

public void setF(float f) {
this.f = f;
}

public char getCh() {
return ch;
}

public void setCh(char ch) {
this.ch = ch;
}

public String getS() {
return s;
}

public void setS(String s) {
this.s = s;
}

public boolean isNah() {
return nah;
}

public void setNah(boolean nah) {
this.nah = nah;
}

public List<Object> getL() {
return l;
}

public void setL(List<Object> l) {
this.l = l;
};

public static void a() {};
}
48 changes: 48 additions & 0 deletions examples/MemberInvokeOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package examples;

import static org.ugp.serialx.Serializer.InvokeFunc;
import static org.ugp.serialx.Serializer.indexOfNotInObj;
import static org.ugp.serialx.Serializer.splitValues;

import java.lang.reflect.InvocationTargetException;

import org.ugp.serialx.JussSerializer;
import org.ugp.serialx.converters.DataParser;
import org.ugp.serialx.converters.ObjectConverter;

/**
* This is example of more advanced parser! It can be used for calling non-static methods from objects via "->" operator!<br>
* 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>
* 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!
*
* @author PETO
*
* @serial 1.3.5
*/
public class MemberInvokeOperator implements DataParser
{
@Override
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
{
int index;
if ((index = indexOfNotInObj(str, "->", false)) > 0)
{
Object obj = myHomeRegistry.parse(str.substring(0, index).trim(), args);
String[] funcArgs = splitValues(str.substring(index+2).trim(), ' ');

try
{
return InvokeFunc(obj, funcArgs[0], ObjectConverter.parseAll(myHomeRegistry, funcArgs, 1, true, args));
}
catch (InvocationTargetException e)
{
throw new RuntimeException(e);
}
catch (Exception e2)
{
return null;
}
}
return CONTINUE;
}
}
37 changes: 37 additions & 0 deletions examples/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package examples;

import org.ugp.serialx.SerializationDebugger;
import org.ugp.serialx.protocols.SelfSerializable;

/**
* Example of self-serializable object!
* 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!
*
* @author PETO
*
* @see SelfSerializable
*
* @since 1.3.2
*/
public class Message implements SelfSerializable
{
public String str;
public int date;

public Message(String str, int date)
{
this.str = str;
this.date = date;
}

@Override
public String toString() {
return "Message["+str+", "+date+"]";
}

@Override
public Object[] serialize()
{
return new Object[] {str, date};
}
}
35 changes: 35 additions & 0 deletions examples/TryParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package examples;

import static org.ugp.serialx.Serializer.indexOfNotInObj;

import org.ugp.serialx.converters.DataParser;

/**
* This is another example of more "advanced" parser. This one allow you to use "try" keyword and catching exceptions!
* 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!
*
* @author PETO
*
* @since 1.3.5
*
* @see MemberInvokeOperator
*/
public class TryParser implements DataParser
{
@Override
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
{
if (indexOfNotInObj(str = str.trim(), "try") == 0)
{
try
{
return myHomeRegistry.parse(str.substring(3).trim(), false, new Class<?>[] {getClass()}, args);
}
catch (Exception e)
{
return e;
}
}
return CONTINUE;
}
}
39 changes: 39 additions & 0 deletions examples/implementations/AdvancedParsersExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package examples.implementations;

import java.io.File;

import org.ugp.serialx.JussSerializer;
import org.ugp.serialx.LogProvider;
import org.ugp.serialx.converters.VariableConverter;

import examples.MemberInvokeOperator;
import examples.TryParser;

/**
* In this example we will create our very own simple scripting language by using {@link MemberInvokeOperator} and {@link TryParser}
* together with {@link JussSerializer#JUSS_PARSERS_AND_OPERATORS}!
* As you can see with SerialX capable of far more than parsing some JSON...
* Note: This is primarily for demonstrational purposes and might not be suitable for production...
*
* @author PETO
*
* @since 1.3.5
*/
public class AdvancedParsersExample
{
public static void main(String[] args) throws Exception
{
//In this case JussSerializer acts as an interpreter for our custom scripting language.
JussSerializer interpreter = new JussSerializer();

interpreter.setParsers(JussSerializer.JUSS_PARSERS_AND_OPERATORS); //Allowing usage of operators in our script!
interpreter.getParsers().addAllAfter(VariableConverter.class, new TryParser(), new MemberInvokeOperator()); //Allowing method calls and try expressions in our script!

LogProvider.instance.setReThrowException(true); //This allows us to implement custom exception handling!

interpreter.LoadFrom(new File("src/examples/implementations/simpleScript.juss")); //Running our script from simpleScript.juss file!

//Printing the results of our script...
//System.out.println(interpreter); //This is not necessary in this case!
}
}
Loading

0 comments on commit 10b0856

Please sign in to comment.