diff --git a/src/main/java/com/realtimetech/kson/KsonContext.java b/src/main/java/com/realtimetech/kson/KsonContext.java index a1926b0..468062c 100644 --- a/src/main/java/com/realtimetech/kson/KsonContext.java +++ b/src/main/java/com/realtimetech/kson/KsonContext.java @@ -3,13 +3,12 @@ import java.io.IOException; import java.lang.reflect.Array; import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; -import java.util.List; import java.util.Map; +import java.util.UUID; import com.realtimetech.kson.annotation.Ignore; import com.realtimetech.kson.annotation.PrimaryKey; @@ -167,6 +166,18 @@ public Object serialize(KsonContext ksonContext, Map value) { } }); + this.registerTransformer(UUID.class, new Transformer() { + @Override + public Object serialize(KsonContext ksonContext, UUID value) { + return value.toString(); + } + + @Override + public UUID deserialize(KsonContext ksonContext, Class object, Object value) { + return UUID.fromString((String) value); + } + }); + } private Field[] getAccessibleFields(Class clazz) { @@ -312,9 +323,14 @@ public Object addToObjectStack(Class clazz, Object object) throws Deserialize return result; } + @SuppressWarnings("rawtypes") private Object createAtToObject(boolean first, Class type, Object originalValue) throws DeserializeException { Object primaryId = null; + if(type.isEnum()) + return Enum.valueOf((Class) type, originalValue.toString()); + + if (originalValue instanceof KsonObject) { KsonObject wrappingObject = (KsonObject) originalValue; @@ -419,7 +435,6 @@ public Object addFromObjectStack(Object object) throws SerializeException { if (targetKson instanceof KsonObject) { KsonObject ksonValue = (KsonObject) targetKson; - for (Field field : this.getAccessibleFields(targetObject.getClass())) { try { ksonValue.put(field.getName(), this.createAtFromObject(false, field.getType(), field.get(targetObject))); @@ -455,6 +470,9 @@ public Object addFromObjectStack(Object object) throws SerializeException { private Object createAtFromObject(boolean first, Class type, Object originalValue) throws SerializeException { if (originalValue == null) return null; + + if(originalValue.getClass().isEnum()) + return originalValue.toString(); Class originalValueType = originalValue.getClass(); diff --git a/src/main/java/com/realtimetech/kson/test/TestObject.java b/src/main/java/com/realtimetech/kson/test/TestObject.java index 7d3356d..5b732a9 100644 --- a/src/main/java/com/realtimetech/kson/test/TestObject.java +++ b/src/main/java/com/realtimetech/kson/test/TestObject.java @@ -15,6 +15,9 @@ public class TestObject { private Test test; + private TestEnum enumType1; + private TestEnum enumType2; + @Ignore private byte[] bytes; @@ -142,6 +145,9 @@ public TestObject(Test test) { this.testArray = new Test[] { test, test }; + this.enumType1 = TestEnum.TYPE_1; + this.enumType2 = TestEnum.TYPE_2; + this.intArray = new int[] { 1, 2, 3, 4 }; this.integer = 1; @@ -365,4 +371,24 @@ public HashMap, HashMap> getMapMap() { public HashMap getStringMap() { return stringMap; } + + public byte[] getBytes() { + return bytes; + } + + public LinkedList getDoubleLinkedList() { + return doubleLinkedList; + } + + public List getDoubleList() { + return doubleList; + } + + public TestEnum getEnumType1() { + return enumType1; + } + + public TestEnum getEnumType2() { + return enumType2; + } }