Search in sources :

Example 1 with UTF8_CURLY_BRACKET_RIGHT

use of com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT 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)1 JsonDocument (com.robo4j.socket.http.json.JsonDocument)1 JsonGenericTypeAdapter (com.robo4j.socket.http.json.JsonGenericTypeAdapter)1 JsonTypeAdapter (com.robo4j.socket.http.json.JsonTypeAdapter)1 WITHOUT_QUOTATION_TYPES (com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES)1 StringConstants (com.robo4j.util.StringConstants)1 UTF8_COLON (com.robo4j.util.Utf8Constant.UTF8_COLON)1 UTF8_COMMA (com.robo4j.util.Utf8Constant.UTF8_COMMA)1 UTF8_CURLY_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT)1 UTF8_CURLY_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT)1 UTF8_SQUARE_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT)1 UTF8_SQUARE_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT)1 Array (java.lang.reflect.Array)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1