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

Commit

Permalink
Merge pull request #26 from StaticDefault/master
Browse files Browse the repository at this point in the history
Added unsolved objects getter.
  • Loading branch information
StaticDefaultTester2 authored Nov 28, 2019
2 parents a7695c0 + 36043cb commit de48335
Showing 1 changed file with 77 additions and 9 deletions.
86 changes: 77 additions & 9 deletions src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ private static enum ValueMode {
private HashMap<Class<?>, Field> primaryKeys;

private HashMap<Class<?>, HashMap<Object, Object>> primaryObjects;
private HashMap<Class<?>, HashMap<Object, Object>> primaryUnsolvedObjects;

private HashMap<Class<?>, Field[]> cachedFields;

private HashMap<String, Class<?>> cachedClasses;

private ClassLoader classLoader;

public KsonContext() {
this(10, 100);
}

public KsonContext(int stackSize, int stringBufferSize) {
this(KsonContext.class.getClassLoader(), stackSize, stringBufferSize);
}
Expand All @@ -84,6 +85,7 @@ public KsonContext(ClassLoader classLoader, int stackSize, int stringBufferSize)

this.primaryKeys = new HashMap<Class<?>, Field>();
this.primaryObjects = new HashMap<Class<?>, HashMap<Object, Object>>();
this.primaryUnsolvedObjects = new HashMap<Class<?>, HashMap<Object, Object>>();

this.cachedFields = new HashMap<Class<?>, Field[]>();

Expand Down Expand Up @@ -113,6 +115,23 @@ public File deserialize(KsonContext ksonContext, Class<?> object, Object value)
}
});

this.registeredTransformers.put(Class.class, new Transformer<Class<?>>() {
@Override
public Object serialize(KsonContext ksonContext, Class<?> value) {
return value.getName();
}

@Override
public Class<?> deserialize(KsonContext ksonContext, Class<?> object, Object value) {
try {
return classLoader.loadClass(value.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
});

this.registeredTransformers.put(Date.class, new Transformer<Date>() {
@Override
public Object serialize(KsonContext ksonContext, Date value) {
Expand Down Expand Up @@ -324,6 +343,10 @@ public Field getPrimaryKeyField(Class<?> type) {
return this.primaryKeys.get(type);
}

public HashMap<Class<?>, HashMap<Object, Object>> getPrimaryUnsolvedObjects() {
return primaryUnsolvedObjects;
}

private Object castToType(Class<?> type, Object object) {
if (object instanceof Number) {
Number number = (Number) object;
Expand Down Expand Up @@ -376,15 +399,38 @@ public <T> T toObject(Class<T> clazz, Object object) throws DeserializeException
return (T) object;
}

public boolean addPrimaryObject(Object object) {
Field primaryKeyField = getPrimaryKeyField(object.getClass());

if (primaryKeyField != null) {
try {
Object object2 = primaryKeyField.get(object);
if (!this.primaryObjects.containsKey(object.getClass())) {
this.primaryObjects.put(object.getClass(), new HashMap<Object, Object>());
}

this.primaryObjects.get(object.getClass()).put(object2, object);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();

return false;
}
}

return true;
}

public Object addToObjectStack(Object object) throws DeserializeException {
return this.addToObjectStack(Object.class, object);
}

public Object addToObjectStack(Class<?> clazz, Object object) throws DeserializeException {
Object result = this.createAtToObject(true, clazz, object);
Object result = null;

if (!this.working) {
this.working = true;
result = this.createAtToObject(true, clazz, object);

while (!this.objectStack.isEmpty()) {
Object targetObject = this.objectStack.pop();
JsonValue targetKson = this.jsonStack.pop();
Expand All @@ -398,7 +444,7 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Deserialize
try {
Object createAtToObject = createAtToObject(false, field.getType(), jsonValue.get(field.getName()));

if(createAtToObject != null) {
if (createAtToObject != null) {
if (createAtToObject.getClass() != field.getType()) {
createAtToObject = castToType(field.getType(), createAtToObject);
}
Expand Down Expand Up @@ -434,6 +480,8 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Deserialize
this.working = false;
this.objectStack.reset();
this.jsonStack.reset();
} else {
result = this.createAtToObject(false, clazz, object);
}

return result;
Expand All @@ -456,7 +504,13 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
throw new DeserializeException("Deserialize failed because can't find target class.");
}
originalValue = wrappingObject.get("#data");
} else if (wrappingObject.containsKey("@id")) {
}
}

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

if (wrappingObject.containsKey("@id")) {
primaryId = wrappingObject.get("@id");
} else if (first) {
Field primaryKeyField = getPrimaryKeyField(type);
Expand Down Expand Up @@ -513,8 +567,23 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
try {
hashMap.put(primaryId, UnsafeAllocator.newInstance(type));
} catch (Exception e) {
e.printStackTrace();
throw new DeserializeException("Deserialize failed because can't allocation primary object.");
}

if (!first) {
if (!this.primaryUnsolvedObjects.containsKey(type)) {
this.primaryUnsolvedObjects.put(type, new HashMap<Object, Object>());
}

this.primaryUnsolvedObjects.get(type).put(primaryId, hashMap.get(primaryId));
}
} else {
if (first) {
if (this.primaryUnsolvedObjects.containsKey(type)) {
this.primaryUnsolvedObjects.get(type).remove(primaryId);
}
}
}

convertedValue = hashMap.get(primaryId);
Expand All @@ -533,10 +602,10 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal

public JsonValue fromObject(Object object) throws SerializeException {
if (!this.working) {
if(object == null) {
if (object == null) {
return null;
}

return (JsonValue) addFromObjectStack(object);
} else {
throw new SerializeException("This context already running serialize!");
Expand Down Expand Up @@ -583,7 +652,7 @@ public Object addFromObjectStack(Object object) throws SerializeException {
this.jsonStack.reset();
this.working = false;
} else {
result = this.createAtFromObject(true, Object.class, object);
result = this.createAtFromObject(false, Object.class, object);
}

return result;
Expand Down Expand Up @@ -634,7 +703,6 @@ private Object createAtFromObject(boolean first, Class<?> type, Object originalV
this.jsonStack.push((JsonValue) convertedKsonValue);
}


if (this.isNeedSerialize(originalValueType) && type != originalValueType && this.useCustomTag) {
JsonObject wrappingObject = new JsonObject();

Expand Down

0 comments on commit de48335

Please sign in to comment.