From dc1488fda73ae6c8813485d4314b9458cebc5530 Mon Sep 17 00:00:00 2001 From: "junhao.chen" Date: Fri, 19 Feb 2021 15:31:27 +0800 Subject: [PATCH] fix nested case --- .../repository/parser/DgraphParser.java | 4 +- .../util/JSONFieldDeserializerUtil.java | 19 +- .../util/JSONFieldDeserializerUtilTest.java | 403 +++--------------- 3 files changed, 66 insertions(+), 360 deletions(-) diff --git a/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/repository/parser/DgraphParser.java b/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/repository/parser/DgraphParser.java index 75cab29f..619a13e9 100644 --- a/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/repository/parser/DgraphParser.java +++ b/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/repository/parser/DgraphParser.java @@ -75,7 +75,7 @@ public JSONObject parseDecoratorJSON(Object entity) { public List 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) @@ -92,7 +92,7 @@ public List extractJSONArray(JSONArray array) { public Optional 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)) diff --git a/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtil.java b/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtil.java index d42469a3..751ba800 100644 --- a/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtil.java +++ b/dgraph/src/main/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtil.java @@ -43,25 +43,34 @@ public static void changeJson(JSONObject json, Map map) { }); } - public static Map getJsonMap(List classes) { + public static Map getJsonMap(Class clazz){ + return getJsonMap(Arrays.asList(clazz),new ArrayList<>()); + } + + private static Map getJsonMap(List classes,List alreadyExistedClasses) { Map 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 innerJsonMap = getJsonMap(Collections.singletonList((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0])); + Map 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 innerJsonMap = getJsonMap(Collections.singletonList(field.getType())); + Map innerJsonMap = getJsonMap(Collections.singletonList(field.getType()),new ArrayList<>(alreadyExistedClasses)); if (!CollectionUtils.isEmpty(innerJsonMap.keySet())) { map.put(DgraphTypeUtil.getDgraphTypeValue(clazz) + "." + field.getName(), innerJsonMap); } @@ -69,7 +78,7 @@ public static Map getJsonMap(List classes) { } UnionClasses unionClasses = field.getAnnotation(UnionClasses.class); if (Objects.nonNull(unionClasses)) { - Map innerJsonMap = getJsonMap(Arrays.stream(unionClasses.value()).collect(Collectors.toList())); + Map 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); } diff --git a/dgraph/src/test/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtilTest.java b/dgraph/src/test/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtilTest.java index b5348395..07bf1e9e 100644 --- a/dgraph/src/test/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtilTest.java +++ b/dgraph/src/test/java/com/github/yituhealthcare/arc/dgraph/util/JSONFieldDeserializerUtilTest.java @@ -5,12 +5,10 @@ import com.alibaba.fastjson.JSONObject; import org.junit.Test; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; /** * @author junhao.chen @@ -24,79 +22,6 @@ private static class Task { private String id; private String name; private List questions; - - public Task(String id, String name, List questions) { - this.id = id; - this.name = name; - this.questions = questions; - } - - public Task() { - } - - public static TaskBuilder builder() { - return new TaskBuilder(); - } - - public String getId() { - return this.id; - } - - public String getName() { - return this.name; - } - - public List getQuestions() { - return this.questions; - } - - public void setId(String id) { - this.id = id; - } - - public void setName(String name) { - this.name = name; - } - - public void setQuestions(List questions) { - this.questions = questions; - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.Task(id=" + this.getId() + ", name=" + this.getName() + ", questions=" + this.getQuestions() + ")"; - } - - public static class TaskBuilder { - private String id; - private String name; - private List questions; - - TaskBuilder() { - } - - public Task.TaskBuilder id(String id) { - this.id = id; - return this; - } - - public Task.TaskBuilder name(String name) { - this.name = name; - return this; - } - - public Task.TaskBuilder questions(List questions) { - this.questions = questions; - return this; - } - - public Task build() { - return new Task(id, name, questions); - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.Task.TaskBuilder(id=" + this.id + ", name=" + this.name + ", questions=" + this.questions + ")"; - } - } } @DgraphType("QUESTION") @@ -105,64 +30,6 @@ private static class Question { private String id; @UnionClasses(ImageDefine.class) private Object define; - - public Question(String id, Object define) { - this.id = id; - this.define = define; - } - - public Question() { - } - - public static QuestionBuilder builder() { - return new QuestionBuilder(); - } - - public String getId() { - return this.id; - } - - public Object getDefine() { - return this.define; - } - - public void setId(String id) { - this.id = id; - } - - public void setDefine(Object define) { - this.define = define; - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.Question(id=" + this.getId() + ", define=" + this.getDefine() + ")"; - } - - public static class QuestionBuilder { - private String id; - private Object define; - - QuestionBuilder() { - } - - public Question.QuestionBuilder id(String id) { - this.id = id; - return this; - } - - public Question.QuestionBuilder define(Object define) { - this.define = define; - return this; - } - - public Question build() { - return new Question(id, define); - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.Question.QuestionBuilder(id=" + this.id + ", define=" + this.define + ")"; - } - } } @DgraphType("DEFINE") @@ -172,244 +39,27 @@ private static class ImageDefine { private SubjectForm subjectForm; private LocationForm locationForm; private JSONObject displayConfigs; - - public ImageDefine(String id, SubjectForm subjectForm, LocationForm locationForm, JSONObject displayConfigs) { - this.id = id; - this.subjectForm = subjectForm; - this.locationForm = locationForm; - this.displayConfigs = displayConfigs; - } - - public ImageDefine() { - } - - public static ImageDefineBuilder builder() { - return new ImageDefineBuilder(); - } - - public String getId() { - return this.id; - } - - public SubjectForm getSubjectForm() { - return this.subjectForm; - } - - public LocationForm getLocationForm() { - return this.locationForm; - } - - public JSONObject getDisplayConfigs() { - return this.displayConfigs; - } - - public void setId(String id) { - this.id = id; - } - - public void setSubjectForm(SubjectForm subjectForm) { - this.subjectForm = subjectForm; - } - - public void setLocationForm(LocationForm locationForm) { - this.locationForm = locationForm; - } - - public void setDisplayConfigs(JSONObject displayConfigs) { - this.displayConfigs = displayConfigs; - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.ImageDefine(id=" + this.getId() + ", subjectForm=" + this.getSubjectForm() + ", locationForm=" + this.getLocationForm() + ", displayConfigs=" + this.getDisplayConfigs() + ")"; - } - - public static class ImageDefineBuilder { - private String id; - private SubjectForm subjectForm; - private LocationForm locationForm; - private JSONObject displayConfigs; - - ImageDefineBuilder() { - } - - public ImageDefine.ImageDefineBuilder id(String id) { - this.id = id; - return this; - } - - public ImageDefine.ImageDefineBuilder subjectForm(SubjectForm subjectForm) { - this.subjectForm = subjectForm; - return this; - } - - public ImageDefine.ImageDefineBuilder locationForm(LocationForm locationForm) { - this.locationForm = locationForm; - return this; - } - - public ImageDefine.ImageDefineBuilder displayConfigs(JSONObject displayConfigs) { - this.displayConfigs = displayConfigs; - return this; - } - - public ImageDefine build() { - return new ImageDefine(id, subjectForm, locationForm, displayConfigs); - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.ImageDefine.ImageDefineBuilder(id=" + this.id + ", subjectForm=" + this.subjectForm + ", locationForm=" + this.locationForm + ", displayConfigs=" + this.displayConfigs + ")"; - } - } } @DgraphType("SUBJECTFORM") private static class SubjectForm { private List formItems; - - public SubjectForm(List formItems) { - this.formItems = formItems; - } - - public SubjectForm() { - } - - public static SubjectFormBuilder builder() { - return new SubjectFormBuilder(); - } - - public List getFormItems() { - return this.formItems; - } - - public void setFormItems(List formItems) { - this.formItems = formItems; - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.SubjectForm(formItems=" + this.getFormItems() + ")"; - } - - public static class SubjectFormBuilder { - private List formItems; - - SubjectFormBuilder() { - } - - public SubjectForm.SubjectFormBuilder formItems(List formItems) { - this.formItems = formItems; - return this; - } - - public SubjectForm build() { - return new SubjectForm(formItems); - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.SubjectForm.SubjectFormBuilder(formItems=" + this.formItems + ")"; - } - } } @DgraphType("FORM_ITEM_CONFIG") private static class FormItemConfig { private JSONObject configDetail; - - public FormItemConfig(JSONObject configDetail) { - this.configDetail = configDetail; - } - - public FormItemConfig() { - } - - public static FormItemConfigBuilder builder() { - return new FormItemConfigBuilder(); - } - - public JSONObject getConfigDetail() { - return this.configDetail; - } - - public void setConfigDetail(JSONObject configDetail) { - this.configDetail = configDetail; - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.FormItemConfig(configDetail=" + this.getConfigDetail() + ")"; - } - - public static class FormItemConfigBuilder { - private JSONObject configDetail; - - FormItemConfigBuilder() { - } - - public FormItemConfig.FormItemConfigBuilder configDetail(JSONObject configDetail) { - this.configDetail = configDetail; - return this; - } - - public FormItemConfig build() { - return new FormItemConfig(configDetail); - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.FormItemConfig.FormItemConfigBuilder(configDetail=" + this.configDetail + ")"; - } - } } @DgraphType("LOCATION_FORM") private static class LocationForm { private List formItems; - - public LocationForm(List formItems) { - this.formItems = formItems; - } - - public LocationForm() { - } - - public static LocationFormBuilder builder() { - return new LocationFormBuilder(); - } - - public List getFormItems() { - return this.formItems; - } - - public void setFormItems(List formItems) { - this.formItems = formItems; - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.LocationForm(formItems=" + this.getFormItems() + ")"; - } - - public static class LocationFormBuilder { - private List formItems; - - LocationFormBuilder() { - } - - public LocationForm.LocationFormBuilder formItems(List formItems) { - this.formItems = formItems; - return this; - } - - public LocationForm build() { - return new LocationForm(formItems); - } - - public String toString() { - return "JSONFieldDeserializerUtilTest.LocationForm.LocationFormBuilder(formItems=" + this.formItems + ")"; - } - } } @Test public void test_get_json_map() { Class clazz = Task.class; - Map map = JSONFieldDeserializerUtil.getJsonMap(Arrays.asList(clazz)); + Map map = JSONFieldDeserializerUtil.getJsonMap(clazz); assertTrue(map.containsKey("TASK.questions")); Map question = (Map) map.get("TASK.questions"); assertTrue(question.containsKey("QUESTION.define")); @@ -466,4 +116,51 @@ private Map generateMap4Test() { return task; } + @DgraphType("PARENT") + private static class ParentClass{ + private A a; + private D d; + } + + @DgraphType("A") + private static class A{ + private B b; + } + + @DgraphType("B") + private static class B{ + private C c; + } + + @DgraphType("C") + private static class C{ + private JSONObject json; + private A a; + } + + @DgraphType("D") + private static class D{ + private E e; + } + + @DgraphType("E") + private static class E{ + private F f; + } + + @DgraphType("F") + private static class F{ + private Map map; + private F f; + } + + @Test + public void test_get_nested_class_json_map(){ + Class clazz = ParentClass.class; + Map map = JSONFieldDeserializerUtil.getJsonMap(clazz); + JSONObject json = new JSONObject(map); + assertNull(json.getJSONObject("PARENT.d").getJSONObject("D.e").getJSONObject("E.f").getString("F.map")); + assertNull(json.getJSONObject("PARENT.a").getJSONObject("A.b").getJSONObject("B.c").getString("C.json")); + } + }