-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial commit for json serialization #24
base: master
Are you sure you want to change the base?
Changes from 11 commits
f5e1301
eb80730
26bc186
95d58ba
3e0494b
423872b
f2aba30
c362a49
9d85f66
53d3925
368fcad
56f57be
8d35a4a
a74a543
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,12 @@ | |
package org.lwes; | ||
|
||
import java.lang.reflect.Array; | ||
import java.math.BigInteger; | ||
|
||
import org.lwes.serializer.StringConverter; | ||
import org.lwes.serializer.StringParser; | ||
import org.lwes.util.EncodedString; | ||
import org.lwes.util.IPAddress; | ||
|
||
/** | ||
* This class provides a base type for the base types in the event system. acts | ||
|
@@ -366,6 +369,76 @@ public BaseType cloneBaseType() { | |
public String toString() { | ||
return typeObject.toString(); | ||
} | ||
|
||
public Object stringyfy(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stringify(), perhaps, but how is this intended to differ from toString()? If there are two different string representations, there should be a comment to identify the reason and what the difference is. |
||
|
||
if(typeObject == null) | ||
return null; | ||
|
||
switch (type) { | ||
case BOOLEAN: | ||
case BYTE: | ||
case DOUBLE: | ||
case FLOAT: | ||
case INT16: | ||
case INT32: | ||
case INT64: | ||
case UINT16: | ||
case UINT32: | ||
case UINT64: | ||
case STRING: | ||
return typeObject.toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, but this is an unnecessary line; it could just fall through to the next case. |
||
case IPADDR: | ||
return typeObject.toString(); | ||
case BOOLEAN_ARRAY: | ||
return StringConverter.strArray((boolean[]) typeObject); | ||
case BYTE_ARRAY: | ||
return StringConverter.strArray((byte[]) typeObject); | ||
case DOUBLE_ARRAY: | ||
return StringConverter.strArray((double[]) typeObject); | ||
case FLOAT_ARRAY: | ||
return StringConverter.strArray((float[]) typeObject); | ||
case INT16_ARRAY: | ||
return StringConverter.strArray((short[]) typeObject); | ||
case INT32_ARRAY: | ||
return StringConverter.strArray((int[]) typeObject); | ||
case INT64_ARRAY: | ||
return StringConverter.strArray((long[]) typeObject); | ||
case IP_ADDR_ARRAY: | ||
return StringConverter.<IPAddress>strArray((IPAddress[]) typeObject); | ||
case STRING_ARRAY: | ||
return StringConverter.<String>strArray((String[]) typeObject); | ||
case UINT16_ARRAY: | ||
return StringConverter.strArray((int[]) typeObject); | ||
case UINT32_ARRAY: | ||
return StringConverter.strArray((long[]) typeObject); | ||
case UINT64_ARRAY: | ||
return StringConverter.<BigInteger>strArray((BigInteger[]) typeObject); | ||
case NBOOLEAN_ARRAY: | ||
return StringConverter.<Boolean>strArray((Boolean[]) typeObject); | ||
case NBYTE_ARRAY: | ||
return StringConverter.<Byte>strArray((Byte[]) typeObject); | ||
case NDOUBLE_ARRAY: | ||
return StringConverter.<Double>strArray((Double[]) typeObject); | ||
case NFLOAT_ARRAY: | ||
return StringConverter.<Float>strArray((Float[]) typeObject); | ||
case NINT16_ARRAY: | ||
return StringConverter.<Short>strArray((Short[]) typeObject); | ||
case NINT32_ARRAY: | ||
return StringConverter.<Integer>strArray((Integer[]) typeObject); | ||
case NINT64_ARRAY: | ||
return StringConverter.<Long>strArray((Long[]) typeObject); | ||
case NSTRING_ARRAY: | ||
return StringConverter.<String>strArray((String[]) typeObject); | ||
case NUINT16_ARRAY: | ||
return StringConverter.<Integer>strArray((Integer[]) typeObject); | ||
case NUINT32_ARRAY: | ||
return StringConverter.<Long>strArray((Long[]) typeObject); | ||
case NUINT64_ARRAY: | ||
return StringConverter.<BigInteger>strArray((BigInteger[]) typeObject); | ||
} | ||
return null; | ||
} | ||
|
||
public String getComment() { | ||
return comment; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.lwes; | ||
|
||
public enum EventImplementation { | ||
MAP_EVENT, ARRAY_EVENT; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,73 +16,78 @@ | |
import org.lwes.util.IPAddress; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for adding a second text representation of each type? Why can't the existing one be used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new text representation is what we want to use going forward for the types, as the current one is too specific to java, like the use of "[Luint16" notation to represent arrays. This new representation is going to be more language neutral |
||
|
||
public enum FieldType { | ||
UINT16(0x01, "uint16", 0), | ||
INT16(0x02, "int16", (short) 0), | ||
UINT32(0x03, "uint32", 0L), | ||
INT32(0x04, "int32", 0), | ||
STRING(0x05, "string", ""), | ||
IPADDR(0x06, "ip_addr", new IPAddress()), | ||
INT64(0x07, "int64", 0L), | ||
UINT64(0x08, "uint64", BigInteger.ZERO), | ||
BOOLEAN(0x09, "boolean", true), | ||
BYTE(0x0A, "byte", (byte) 0), | ||
FLOAT(0x0B, "float", (float) 0.0), | ||
DOUBLE(0x0C, "double", 0.0), | ||
UINT16(0x01, "uint16", 0, "uint16"), | ||
INT16(0x02, "int16", (short) 0, "int16"), | ||
UINT32(0x03, "uint32", 0L, "uint32"), | ||
INT32(0x04, "int32", 0, "int32"), | ||
STRING(0x05, "string", "", "string"), | ||
IPADDR(0x06, "ip_addr", new IPAddress(), "ip_addr"), | ||
INT64(0x07, "int64", 0L, "int64"), | ||
UINT64(0x08, "uint64", BigInteger.ZERO, "uint64"), | ||
BOOLEAN(0x09, "boolean", true, "boolean"), | ||
BYTE(0x0A, "byte", (byte) 0, "byte"), | ||
FLOAT(0x0B, "float", (float) 0.0, "float"), | ||
DOUBLE(0x0C, "double", 0.0, "double"), | ||
|
||
// Primitive Arrays | ||
UINT16_ARRAY(0x81, "[Luint16", new short[0]), | ||
INT16_ARRAY(0x82, "[Lint16", new int[0]), | ||
UINT32_ARRAY(0x83, "[Luint32", new int[0]), | ||
INT32_ARRAY(0x84, "[Lint32", new long[0]), | ||
STRING_ARRAY(0x85, "[Lstring", new String[0]), | ||
IP_ADDR_ARRAY(0x86, "[Lip_addr", new IPAddress[0]), | ||
INT64_ARRAY(0x87, "[Lint64", new long[0]), | ||
UINT64_ARRAY(0x88, "[Luint64", new BigInteger[0]), | ||
BOOLEAN_ARRAY(0x89, "[Lboolean", new boolean[0]), | ||
BYTE_ARRAY(0x8A, "[Lbyte", new byte[0]), | ||
FLOAT_ARRAY(0x8B, "[Lfloat", new float[0]), | ||
DOUBLE_ARRAY(0x8C, "[Ldouble", new double[0]), | ||
UINT16_ARRAY(0x81, "[Luint16", new short[0], "uint16_array"), | ||
INT16_ARRAY(0x82, "[Lint16", new int[0], "int16_array"), | ||
UINT32_ARRAY(0x83, "[Luint32", new int[0], "uint32_array"), | ||
INT32_ARRAY(0x84, "[Lint32", new long[0], "int32_array"), | ||
STRING_ARRAY(0x85, "[Lstring", new String[0], "string_array"), | ||
IP_ADDR_ARRAY(0x86, "[Lip_addr", new IPAddress[0], "ip_addr_array"), | ||
INT64_ARRAY(0x87, "[Lint64", new long[0], "int64_array"), | ||
UINT64_ARRAY(0x88, "[Luint64", new BigInteger[0], "uint64_array"), | ||
BOOLEAN_ARRAY(0x89, "[Lboolean", new boolean[0], "boolean_array"), | ||
BYTE_ARRAY(0x8A, "[Lbyte", new byte[0], "byte_array"), | ||
FLOAT_ARRAY(0x8B, "[Lfloat", new float[0], "float_array"), | ||
DOUBLE_ARRAY(0x8C, "[Ldouble", new double[0], "double_array"), | ||
|
||
// Nullable, object backed arrays | ||
NUINT16_ARRAY(0x8D, "[LNuint16", new Short[0]), | ||
NINT16_ARRAY(0x8E, "[LNint16", new Integer[0]), | ||
NUINT32_ARRAY(0x8F, "[LNuint32", new Integer[0]), | ||
NINT32_ARRAY(0x90, "[LNint32", new Long[0]), | ||
NSTRING_ARRAY(0x91, "[LString", new String[0]), | ||
NUINT16_ARRAY(0x8D, "[LNuint16", new Short[0], "nullable_uint16_array"), | ||
NINT16_ARRAY(0x8E, "[LNint16", new Integer[0], "nullable_int16_array"), | ||
NUINT32_ARRAY(0x8F, "[LNuint32", new Integer[0], "nullable_uint32_array"), | ||
NINT32_ARRAY(0x90, "[LNint32", new Long[0], "nullable_int32_array"), | ||
NSTRING_ARRAY(0x91, "[LString", new String[0], "nullable_string_array"), | ||
// N_IP_ADDR_ARRAY not implemented... 0x92 | ||
NINT64_ARRAY(0x93, "[LNint64", new Long[0]), | ||
NUINT64_ARRAY(0x94, "[LNuint64", new BigInteger[0]), | ||
NBOOLEAN_ARRAY(0x95, "[LBoolean", new Boolean[0]), | ||
NBYTE_ARRAY(0x96, "[LByte", new Byte[0]), | ||
NFLOAT_ARRAY(0x97, "[LFloat", new Float[0]), | ||
NDOUBLE_ARRAY(0x98, "[LDouble", new Double[0]); | ||
NINT64_ARRAY(0x93, "[LNint64", new Long[0], "nullable_int64_array"), | ||
NUINT64_ARRAY(0x94, "[LNuint64", new BigInteger[0], "nullable_uint64_array"), | ||
NBOOLEAN_ARRAY(0x95, "[LBoolean", new Boolean[0], "nullable_boolean_array"), | ||
NBYTE_ARRAY(0x96, "[LByte", new Byte[0], "nullable_byte_array"), | ||
NFLOAT_ARRAY(0x97, "[LFloat", new Float[0], "nullable_float_array"), | ||
NDOUBLE_ARRAY(0x98, "[LDouble", new Double[0], "nullable_double_array"); | ||
|
||
public final byte token; | ||
public final String name; | ||
public final String typeDesc; | ||
private Integer constantSize; | ||
private final boolean array, nullableArray; | ||
private FieldType componentType, arrayType, nullableArrayType; | ||
private final Object defaultValue; | ||
private static final FieldType[] TYPES_BY_TOKEN = new FieldType[256]; | ||
private static final Map<String, FieldType> TYPES_BY_NAME; | ||
private static final Map<String, FieldType> TYPES_BY_DESC; | ||
|
||
private FieldType(int token, String name) { | ||
this(token, name, null); | ||
this(token, name, null, null); | ||
} | ||
|
||
private FieldType(int token, String name, Object defaultValue) { | ||
private FieldType(int token, String name, Object defaultValue, String typeDesc) { | ||
this.token = (byte) token; | ||
this.name = name; | ||
this.array = name.startsWith("[L"); | ||
this.nullableArray = this.array && name().startsWith("N"); | ||
this.defaultValue = defaultValue; | ||
this.typeDesc = typeDesc; | ||
} | ||
|
||
static { | ||
TYPES_BY_NAME = new HashMap<String, FieldType>(); | ||
TYPES_BY_DESC = new HashMap<String, FieldType>(); | ||
for (FieldType type : values()) { | ||
TYPES_BY_TOKEN[type.token & 0xff] = type; | ||
TYPES_BY_NAME.put(type.name, type); | ||
TYPES_BY_DESC.put(type.typeDesc, type); | ||
|
||
if (type.isArray()) { | ||
// This will fail if our naming becomes inconsistent or a type starts with N. | ||
|
@@ -128,6 +133,14 @@ public static FieldType byName(String name) { | |
return type; | ||
} | ||
|
||
public static FieldType byTypeDesc(String typeDesc) { | ||
final FieldType type = TYPES_BY_DESC.get(typeDesc); | ||
if (type == null) { | ||
throw new IllegalArgumentException("Bad type description: " + typeDesc); | ||
} | ||
return type; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.lwes; | ||
|
||
public class TypeValue { | ||
|
||
private String type; | ||
private Object value; | ||
public String getType() { | ||
return type; | ||
} | ||
public Object getValue() { | ||
return value; | ||
} | ||
public TypeValue(String type, Object value) { | ||
super(); | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the changes here are correct. I think that instead of keeping around another structure, you should instead just convert in this function. It may require deserializing and then converting to json. @pfarner may have a similar opinion, but hopefully he'll see this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the extra structure i was using to hold attributes.