Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nested case #90

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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