Search in sources :

Example 1 with GenericMap

use of com.alipay.hessian.generic.model.GenericMap in project incubator-shenyu by apache.

the class SofaParamResolveServiceImpl method convertToParameterValue.

/**
 * convert to parameter value.
 *
 * @param value         value support [json object string, json array string,string]
 * @param parameterType parameter type support [javaPackage.ClassName#GenericType1#GenericType2], split is ,
 * @return GenericObject, GenericMap, List, string, array...
 * @see com.alipay.hessian.generic.model
 */
@SuppressWarnings("all")
private Object convertToParameterValue(final Object value, final String[] parameterType) {
    if (isSingleType(parameterType)) {
        return value;
    }
    if (value instanceof JsonObject && parameterType[0].contains("Map")) {
        final Map<String, Object> mapValue = GsonUtils.getInstance().convertToMap(value.toString());
        if (parameterType.length == 1) {
            // no generic info
            return mapValue;
        }
        assert parameterType.length == 3;
        // generic map
        final GenericMap genericMap = new GenericMap(parameterType[2]);
        mapValue.replaceAll((k, v) -> convertToGenericObject(parameterType[2], mapValue.get(k)));
        genericMap.setMap(mapValue);
        return genericMap;
    }
    if (value instanceof JsonObject) {
        return convertToGenericObject(parameterType[0], value);
    }
    if (value instanceof JsonArray) {
        if (parameterType.length == 1) {
            // no generic info
            return GsonUtils.getInstance().fromList(value.toString(), Object.class);
        }
        // generic collection
        final GenericCollection genericCollection = new GenericCollection(parameterType[1]);
        genericCollection.setCollection(convertToGenericObjects(parameterType[1], (Iterable<Object>) value));
        return genericCollection;
    }
    return value;
}
Also used : JsonArray(com.google.gson.JsonArray) GenericMap(com.alipay.hessian.generic.model.GenericMap) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericCollection(com.alipay.hessian.generic.model.GenericCollection)

Aggregations

GenericCollection (com.alipay.hessian.generic.model.GenericCollection)1 GenericMap (com.alipay.hessian.generic.model.GenericMap)1 GenericObject (com.alipay.hessian.generic.model.GenericObject)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1