Search in sources :

Example 51 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project incubator-servicecomb-java-chassis by apache.

the class ProtobufSchemaUtils method getOrCreateSchema.

// 适用于将单个类型包装的场景
// 比如return
public static WrapSchema getOrCreateSchema(Type type) {
    JavaType javaType = TypeFactory.defaultInstance().constructType(type);
    // List<String> -> java.util.List<java.lang.String>
    // List<List<String>> -> java.util.List<java.util.List<java.lang.String>>
    String key = javaType.toCanonical();
    return getOrCreateSchema(key, () -> {
        if (!isNeedWrap(javaType.getRawClass())) {
            // 可以直接使用
            Schema<?> schema = RuntimeSchema.createFrom(javaType.getRawClass());
            return WrapSchemaFactory.createSchema(schema, WrapType.NOT_WRAP);
        }
        // 需要包装
        WrapClassConfig config = new WrapClassConfig();
        config.setType(WrapType.NORMAL_WRAP);
        config.setClassName("gen.wrap.protobuf." + org.apache.servicecomb.swagger.generator.core.utils.ClassUtils.correctClassName(key));
        if (!Void.TYPE.isAssignableFrom(javaType.getRawClass())) {
            config.addField("field0", javaType);
        }
        JavassistUtils.genSingleWrapperInterface(config);
        return createWrapSchema(config);
    });
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Example 52 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project pmph by BCSquad.

the class JsonUtil method main.

@SuppressWarnings({ "deprecation", "unchecked" })
public static void main(String[] args) throws Exception {
    // String jsonDecPosition =
    // "[{'id':4,'chosenPosition':1,'rank':1,'isDigitalEditor':true},{'id':3,'chosenPosition':2,'rank':3,'isDigitalEditor':true},{'id':1,'chosenPosition':3,'rank':'','isDigitalEditor':false}]";
    String jsonString = "[{'title':'你会网上冲浪吗?','type':1,'direction':null,'sort':1,'surveyQuestionOptionList':[{'optionContent':'不会'},{'optionContent':'会'}]},{'title':'你最喜欢的运动是什么','type':2,'direction':'','sort':'2','surveyQuestionOptionList':[{'optionContent':'篮球'},{'optionContent':'足球'},{'optionContent':'乒乓球'},{'optionContent':'羽毛球'},{'optionContent':'棒球'},{'optionContent':'网球'}]}]";
    // JavaType javaType = getCollectionType(ArrayList.class, SurveyQuestionListVO.class);
    // List<SurveyQuestionListVO> lst =
    // (List<SurveyQuestionListVO>) objectMapper.readValue(jsonString, javaType);
    // System.out.println(lst.toString());
    // System.out.println(decode(jsonString, new TypeReference<SurveyQuestionListVO>() {
    // }).toString());
    JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, SurveyQuestionListVO.class);
    List<SurveyQuestionListVO> lst = (List<SurveyQuestionListVO>) objectMapper.readValue(jsonString, javaType);
    // List<SurveyQuestionListVO> beanList =
    // objectMapper.readValue(jsonString, new TypeReference<List<SurveyQuestionListVO>>() {
    // });
    System.out.println(lst.toString());
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SurveyQuestionListVO(com.bc.pmpheep.back.vo.SurveyQuestionListVO) ArrayList(java.util.ArrayList) List(java.util.List)

Example 53 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project keycloak by keycloak.

the class ObjectMapperResolver method createStreamSerializer.

public static ObjectMapper createStreamSerializer() {
    ObjectMapper mapper = new ObjectMapper();
    JavaType type = TypeFactory.unknownType();
    JavaType streamType = mapper.getTypeFactory().constructParametricType(Stream.class, type);
    SimpleModule module = new SimpleModule();
    module.addSerializer(new StreamSerializer(streamType, type));
    mapper.registerModule(module);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    if (Boolean.parseBoolean(System.getProperty("keycloak.jsonPrettyPrint", "false"))) {
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
    }
    return mapper;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) StreamSerializer(com.fasterxml.jackson.datatype.jdk8.StreamSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 54 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project cytoscape-impl by cytoscape.

the class PropertiesJsonDeserializer method readValue.

public static Object readValue(final String key, final String input, final ObjectMapper mapper, final AbstractCustomGraphics2<?> cg2) {
    Object value = null;
    final Class<?> type = cg2.getSettingType(key);
    if (type != null) {
        final TypeFactory typeFactory = mapper.getTypeFactory();
        try {
            if (type == Array.class) {
                final Class<?> elementType = cg2.getSettingElementType(key);
                if (elementType != null) {
                    final ArrayType arrType = typeFactory.constructArrayType(elementType);
                    if (mapper.canDeserialize(arrType))
                        value = mapper.readValue(input, arrType);
                }
            } else if (List.class.isAssignableFrom(type)) {
                final Class<?> elementType = cg2.getSettingElementType(key);
                if (elementType != null) {
                    final CollectionType collType = typeFactory.constructCollectionType(List.class, elementType);
                    if (mapper.canDeserialize(collType))
                        value = mapper.readValue(input, collType);
                }
            } else {
                final JavaType simpleType = typeFactory.constructSimpleType(type, new JavaType[] {});
                if (mapper.canDeserialize(simpleType))
                    value = mapper.readValue(input, simpleType);
            }
        } catch (Exception e) {
            logger.error("Cannot parse JSON field " + key, e);
        }
    }
    return value;
}
Also used : ArrayType(com.fasterxml.jackson.databind.type.ArrayType) JavaType(com.fasterxml.jackson.databind.JavaType) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) List(java.util.List) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 55 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project spring-integration by spring-projects.

the class Jackson2JsonObjectMapper method extractJavaType.

@Override
@SuppressWarnings({ "unchecked" })
protected JavaType extractJavaType(Map<String, Object> javaTypes) throws Exception {
    JavaType classType = this.createJavaType(javaTypes, JsonHeaders.TYPE_ID);
    if (!classType.isContainerType() || classType.isArrayType()) {
        return classType;
    }
    JavaType contentClassType = this.createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
    if (classType.getKeyType() == null) {
        return this.objectMapper.getTypeFactory().constructCollectionType((Class<? extends Collection<?>>) classType.getRawClass(), contentClassType);
    }
    JavaType keyClassType = this.createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
    return this.objectMapper.getTypeFactory().constructMapType((Class<? extends Map<?, ?>>) classType.getRawClass(), keyClassType, contentClassType);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)322 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 Test (org.junit.Test)29 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)25 IOException (java.io.IOException)25 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)22 ArrayList (java.util.ArrayList)21 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)17 Property (io.swagger.models.properties.Property)16 List (java.util.List)16 Map (java.util.Map)14 ModelImpl (io.swagger.models.ModelImpl)13 StringProperty (io.swagger.models.properties.StringProperty)13 Annotation (java.lang.annotation.Annotation)12 Method (java.lang.reflect.Method)11 Type (java.lang.reflect.Type)11 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)10 BodyParameter (io.swagger.models.parameters.BodyParameter)10 MapProperty (io.swagger.models.properties.MapProperty)10 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)9