Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Fixed enum type error.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongHwan committed Sep 23, 2019
1 parent e040900 commit 20b9af0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +166,18 @@ public Object serialize(KsonContext ksonContext, Map<?, ?> value) {
}
});

this.registerTransformer(UUID.class, new Transformer<UUID>() {
@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) {
Expand Down Expand Up @@ -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<Enum>) type, originalValue.toString());


if (originalValue instanceof KsonObject) {
KsonObject wrappingObject = (KsonObject) originalValue;

Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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<? extends Object> originalValueType = originalValue.getClass();

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/realtimetech/kson/test/TestObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
public class TestObject {
private Test test;

private TestEnum enumType1;
private TestEnum enumType2;

@Ignore
private byte[] bytes;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -365,4 +371,24 @@ public HashMap<HashMap<String, String>, HashMap<String, String>> getMapMap() {
public HashMap<String, String> getStringMap() {
return stringMap;
}

public byte[] getBytes() {
return bytes;
}

public LinkedList<Double> getDoubleLinkedList() {
return doubleLinkedList;
}

public List<Double> getDoubleList() {
return doubleList;
}

public TestEnum getEnumType1() {
return enumType1;
}

public TestEnum getEnumType2() {
return enumType2;
}
}

0 comments on commit 20b9af0

Please sign in to comment.