Search in sources :

Example 1 with UTF8_COMMA

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);
}
Also used : Array(java.lang.reflect.Array) StringConstants(com.robo4j.util.StringConstants) UTF8_COLON(com.robo4j.util.Utf8Constant.UTF8_COLON) HashMap(java.util.HashMap) UTF8_COMMA(com.robo4j.util.Utf8Constant.UTF8_COMMA) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonGenericTypeAdapter(com.robo4j.socket.http.json.JsonGenericTypeAdapter) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) Collection(java.util.Collection) WITHOUT_QUOTATION_TYPES(com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES) Set(java.util.Set) Collectors(java.util.stream.Collectors) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO)

Example 2 with UTF8_COMMA

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();
}
Also used : Array(java.lang.reflect.Array) StringConstants(com.robo4j.util.StringConstants) UTF8_COLON(com.robo4j.util.Utf8Constant.UTF8_COLON) HashMap(java.util.HashMap) UTF8_COMMA(com.robo4j.util.Utf8Constant.UTF8_COMMA) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonGenericTypeAdapter(com.robo4j.socket.http.json.JsonGenericTypeAdapter) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) Collection(java.util.Collection) WITHOUT_QUOTATION_TYPES(com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES) Set(java.util.Set) Collectors(java.util.stream.Collectors) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) LinkedList(java.util.LinkedList) List(java.util.List) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ClassGetSetDTO (com.robo4j.socket.http.dto.ClassGetSetDTO)2 JsonDocument (com.robo4j.socket.http.json.JsonDocument)2 JsonGenericTypeAdapter (com.robo4j.socket.http.json.JsonGenericTypeAdapter)2 JsonTypeAdapter (com.robo4j.socket.http.json.JsonTypeAdapter)2 WITHOUT_QUOTATION_TYPES (com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES)2 StringConstants (com.robo4j.util.StringConstants)2 UTF8_COLON (com.robo4j.util.Utf8Constant.UTF8_COLON)2 UTF8_COMMA (com.robo4j.util.Utf8Constant.UTF8_COMMA)2 UTF8_CURLY_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT)2 UTF8_CURLY_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT)2 UTF8_SQUARE_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT)2 UTF8_SQUARE_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT)2 Array (java.lang.reflect.Array)2 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2