Search in sources :

Example 1 with JsonDocument

use of com.robo4j.socket.http.json.JsonDocument in project robo4j by Robo4J.

the class AbstractHttpMessageCodec method decode.

@Override
public T decode(String json) {
    JsonReader jsonReader = new JsonReader(json);
    JsonDocument document = jsonReader.read();
    return ReflectUtils.createInstanceByClazzAndDescriptorAndJsonDocument(clazz, document);
}
Also used : JsonReader(com.robo4j.socket.http.json.JsonReader) JsonDocument(com.robo4j.socket.http.json.JsonDocument)

Example 2 with JsonDocument

use of com.robo4j.socket.http.json.JsonDocument in project robo4j by Robo4J.

the class ReflectUtils method adjustRoboJsonDocumentCast.

private static Object adjustRoboJsonDocumentCast(ClassGetSetDTO classGetSetDTO, JsonDocument document, String key) {
    if (classGetSetDTO.getCollection() != null) {
        switch(classGetSetDTO.getCollection()) {
            case ARRAY:
                Class<?> arrayClass = classGetSetDTO.getValueClass();
                List<?> list = ((JsonDocument) document.getKey(key)).getArray().stream().map(arrayClass::cast).collect(Collectors.toCollection(LinkedList::new));
                return listToArray(list);
            case LIST:
                Class<?> listClass = classGetSetDTO.getValueClass();
                // @formatter:off
                return ((JsonDocument) document.getKey(key)).getArray().stream().filter(Objects::nonNull).map(e -> adjustRoboClassCast(listClass, e)).collect(Collectors.toList());
            // @formatter:on
            case MAP:
                JsonDocument mapDocument = (JsonDocument) document.getKey(key);
                Map<String, Object> documentMap = mapDocument.getMap();
                if (documentMap.isEmpty()) {
                    return documentMap;
                } else {
                    // @formatter:off
                    return documentMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
                        if (e.getValue() != null && e.getValue() instanceof JsonDocument) {
                            Class<?> mapValueClazz = classGetSetDTO.getValueClass();
                            return adjustRoboClassCast(mapValueClazz, e.getValue());
                        }
                        return e.getValue();
                    }));
                // @formatter:on
                }
            default:
                throw new RoboReflectException("wrong collection: " + classGetSetDTO);
        }
    } else {
        return adjustRoboClassCast(classGetSetDTO.getValueClass(), document.getKey(key));
    }
}
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) Objects(java.util.Objects) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonDocument(com.robo4j.socket.http.json.JsonDocument)

Example 3 with JsonDocument

use of com.robo4j.socket.http.json.JsonDocument in project robo4j by Robo4J.

the class ReflectUtils method createInstanceByClazzAndDescriptorAndJsonDocument.

@SuppressWarnings("unchecked")
public static <T> T createInstanceByClazzAndDescriptorAndJsonDocument(Class<T> clazz, JsonDocument jsonDocument) {
    try {
        Object instance = clazz.getDeclaredConstructor().newInstance();
        getFieldsTypeMap(clazz).entrySet().stream().filter(e -> Objects.nonNull(jsonDocument.getKey(e.getKey()))).forEach(e -> {
            try {
                ClassGetSetDTO value = e.getValue();
                if (value.getValueClass().isEnum()) {
                    if (e.getValue().getCollection() != null) {
                        switch(e.getValue().getCollection()) {
                            case LIST:
                                List<Enum<?>> enumList = ((JsonDocument) jsonDocument.getKey(e.getKey())).getArray().stream().map(el -> extractEnumConstant(el.toString(), (Enum<?>[]) value.getValueClass().getEnumConstants())).collect(Collectors.toCollection(LinkedList::new));
                                value.getSetMethod().invoke(instance, enumList);
                                break;
                            case MAP:
                                throw new IllegalStateException("not implemented");
                            default:
                                throw new IllegalStateException("not allowed");
                        }
                    } else {
                        value.getSetMethod().invoke(instance, extractEnumConstant(jsonDocument.getKey(e.getKey()).toString(), (Enum<?>[]) value.getValueClass().getEnumConstants()));
                    }
                } else {
                    value.getSetMethod().invoke(instance, adjustRoboJsonDocumentCast(value, jsonDocument, e.getKey()));
                }
            } catch (Exception e1) {
                throw new RoboReflectException("create instance field", e1);
            }
        });
        return (T) instance;
    } catch (Exception e) {
        throw new RoboReflectException("create instance with setter", e);
    }
}
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) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO)

Example 4 with JsonDocument

use of com.robo4j.socket.http.json.JsonDocument in project robo4j by Robo4J.

the class ReflectUtils method castObjectByRoboJsonDocument.

@SuppressWarnings("unchecked")
private static <T> T castObjectByRoboJsonDocument(Class<T> clazz, Object value) {
    if (clazz.equals(String.class) || WITHOUT_QUOTATION_TYPES.contains(clazz)) {
        return (T) adjustRoboClassCast(clazz, value);
    } else {
        try {
            Object instance = clazz.getDeclaredConstructor().newInstance();
            Map<String, ClassGetSetDTO> fieldNameMethods = getFieldsTypeMap(clazz);
            JsonDocument document = (JsonDocument) value;
            fieldNameMethods.forEach((k, v) -> {
                Object setValue = adjustRoboClassCast(v.getValueClass(), document.getKey(k));
                try {
                    v.getSetMethod().invoke(instance, setValue);
                } catch (Exception e) {
                    throw new RoboReflectException("set value", e);
                }
            });
            return (T) instance;
        } catch (Exception e) {
            throw new RoboReflectException("casting: new class instance", e);
        }
    }
}
Also used : UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument)

Example 5 with JsonDocument

use of com.robo4j.socket.http.json.JsonDocument in project robo4j by Robo4J.

the class HttpPathUtils method readServerPathDTO.

/**
 * parse json string to mutable path properties
 *
 * @param configurationJson
 *            configuration json
 * @return return server path dto with method and possible properties
 */
public static HttpPathMethodDTO readServerPathDTO(String configurationJson) {
    Class<HttpPathMethodDTO> clazz = HttpPathMethodDTO.class;
    JsonReader jsonReader = new JsonReader(configurationJson);
    JsonDocument document = jsonReader.read();
    return ReflectUtils.createInstanceByClazzAndDescriptorAndJsonDocument(clazz, document);
}
Also used : JsonReader(com.robo4j.socket.http.json.JsonReader) HttpPathMethodDTO(com.robo4j.socket.http.dto.HttpPathMethodDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument)

Aggregations

JsonDocument (com.robo4j.socket.http.json.JsonDocument)5 ClassGetSetDTO (com.robo4j.socket.http.dto.ClassGetSetDTO)3 UTF8_CURLY_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT)3 UTF8_CURLY_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT)3 UTF8_SQUARE_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT)3 UTF8_SQUARE_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT)3 JsonGenericTypeAdapter (com.robo4j.socket.http.json.JsonGenericTypeAdapter)2 JsonReader (com.robo4j.socket.http.json.JsonReader)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 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