@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")); }
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); }
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); } }
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); }  }
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); }
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);  }