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

Commit 2ef9da3

Browse files
Merge pull request #31 from StaticDefault/master
Fixed many issues.
2 parents ea35d35 + b309989 commit 2ef9da3

File tree

8 files changed

+318
-47
lines changed

8 files changed

+318
-47
lines changed

src/main/java/com/realtimetech/kson/KsonContext.java

Lines changed: 106 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public Collection<?> deserialize(KsonContext ksonContext, Class<?> fieldType, Ob
173173

174174
for (Object object : (JsonArray) value) {
175175
try {
176-
collections.add(ksonContext.addToObjectStack(object.getClass(), object));
176+
collections.add(object == null ? null : ksonContext.addToObjectStack(object.getClass(), object));
177177
} catch (Exception e) {
178178
e.printStackTrace();
179179
}
@@ -194,6 +194,7 @@ public Object serialize(KsonContext ksonContext, Map<?, ?> value) {
194194
try {
195195
Object keyKson = ksonContext.addFromObjectStack(keyObject);
196196
Object valueKson = ksonContext.addFromObjectStack(valueObject);
197+
197198
jsonObject.put(keyKson, valueKson);
198199
} catch (SerializeException e) {
199200
e.printStackTrace();
@@ -219,7 +220,7 @@ public Object serialize(KsonContext ksonContext, Map<?, ?> value) {
219220
Object valueObject = jsonObject.get(keyObject);
220221

221222
try {
222-
map.put(ksonContext.addToObjectStack(keyObject.getClass(), keyObject), ksonContext.addToObjectStack(valueObject.getClass(), valueObject));
223+
map.put(keyObject == null ? null : ksonContext.addToObjectStack(keyObject.getClass(), keyObject), valueObject == null ? null : ksonContext.addToObjectStack(valueObject.getClass(), valueObject));
223224
} catch (Exception e) {
224225
e.printStackTrace();
225226
}
@@ -242,16 +243,60 @@ public UUID deserialize(KsonContext ksonContext, Class<?> object, Object value)
242243
});
243244
}
244245

246+
private static final String PRIMITIVE_INT_ARRAY_NAME = int[].class.getName();
247+
private static final String PRIMITIVE_FLOAT_ARRAY_NAME = float[].class.getName();
248+
private static final String PRIMITIVE_BOOLEAN_ARRAY_NAME = boolean[].class.getName();
249+
private static final String PRIMITIVE_CHAR_ARRAY_NAME = char[].class.getName();
250+
private static final String PRIMITIVE_DOUBLE_ARRAY_NAME = double[].class.getName();
251+
private static final String PRIMITIVE_LONG_ARRAY_NAME = long[].class.getName();
252+
private static final String PRIMITIVE_SHORT_ARRAY_NAME = short[].class.getName();
253+
private static final String PRIMITIVE_BYTE_ARRAY_NAME = byte[].class.getName();
254+
245255
private Class<?> getClassByName(String name) throws ClassNotFoundException {
246-
if (!this.cachedClasses.containsKey(name)) {
247-
this.cachedClasses.put(name, classLoader.loadClass(name));
256+
if (name.length() == 2) {
257+
if (PRIMITIVE_INT_ARRAY_NAME.equals(name)) {
258+
return int[].class;
259+
} else if (PRIMITIVE_FLOAT_ARRAY_NAME.equals(name)) {
260+
return float[].class;
261+
} else if (PRIMITIVE_BOOLEAN_ARRAY_NAME.equals(name)) {
262+
return boolean[].class;
263+
} else if (PRIMITIVE_CHAR_ARRAY_NAME.equals(name)) {
264+
return char[].class;
265+
} else if (PRIMITIVE_DOUBLE_ARRAY_NAME.equals(name)) {
266+
return double[].class;
267+
} else if (PRIMITIVE_LONG_ARRAY_NAME.equals(name)) {
268+
return long[].class;
269+
} else if (PRIMITIVE_SHORT_ARRAY_NAME.equals(name)) {
270+
return short[].class;
271+
} else if (PRIMITIVE_BYTE_ARRAY_NAME.equals(name)) {
272+
return byte[].class;
273+
}
248274
}
249275

250-
return this.cachedClasses.get(name);
276+
Class<?> target = this.cachedClasses.get(name);
277+
278+
if (target == null) {
279+
Class<?> loadClass = classLoader.loadClass(name);
280+
if (loadClass == null) {
281+
loadClass = Object.class;
282+
}
283+
284+
target = loadClass;
285+
this.cachedClasses.put(name, loadClass);
286+
}
287+
288+
return target;
251289
}
252290

253291
private Field[] getAccessibleFields(Class<?> clazz) {
254292
if (!this.cachedFields.containsKey(clazz)) {
293+
if (clazz == Integer.class) {
294+
try {
295+
throw new NullPointerException();
296+
} catch (Exception e) {
297+
e.printStackTrace();
298+
}
299+
}
255300
LinkedList<Field> fields = new LinkedList<Field>();
256301

257302
if (clazz != null) {
@@ -323,7 +368,7 @@ public Transformer<?> getTransformer(Class<?> type) {
323368
return this.transformers.get(type);
324369
}
325370

326-
public Field getPrimaryKeyField(Class<?> type) {
371+
private Field getPrimaryKeyField(Class<?> type) {
327372
if (!this.primaryKeys.containsKey(type)) {
328373
boolean matched = false;
329374

@@ -383,41 +428,41 @@ private Object castToType(Class<?> type, Object object) {
383428
}
384429
}
385430

386-
public <T> T toObject(Class<T> clazz, Object object) throws DeserializeException {
387-
if (object instanceof String) {
388-
try {
389-
object = this.fromString((String) object);
390-
} catch (IOException e) {
391-
392-
}
393-
}
394-
395-
if (object instanceof JsonValue) {
396-
return (T) this.addToObjectStack(clazz, (JsonValue) object);
397-
}
431+
public <T> T toObject(Class<T> clazz, String string) throws DeserializeException, IOException {
432+
return (T) this.toObject(clazz, this.fromString((String) string));
433+
}
398434

399-
return (T) object;
435+
public <T> T toObject(Class<T> clazz, JsonValue object) throws DeserializeException {
436+
return (T) this.addToObjectStack(clazz, (JsonValue) object);
400437
}
401438

439+
@Deprecated
402440
public boolean addPrimaryObject(Object object) {
441+
if (object == null) {
442+
return false;
443+
}
444+
403445
Field primaryKeyField = getPrimaryKeyField(object.getClass());
404446

405447
if (primaryKeyField != null) {
406448
try {
407-
Object object2 = primaryKeyField.get(object);
449+
Object objectBefore = primaryKeyField.get(object);
450+
408451
if (!this.primaryObjects.containsKey(object.getClass())) {
409452
this.primaryObjects.put(object.getClass(), new HashMap<Object, Object>());
410453
}
411454

412-
this.primaryObjects.get(object.getClass()).put(object2, object);
455+
this.primaryObjects.get(object.getClass()).put(objectBefore, object);
456+
457+
return true;
413458
} catch (IllegalArgumentException | IllegalAccessException e) {
414459
e.printStackTrace();
415460

416461
return false;
417462
}
418463
}
419464

420-
return true;
465+
return false;
421466
}
422467

423468
public Object addToObjectStack(Object object) throws DeserializeException {
@@ -490,6 +535,7 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Deserialize
490535
@SuppressWarnings("rawtypes")
491536
private Object createAtToObject(boolean first, Class<?> type, Object originalValue) throws DeserializeException {
492537
Object primaryId = null;
538+
Field primaryKeyField = null;
493539

494540
if (originalValue == null)
495541
return null;
@@ -507,23 +553,25 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
507553
}
508554
}
509555

556+
if (type.isEnum()) {
557+
return Enum.valueOf((Class<Enum>) type, originalValue.toString());
558+
}
559+
510560
if (originalValue instanceof JsonObject) {
511561
JsonObject wrappingObject = (JsonObject) originalValue;
512562

513563
if (wrappingObject.containsKey("@id")) {
564+
primaryKeyField = getPrimaryKeyField(type);
514565
primaryId = wrappingObject.get("@id");
515566
} else if (first) {
516-
Field primaryKeyField = getPrimaryKeyField(type);
567+
primaryKeyField = getPrimaryKeyField(type);
517568

518569
if (primaryKeyField != null) {
519570
primaryId = wrappingObject.get(primaryKeyField.getName());
520571
}
521572
}
522573
}
523574

524-
if (type.isEnum())
525-
return Enum.valueOf((Class<Enum>) type, originalValue.toString());
526-
527575
if (primaryId == null) {
528576
Transformer<Object> transformer = (Transformer<Object>) this.getTransformer(type);
529577

@@ -565,7 +613,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
565613

566614
if (!hashMap.containsKey(primaryId)) {
567615
try {
568-
hashMap.put(primaryId, UnsafeAllocator.newInstance(type));
616+
Object newInstance = UnsafeAllocator.newInstance(type);
617+
hashMap.put(primaryId, newInstance);
618+
if (primaryKeyField != null) {
619+
primaryKeyField.set(newInstance, primaryId);
620+
}
569621
} catch (Exception e) {
570622
e.printStackTrace();
571623
throw new DeserializeException("Deserialize failed because can't allocation primary object.");
@@ -606,7 +658,17 @@ public JsonValue fromObject(Object object) throws SerializeException {
606658
return null;
607659
}
608660

609-
return (JsonValue) addFromObjectStack(object);
661+
Object convertedObject = addFromObjectStack(object);
662+
663+
if (convertedObject == null) {
664+
return null;
665+
}
666+
667+
if (convertedObject instanceof JsonValue) {
668+
return (JsonValue) convertedObject;
669+
}
670+
671+
throw new SerializeException("Can't serialize to json value!");
610672
} else {
611673
throw new SerializeException("This context already running serialize!");
612674
}
@@ -674,25 +736,31 @@ private Object createAtFromObject(boolean first, Class<?> type, Object originalV
674736
originalValue = transformer.serialize(this, originalValue);
675737
}
676738

739+
boolean needSerialize = this.isNeedSerialize(originalValue.getClass());
740+
677741
if (!first && this.useCustomTag) {
678-
Field primaryKeyField = getPrimaryKeyField(originalValueType);
742+
if (needSerialize) {
743+
Field primaryKeyField = getPrimaryKeyField(originalValueType);
679744

680-
if (primaryKeyField != null) {
681-
JsonObject wrappingObject = new JsonObject();
745+
if (primaryKeyField != null) {
746+
JsonObject wrappingObject = new JsonObject();
682747

683-
try {
684-
wrappingObject.put("@id", primaryKeyField.get(originalValue));
685-
} catch (IllegalArgumentException | IllegalAccessException e) {
686-
throw new SerializeException("Serialize failed because primary key can't get from field.");
687-
}
748+
try {
749+
wrappingObject.put("@id", primaryKeyField.get(originalValue));
750+
} catch (IllegalArgumentException | IllegalAccessException e) {
751+
throw new SerializeException("Serialize failed because primary key can't get from field.");
752+
}
688753

689-
originalValue = wrappingObject;
754+
originalValue = wrappingObject;
755+
756+
needSerialize = false;
757+
}
690758
}
691759
}
692760

693761
Object convertedKsonValue = originalValue;
694762

695-
if (this.isNeedSerialize(convertedKsonValue.getClass())) {
763+
if (needSerialize) {
696764
if (originalValue.getClass().isArray()) {
697765
convertedKsonValue = new JsonArray();
698766
} else {

src/main/java/com/realtimetech/kson/element/JsonArray.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44

5+
import com.realtimetech.kson.annotation.Ignore;
56
import com.realtimetech.kson.writer.KsonWriter;
67

78
public class JsonArray extends ArrayList<Object> implements JsonValue {
@@ -11,6 +12,7 @@ public class JsonArray extends ArrayList<Object> implements JsonValue {
1112
*/
1213
private static final long serialVersionUID = 5513748119461105760L;
1314

15+
@Ignore
1416
protected KsonWriter ksonWriter = null;
1517

1618
@Override
@@ -27,4 +29,27 @@ public String toString(boolean useKsonStandard) {
2729
public String toString() {
2830
return toKsonString();
2931
}
32+
33+
@Ignore
34+
private int unique = RANDOM.nextInt();
35+
36+
@Override
37+
public int unique() {
38+
return unique;
39+
}
40+
41+
@Override
42+
public void unique(int unique) {
43+
this.unique = unique;
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return unique;
49+
}
50+
51+
@Override
52+
public int actualHash() {
53+
return super.hashCode();
54+
}
3055
}

0 commit comments

Comments
 (0)