Skip to content

Commit

Permalink
Merge pull request #90 from B-Five/master
Browse files Browse the repository at this point in the history
fix nested case
  • Loading branch information
wangyuheng authored Mar 17, 2021
2 parents 4f5fd6f + 321b426 commit aae6c01
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public JSONObject parseDecoratorJSON(Object entity) {
public <T> List<T> extractJSONArray(JSONArray array) {
return array.stream()
.map(it -> (JSONObject) it)
.peek(it -> JSONFieldDeserializerUtil.changeJson(it,JSONFieldDeserializerUtil.getJsonMap(Collections.singletonList(domainClass))))
.peek(it -> JSONFieldDeserializerUtil.changeJson(it,JSONFieldDeserializerUtil.getJsonMap(domainClass)))
.map(jsonObject -> JSON.toJSONString(jsonObject, postFilter))
.map(jsonStr -> (T) JSON.parseObject(jsonStr, domainClass, new JSONObjectDeserializer()))
.peek(this::unionClassHandle)
Expand All @@ -92,7 +92,7 @@ public <T> List<T> extractJSONArray(JSONArray array) {
public <T> Optional<T> extractJSON(JSONObject jsonObject) {
return Optional.ofNullable(jsonObject)
.map(it -> {
JSONFieldDeserializerUtil.changeJson(it,JSONFieldDeserializerUtil.getJsonMap(Collections.singletonList(domainClass)));
JSONFieldDeserializerUtil.changeJson(it,JSONFieldDeserializerUtil.getJsonMap(domainClass));
return it;
})
.map(it -> JSON.toJSONString(it, postFilter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,42 @@ public static void changeJson(JSONObject json, Map<String, Object> map) {
});
}

public static Map<String, Object> getJsonMap(List<Class> classes) {
public static Map<String,Object> getJsonMap(Class clazz){
return getJsonMap(Arrays.asList(clazz),new ArrayList<>());
}

private static Map<String, Object> getJsonMap(List<Class> classes,List<Class> alreadyExistedClasses) {
Map<String, Object> map = new HashMap<>();
for (Class clazz : classes) {
if (alreadyExistedClasses.contains(clazz)){
continue;
}
alreadyExistedClasses.add(clazz);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (Objects.equals(field.getType(), JSONObject.class)) {
if (Objects.equals(field.getType(), JSONObject.class) || Objects.equals(field.getType(),Map.class)) {
map.put(DgraphTypeUtil.getDgraphTypeValue(clazz) + "." + field.getName(), null);
continue;
}
if (!BASIC_CLASS_EXCEPT_JSON.contains(field.getType()) && !field.isEnumConstant()) {
if (Objects.equals(field.getType(), List.class)) {
Class listInnerClazz = (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (!BASIC_CLASS_EXCEPT_JSON.contains(listInnerClazz)) {
Map<String, Object> innerJsonMap = getJsonMap(Collections.singletonList((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]));
Map<String, Object> innerJsonMap = getJsonMap(Collections.singletonList((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]),new ArrayList<>(alreadyExistedClasses));
if (!CollectionUtils.isEmpty(innerJsonMap.keySet())) {
map.put(DgraphTypeUtil.getDgraphTypeValue(clazz) + "." + field.getName(), innerJsonMap);
}
}
} else {
Map<String, Object> innerJsonMap = getJsonMap(Collections.singletonList(field.getType()));
Map<String, Object> innerJsonMap = getJsonMap(Collections.singletonList(field.getType()),new ArrayList<>(alreadyExistedClasses));
if (!CollectionUtils.isEmpty(innerJsonMap.keySet())) {
map.put(DgraphTypeUtil.getDgraphTypeValue(clazz) + "." + field.getName(), innerJsonMap);
}
}
}
UnionClasses unionClasses = field.getAnnotation(UnionClasses.class);
if (Objects.nonNull(unionClasses)) {
Map<String, Object> innerJsonMap = getJsonMap(Arrays.stream(unionClasses.value()).collect(Collectors.toList()));
Map<String, Object> innerJsonMap = getJsonMap(Arrays.stream(unionClasses.value()).collect(Collectors.toList()),new ArrayList<>(alreadyExistedClasses));
if (!CollectionUtils.isEmpty(innerJsonMap.keySet())) {
map.put(DgraphTypeUtil.getDgraphTypeValue(clazz) + "." + field.getName(), innerJsonMap);
}
Expand Down
Loading

0 comments on commit aae6c01

Please sign in to comment.