Skip to content
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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<groupId>org.lwes</groupId>
<artifactId>lwes-java</artifactId>
<packaging>jar</packaging>
<version>1.4.3-SNAPSHOT</version>
<version>1.4.4-SNAPSHOT</version>
<name>lwes-java</name>
<description>Lightweight event system, java implementation</description>
<url>http://lwes.org</url>
Expand Down Expand Up @@ -48,6 +48,13 @@
</developers>

<dependencies>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lwes/ArrayEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.EnumMap;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
Expand All @@ -26,8 +27,10 @@
import org.apache.commons.lang3.mutable.MutableInt;
import org.lwes.serializer.Deserializer;
import org.lwes.serializer.DeserializerState;
import org.lwes.serializer.JsonSerializer;
import org.lwes.serializer.Serializer;
import org.lwes.util.EncodedString;
import org.lwes.util.EventTranslator;

public final class ArrayEvent extends DefaultEvent {

Expand Down Expand Up @@ -679,4 +682,16 @@ public Object getValue() {
return value;
}
}

public String json() {
return EventTranslator.arrayToMapEvent(this).json();
}
Copy link
Member

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.

Copy link
Author

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.


public String unTypedJson() {
return EventTranslator.arrayToMapEvent(this).unTypedJson();
}

public Map<String, Object> exportTypedAndUnTypedAttributes(){
return EventTranslator.arrayToMapEvent(this).exportTypedAndUnTypedAttributes();
}
}
73 changes: 73 additions & 0 deletions src/main/java/org/lwes/BaseType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -366,6 +369,76 @@ public BaseType cloneBaseType() {
public String toString() {
return typeObject.toString();
}

public Object stringyfy(){
Copy link
Member

Choose a reason for hiding this comment

The 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();
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/lwes/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.InetAddress;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.lwes.util.CharacterEncoding;
Expand Down Expand Up @@ -226,7 +227,13 @@ public interface Event extends Iterable<FieldAccessor> {
void deserialize(DataInput stream, int length) throws IOException;

int getBytesSize();


String json();

String unTypedJson();

Map<String, Object> exportTypedAndUnTypedAttributes();

// MISCELLANEOUS

Event copy();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/lwes/EventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.lwes;

import org.lwes.db.EventTemplateDB;
import org.lwes.serializer.JsonDeserializer;

import java.io.File;
import java.io.InputStream;
Expand Down Expand Up @@ -189,5 +190,22 @@ public Event createEvent(byte[] bytes, boolean validate) throws EventSystemExcep
}
return new MapEvent(bytes, validate, eventTemplateDB);
}

/**
* Create an event from its json representation
* @param json
* @param type
* @return
*/
public Event createEventFromJson(String json, EventImplementation type){
JsonDeserializer deSerializer = JsonDeserializer.getInstance();
switch (type) {
case MAP_EVENT:
return deSerializer.fromJson(json, new MapEvent());
case ARRAY_EVENT:
return deSerializer.fromJson(json, new ArrayEvent());
}
return null;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/lwes/EventImplementation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.lwes;

public enum EventImplementation {
MAP_EVENT, ARRAY_EVENT;
}
87 changes: 50 additions & 37 deletions src/main/java/org/lwes/FieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,78 @@
import org.lwes.util.IPAddress;
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/lwes/MapEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.io.DataOutput;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;
Expand All @@ -23,6 +25,7 @@
import org.lwes.db.EventTemplateDB;
import org.lwes.serializer.Deserializer;
import org.lwes.serializer.DeserializerState;
import org.lwes.serializer.JsonSerializer;
import org.lwes.serializer.Serializer;

public class MapEvent extends DefaultEvent {
Expand Down Expand Up @@ -606,4 +609,21 @@ public FieldType getType(String field) {
public int getBytesSize() {
return bytesStoreSize;
}

public String json() {
return JsonSerializer.getInstance().json(name, attributes);
}

public String unTypedJson() {
return JsonSerializer.getInstance().unTypedJson(name, attributes);
}

public Map<String, Object> exportTypedAndUnTypedAttributes(){
JsonSerializer helper = JsonSerializer.getInstance();
Map<String, Object> container = new HashMap<String, Object>();
container.put("typed", helper.getTypedAttributes(attributes));
container.put("attributes", helper.getUnTypedAttributes(name, attributes));
return container;
}

}
19 changes: 19 additions & 0 deletions src/main/java/org/lwes/TypeValue.java
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;
}

}
Loading