ReflectUtils { public static String createJson(Object obj) { final Map<String, ClassGetSetDTO> map = getFieldsTypeMap(obj.getClass()); String preKey = map.keySet().isEmpty() ? null : map.keySet().iterator().next(); if(map.size() == 1 && preKey != null && preKey.equals(StringConstants.EMPTY)){ ClassGetSetDTO descriptor = map.values().iterator().next(); switch (descriptor.getCollection()){ case MAP: return new StringBuilder() .append(map.entrySet().stream() .map(entry -> new MapEntryDTO(entry.getKey(), ReflectUtils.getJsonValue(entry.getValue(), obj))) .filter(entry -> Objects.nonNull(entry.getValue())) .map(entry -> JsonElementStringBuilder.Builder() .add(entry.getValue()) .build()) .collect(Collectors.joining(UTF8_COMMA))) .toString(); case ARRAY: case LIST: return new StringBuilder() .append(map.entrySet().stream() .map(entry -> new MapEntryDTO(entry.getKey(), ReflectUtils.getJsonValue(entry.getValue(), obj))) .filter(entry -> Objects.nonNull(entry.getValue())) .map(entry -> JsonElementStringBuilder.Builder() .add(entry.getValue()) .build()) .collect(Collectors.joining(UTF8_COMMA))) .toString(); } } return createJson(map, obj); }  @SuppressWarnings("unchecked") static T createInstanceByClazzAndDescriptorAndJsonDocument(Class<T> clazz, JsonDocument jsonDocument); static String createJson(Object obj); static String createJson(Map<String, ClassGetSetDTO> descriptorMap, Object obj); static Map<String, ClassGetSetDTO> getFieldsTypeMap(Class<?> clazz); static JsonTypeAdapter getJsonTypeAdapter(Class<T> clazz);  }
@Test void createJsonByNumberMapCollection() { String expectedString = "{\"one\":1,\"two\":2}"; Map<String, Integer> map = new HashMap<>(); map.put("one", 1); map.put("two", 2); String result = ReflectUtils.createJson(map); System.out.println("result: " + result); assertEquals(expectedString, result); }
@Test void createJsonByObjectMapCollection(){ Throwable exception = assertThrows(RoboReflectException.class, () -> { Map<String, PathAttributeDTO> map = new HashMap<>(); map.put("one", new PathAttributeDTO("one1", "1")); map.put("two", new PathAttributeDTO("two2", "2")); ReflectUtils.createJson(map); }); assertTrue(exception.getMessage().startsWith("object getter value")); }
@Test void objectWithEnumToJson() { final String expectedJson = "{\"command\":\"MOVE\",\"desc\":\"some description\"}"; TestCommand command = new TestCommand(); command.setCommand(TestCommandEnum.MOVE); command.setDesc("some description"); final String result = ReflectUtils.createJson(command); System.out.println("result: " + result); assertNotNull(result); assertEquals(expectedJson, result); }
@Test void objectWithEnumListToJson() { final String expectedJson = "{\"commands\":[\"MOVE\",\"STOP\",\"BACK\"],\"desc\":\"commands description\"}"; TestCommandList commands = new TestCommandList(); commands.setCommands(Arrays.asList(TestCommandEnum.MOVE, TestCommandEnum.STOP, TestCommandEnum.BACK)); commands.setDesc("commands description"); final String result = ReflectUtils.createJson(commands); System.out.println("result: " + result); assertNotNull(result); assertEquals(expectedJson, result); }
@Test void pathAttributesListToJsonTest() { final String expectedJson = "{\"attributes\":[{\"name\":\"name\",\"value\":\"java.lang.String\"},{\"name\":\"values\",\"value\":\"java.util.HashMap\"}]}"; PathAttributeListDTO attributes = new PathAttributeListDTO(); attributes.addAttribute(new PathAttributeDTO("name", "java.lang.String")); attributes.addAttribute(new PathAttributeDTO("values", "java.util.HashMap")); String result = ReflectUtils.createJson(attributes); System.out.println("result:" + result); assertEquals(expectedJson, result); }
@Test void serverAttributesResponse(){ ResponseAttributeListDTO tmpAttr = new ResponseAttributeListDTO(); tmpAttr.setType("Type"); tmpAttr.setId("ID"); tmpAttr.addValue(new HttpPathMethodDTO("testUnit", HttpMethod.GET, Collections.singletonList("test"))); String result = ReflectUtils.createJson(tmpAttr); System.out.println("result:" + result); }
@Test void complexObjectToJsonTest(){ String expectedJson = "{\"name\":\"object\",\"value\":42,\"textList\":[\"one\",\"two\"],\"dictionary\":{\"one\":\"one1\",\"two\":\"two2\"},\"attributes\":{\"one\":{\"name\":\"name\",\"value\":\"test name\"},\"two\":{\"name\":\"value\",\"value\":\"42\"}}}"; TestListMapValues obj = new TestListMapValues(); obj.setName("object"); obj.setValue(42); obj.setTextList(Arrays.asList("one", "two")); Map<String, String> textMap = new HashMap<>(); textMap.put("one", "one1"); textMap.put("two", "two2"); obj.setDictionary(textMap); Map<String, PathAttributeDTO> attributes = new HashMap<>(); attributes.put("one", new PathAttributeDTO("name", "test name")); attributes.put("two", new PathAttributeDTO("value", "42")); obj.setAttributes(attributes); String result = ReflectUtils.createJson(obj); System.out.println("result:"+result); assertEquals(expectedJson, result); }
@Test void createJsonByStringListCollectionTest() { String expectedJson = "[\"One\",\"Two\"]"; List<String> list = new ArrayList<>(); list.add("One"); list.add("Two"); String result = ReflectUtils.createJson(list); System.out.println("result: " + result); assertEquals(expectedJson, result); }
@Test void createJsonByNumberListCollectionTest() { String expectedJson = "[1,2]"; List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); String result = ReflectUtils.createJson(list); System.out.println("result: " + result); assertEquals(expectedJson, result); }
@Test void createJsonByObjectListCollectionTest(){ Throwable exception = assertThrows(RoboReflectException.class, () -> { List<PathAttributeDTO> list = new ArrayList<>(); list.add(new PathAttributeDTO("one", "1")); list.add(new PathAttributeDTO("two", "2")); ReflectUtils.createJson(list); }); assertTrue(exception.getMessage().startsWith("object getter value")); }
@Test void createJsonByStringMapCollection() { String expectedString = "{\"one\":\"1\",\"two\":\"2\"}"; Map<String, String> map = new HashMap<>(); map.put("one", "1"); map.put("two", "2"); String result = ReflectUtils.createJson(map); System.out.println("result: " + result); assertEquals(expectedString, result); }