Example usage for com.fasterxml.jackson.databind.type MapType narrowKey

List of usage examples for com.fasterxml.jackson.databind.type MapType narrowKey

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.type MapType narrowKey.

Prototype

public JavaType narrowKey(Class<?> paramClass) 

Source Link

Usage

From source file:com.xiongyingqi.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver.java

@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest)
        throws Exception {
    String[] paramValues = webRequest.getParameterValues(name);

    Class<?> paramType = parameter.getParameterType();
    if (paramValues == null) {
        return null;
    }/*from   w  w w  . j  ava  2 s .co  m*/

    // ?
    //      for (int i = 0; i < paramValues.length; i++) {
    //         String paramValue = paramValues[i];
    //         String encodedParamValue = new String(paramValue.getBytes("ISO-8859-1"), encoding);
    //         paramValues[i] = encodedParamValue;
    //      }
    try {
        if (paramValues.length == 1) {
            String text = paramValues[0];
            Type type = parameter.getGenericParameterType();

            if (MapWapper.class.isAssignableFrom(paramType)) {
                MapWapper<?, ?> jsonMap = (MapWapper<?, ?>) paramType.newInstance();

                MapType mapType = (MapType) getJavaType(HashMap.class);

                if (type instanceof ParameterizedType) {
                    mapType = (MapType) mapType
                            .narrowKey((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]);
                    mapType = (MapType) mapType.narrowContentsBy(
                            (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[1]);
                }
                jsonMap.setInnerMap(mapper.<Map>readValue(text, mapType));
                return jsonMap;
            }

            JavaType javaType = getJavaType(paramType);

            if (Collection.class.isAssignableFrom(paramType)) {
                javaType = javaType
                        .narrowContentsBy((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]);
            }
            return mapper.readValue(paramValues[0], javaType);
        }

    } catch (Exception e) {
        throw new JsonMappingException("Could not read request json parameter", e);
    }

    throw new UnsupportedOperationException("too many request json parameter '" + name
            + "' for method parameter type [" + paramType + "], only support one json parameter");
}