-
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
Open
vikramkadi
wants to merge
14
commits into
master
Choose a base branch
from
feature/queasy-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f5e1301
Initial commit for json serialization
eb80730
organize imports
26bc186
Refine tests
95d58ba
refactoring
3e0494b
Test for max integer
423872b
refactoring
f2aba30
Refactoring
c362a49
Using our own string converter for serialize/deserialize instead of gson
9d85f66
Adding event name to attributes
53d3925
Having both typed and untyped
368fcad
More descriptive json type names
56f57be
Code review comments
8d35a4a
Version to 1.5.0-SNAPSHOT
a74a543
Change log
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.lwes; | ||
|
||
public enum EventImplementation { | ||
MAP_EVENT, ARRAY_EVENT; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.lwes; | ||
|
||
public class TypeValue { | ||
|
||
private String type; | ||
private String value; | ||
public String getType() { | ||
return type; | ||
} | ||
public String getValue() { | ||
return value; | ||
} | ||
public TypeValue(String type, String value) { | ||
super(); | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
} |
173 changes: 173 additions & 0 deletions
173
src/main/java/org/lwes/serializer/JsonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package org.lwes.serializer; | ||
|
||
import java.math.BigInteger; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.lwes.Event; | ||
import org.lwes.FieldType; | ||
import org.lwes.TypeValue; | ||
import org.lwes.util.IPAddress; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
public class JsonDeserializer { | ||
|
||
private static JsonDeserializer instance; | ||
|
||
private Gson gson; | ||
private JsonParser parser; | ||
|
||
public static JsonDeserializer getInstance(){ | ||
if(instance == null){ | ||
synchronized (JsonSerializer.class) { | ||
//Double-checked locking | ||
if(instance == null) | ||
instance = new JsonDeserializer(); | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
private JsonDeserializer(){ | ||
parser = new JsonParser(); | ||
GsonBuilder bldr = new GsonBuilder(); | ||
gson = bldr.create(); | ||
} | ||
|
||
public Event fromJson(String json, Event e){ | ||
try{ | ||
JsonObject root = parser.parse(json).getAsJsonObject(); | ||
String name = root.getAsJsonPrimitive("name").getAsString(); | ||
if(StringUtils.isEmpty(name)) | ||
return null; | ||
JsonObject typedContainer = root.getAsJsonObject("typed"); | ||
if(typedContainer == null) | ||
return null; | ||
Map<String, TypeValue> typedElems = parseTyped(typedContainer); | ||
|
||
e.setEventName(name); | ||
for(Entry<String, TypeValue> element : typedElems.entrySet()){ | ||
FieldType ft = FieldType.byName(element.getValue().getType()); | ||
e.set(element.getKey(), | ||
FieldType.byName(element.getValue().getType()), | ||
getObjectForType(ft, element.getValue().getValue())); | ||
} | ||
|
||
}catch(Exception ex){ | ||
ex.printStackTrace(); | ||
return null; | ||
} | ||
return e; | ||
} | ||
|
||
Map<String, TypeValue> parseTyped(JsonObject typedContainer){ | ||
Set<Entry<String, JsonElement>> types = typedContainer.entrySet(); | ||
Map<String, TypeValue> typedElements = new HashMap<String, TypeValue>(); | ||
for(Entry<String, JsonElement> type : types){ | ||
TypeValue tv = getTypeValue(type.getValue().getAsJsonObject()); | ||
if(tv != null) | ||
typedElements.put(type.getKey(), tv); | ||
} | ||
return typedElements; | ||
} | ||
|
||
TypeValue getTypeValue(JsonObject typedElement){ | ||
String type = typedElement.getAsJsonPrimitive("type").getAsString(); | ||
String value = typedElement.getAsJsonPrimitive("value").getAsString(); | ||
FieldType fType = FieldType.byName(type); | ||
if(fType == null) | ||
return null; | ||
return new TypeValue(fType.name, value); | ||
} | ||
|
||
Object getObjectForType(FieldType ft, String str) { | ||
//Giant switch statement dervied from Serializer.serializeValue's | ||
//type system definition | ||
switch (ft) { | ||
case BOOLEAN: | ||
return getObjectForType(str, Boolean.class); | ||
case BYTE: | ||
return getObjectForType(str, Byte.class); | ||
case DOUBLE: | ||
return getObjectForType(str, Double.class); | ||
case FLOAT: | ||
return getObjectForType(str, Float.class); | ||
case INT16: | ||
return getObjectForType(str, Short.class); | ||
case INT32: | ||
return getObjectForType(str, Integer.class); | ||
case INT64: | ||
return getObjectForType(str, Long.class); | ||
case IPADDR: | ||
return getObjectForType(str, IPAddress.class); | ||
case STRING: | ||
return getObjectForType(str, String.class); | ||
case UINT16: | ||
return getObjectForType(str, Integer.class); | ||
case UINT32: | ||
return getObjectForType(str, Long.class); | ||
case UINT64: | ||
return getObjectForType(str, BigInteger.class); | ||
case BOOLEAN_ARRAY: | ||
return getObjectForType(str, boolean[].class); | ||
case BYTE_ARRAY: | ||
return getObjectForType(str, byte[].class); | ||
case DOUBLE_ARRAY: | ||
return getObjectForType(str, double[].class); | ||
case FLOAT_ARRAY: | ||
return getObjectForType(str, float[].class); | ||
case INT16_ARRAY: | ||
return getObjectForType(str, short[].class); | ||
case INT32_ARRAY: | ||
return getObjectForType(str, int[].class); | ||
case INT64_ARRAY: | ||
return getObjectForType(str, long[].class); | ||
case IP_ADDR_ARRAY: | ||
return getObjectForType(str, IPAddress[].class); | ||
case STRING_ARRAY: | ||
return getObjectForType(str, String[].class); | ||
case UINT16_ARRAY: | ||
return getObjectForType(str, int[].class); | ||
case UINT32_ARRAY: | ||
return getObjectForType(str, long[].class); | ||
case UINT64_ARRAY: | ||
return getObjectForType(str, BigInteger[].class); | ||
case NBOOLEAN_ARRAY: | ||
return getObjectForType(str, Boolean[].class); | ||
case NBYTE_ARRAY: | ||
return getObjectForType(str, Byte[].class); | ||
case NDOUBLE_ARRAY: | ||
return getObjectForType(str, Double[].class); | ||
case NFLOAT_ARRAY: | ||
return getObjectForType(str, Float[].class); | ||
case NINT16_ARRAY: | ||
return getObjectForType(str, Short[].class); | ||
case NINT32_ARRAY: | ||
return getObjectForType(str, Integer[].class); | ||
case NINT64_ARRAY: | ||
return getObjectForType(str, Long[].class); | ||
case NSTRING_ARRAY: | ||
return getObjectForType(str, String[].class); | ||
case NUINT16_ARRAY: | ||
return getObjectForType(str, Integer[].class); | ||
case NUINT32_ARRAY: | ||
return getObjectForType(str, Long[].class); | ||
case NUINT64_ARRAY: | ||
return getObjectForType(str, BigInteger[].class); | ||
default: | ||
return str; | ||
} | ||
} | ||
|
||
Object getObjectForType(String str, Class clz){ | ||
return gson.fromJson(str, clz); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.