use of com.robo4j.util.Utf8Constant.UTF8_COMMA in project robo4j by Robo4J.
the class ReflectUtils method createJson.
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);
}
use of com.robo4j.util.Utf8Constant.UTF8_COMMA in project robo4j by Robo4J.
the class ReflectUtils method processCollectionToJson.
@SuppressWarnings("unchecked")
private static String processCollectionToJson(ClassGetSetDTO getterDTO, TypeMapper typeMapper, Object obj) {
JsonElementStringBuilder result = JsonElementStringBuilder.Builder();
JsonTypeAdapter jsonTypeAdapter = getAdapterByClazz(getterDTO.getValueClass(), typeMapper);
switch(getterDTO.getCollection()) {
case ARRAY:
Object[] arrayObjects = (Object[]) obj;
String arrayValue = Stream.of(arrayObjects).map(element -> jsonTypeAdapter.adapt(element)).collect(Collectors.joining(UTF8_COMMA));
// formatter:off
result.add(UTF8_SQUARE_BRACKET_LEFT).add(arrayValue).add(UTF8_SQUARE_BRACKET_RIGHT);
break;
case LIST:
List<Object> objects = (List<Object>) obj;
String listValue = objects.stream().map(element -> jsonTypeAdapter.adapt(element)).collect(Collectors.joining(UTF8_COMMA));
// formatter:off
result.add(UTF8_SQUARE_BRACKET_LEFT).add(listValue).add(UTF8_SQUARE_BRACKET_RIGHT);
// formatter:on
break;
case MAP:
Map<Object, Object> objectMap = (Map<Object, Object>) obj;
result.add(UTF8_CURLY_BRACKET_LEFT).add(objectMap.entrySet().stream().map(entry -> JsonElementStringBuilder.Builder().addQuotationWithDelimiter(UTF8_COLON, entry.getKey()).add(jsonTypeAdapter.adapt(entry.getValue())).build()).collect(Collectors.joining(UTF8_COMMA))).add(UTF8_CURLY_BRACKET_RIGHT);
break;
}
return result.build();
}
Aggregations