Example usage for com.fasterxml.jackson.databind BeanProperty getType

List of usage examples for com.fasterxml.jackson.databind BeanProperty getType

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind BeanProperty getType.

Prototype

public abstract JavaType getType();

Source Link

Usage

From source file:org.jongo.marshall.jackson.oid.ObjectIdSerializer.java

public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
        throws JsonMappingException {
    return new ObjectIdSerializer(ObjectId.class.isAssignableFrom(property.getType().getRawClass()));
}

From source file:org.jongo.marshall.jackson.oid.ObjectIdDeserializer.java

public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
        throws JsonMappingException {
    return new ObjectIdDeserializer(ObjectId.class.isAssignableFrom(property.getType().getRawClass()));
}

From source file:com.addthis.codec.jackson.BasicInjectableValues.java

@Override
public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty,
        Object beanInstance) {/*ww  w.  ja v a  2  s. c o  m*/
    if (TypeReference.class.getName().equals(valueId)) {
        return new StoredTypeReference(forProperty.getType().containedType(0));
    }
    return null;
}

From source file:nl.talsmasoftware.enumerables.support.json.jackson2.EnumerableDeserializer.java

/**
 * Creates a more specific deserializer in case this instance is the 'general untyped' instance. The available
 * type information from the bean property and the deserialization context will be used to return a more specific
 * instance in that case.//w w w .j a  v a  2 s. c  o m
 * If this deserializer is already specific, or no additional type information can be obtained, the method simply
 * returns a reference to <code>this</code> instance.
 *
 * @param ctxt     The deserialization context to obtain type information from, if possible.
 * @param property The bean property to obtain type information from, if possible.
 * @return A more specific deserializer or a reference to <code>this</code> instance in case no more specific
 * deserializer could be found.
 */
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
    JavaType type = property != null ? property.getType() : null;
    if (asEnumerableSubclass(type) != null && !type.equals(javaType))
        return new EnumerableDeserializer(type);

    type = Compatibility.getContextualType(ctxt);
    if (asEnumerableSubclass(type) != null && !type.equals(javaType))
        return new EnumerableDeserializer(type);

    return this;
}

From source file:capital.scalable.restdocs.jackson.FieldDocumentationObjectVisitor.java

@Override
public void optionalProperty(BeanProperty prop) throws JsonMappingException {
    String jsonName = prop.getName();
    String fieldName = prop.getMember().getName();

    JavaType type = prop.getType();
    if (type == null) {
        throw new IllegalStateException(
                "Missing type for property '" + jsonName + "', " + "field '" + fieldName + "'");
    }//from ww  w.  j  a v a 2s .  co m

    JsonSerializer<?> ser = getSer(prop);
    if (ser == null) {
        return;
    }

    String fieldPath = path + (path.isEmpty() ? "" : ".") + jsonName;
    Class<?> javaBaseClass = prop.getMember().getDeclaringClass();
    boolean shouldExpand = shouldExpand(prop);

    InternalFieldInfo fieldInfo = new InternalFieldInfo(javaBaseClass, fieldName, fieldPath, shouldExpand);

    JsonFormatVisitorWrapper visitor = new FieldDocumentationVisitorWrapper(getProvider(), context, fieldPath,
            fieldInfo);

    ser.acceptJsonFormatVisitor(visitor, type);
}

From source file:com.netflix.suro.jackson.DefaultObjectMapper.java

@Inject
public DefaultObjectMapper(final Injector injector, Set<TypeHolder> crossInjectable) {
    SimpleModule serializerModule = new SimpleModule("SuroServer default serializers");
    serializerModule.addSerializer(ByteOrder.class, ToStringSerializer.instance);
    serializerModule.addDeserializer(ByteOrder.class, new JsonDeserializer<ByteOrder>() {
        @Override//  w  w  w .  j  a v a2  s  .  co m
        public ByteOrder deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            if (ByteOrder.BIG_ENDIAN.toString().equals(jp.getText())) {
                return ByteOrder.BIG_ENDIAN;
            }
            return ByteOrder.LITTLE_ENDIAN;
        }
    });
    registerModule(serializerModule);
    registerModule(new GuavaModule());

    if (injector != null) {
        setInjectableValues(new InjectableValues() {
            @Override
            public Object findInjectableValue(Object valueId, DeserializationContext ctxt,
                    BeanProperty forProperty, Object beanInstance) {
                LOG.info("Looking for " + valueId);
                try {
                    return injector.getInstance(
                            Key.get(forProperty.getType().getRawClass(), Names.named((String) valueId)));
                } catch (Exception e) {
                    try {
                        return injector.getInstance(forProperty.getType().getRawClass());
                    } catch (Exception ex) {
                        LOG.info("No implementation found, returning null");
                    }
                    return null;
                }
            }
        });
    }

    configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    configure(MapperFeature.AUTO_DETECT_GETTERS, false);
    configure(MapperFeature.AUTO_DETECT_CREATORS, false);
    configure(MapperFeature.AUTO_DETECT_FIELDS, false);
    configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
    configure(MapperFeature.AUTO_DETECT_SETTERS, false);
    configure(SerializationFeature.INDENT_OUTPUT, false);

    if (crossInjectable != null) {
        for (TypeHolder entry : crossInjectable) {
            LOG.info("Registering subtype : " + entry.getName() + " -> "
                    + entry.getRawType().getCanonicalName());
            registerSubtypes(new NamedType(entry.getRawType(), entry.getName()));
        }
    }
}

From source file:capital.scalable.restdocs.jackson.FieldDocumentationObjectVisitor.java

protected JsonSerializer<?> getSer(BeanProperty prop) throws JsonMappingException {
    JsonSerializer<Object> ser = null;
    if (prop instanceof BeanPropertyWriter) {
        ser = ((BeanPropertyWriter) prop).getSerializer();
    }// ww w.  j  av  a  2 s  .  c  o m
    if (ser == null) {
        ser = getProvider().findValueSerializer(prop.getType(), prop);
    }
    return ser;
}

From source file:java2typescript.jackson.module.visitors.TSJsonObjectFormatVisitor.java

protected AbstractType getTSTypeForProperty(BeanProperty writer) throws JsonMappingException {
    if (writer == null) {
        throw new IllegalArgumentException("Null writer");
    }/*from  w w w. j  a  v a  2  s. c  om*/
    JavaType type = writer.getType();
    if (type.getRawClass().equals(Void.TYPE)) {
        return VoidType.getInstance();
    }

    AbstractType customType = conf.getCustomTypes().get(type.getRawClass().getName());
    if (customType != null) {
        return customType;
    }

    try {
        JsonSerializer<Object> ser = getSer(writer);

        if (ser != null) {
            if (type == null) {
                throw new IllegalStateException("Missing type for property '" + writer.getName() + "'");
            }
            return getTSTypeForHandler(this, ser, type, conf);
        } else {
            return AnyType.getInstance();
        }

    } catch (Exception e) {
        throw new RuntimeException(String.format(//
                "Error when serializing %s, you should add a custom mapping for it", type.getRawClass()), e);
    }

}

From source file:java2typescript.jackson.module.visitors.TSJsonObjectFormatVisitor.java

protected JsonSerializer<java.lang.Object> getSer(BeanProperty writer) throws JsonMappingException {
    JsonSerializer<Object> ser = null;
    if (writer instanceof BeanPropertyWriter) {
        ser = ((BeanPropertyWriter) writer).getSerializer();
    }//  ww  w .j a  va 2s. c o m
    if (ser == null) {
        ser = getProvider().findValueSerializer(writer.getType(), writer);
    }
    return ser;
}

From source file:org.brutusin.json.impl.JacksonSchemaFactory.java

void enrich(SimpleTypeSchema schema, BeanProperty beanProperty) {
    JsonProperty jsonAnnot = beanProperty.getAnnotation(JsonProperty.class);
    IndexableProperty indexAnnot = beanProperty.getAnnotation(IndexableProperty.class);
    DependentProperty dependsAnnot = beanProperty.getAnnotation(DependentProperty.class);

    if (schema instanceof StringSchema) {
        StringSchema sschema = (StringSchema) schema;
        try {/*from   w  ww  .ja va 2  s  .  c  o  m*/
            Set<String> enums = sschema.getEnums();
            if (enums != null) {
                Method method = schema.getClass().getMethod("setValues", List.class);
                method.invoke(schema, new ArrayList(enums));
            }
        } catch (Exception parseException) {
            throw new Error("Error setting enum value from enumeration for " + beanProperty.getFullName(),
                    parseException);
        }
    }

    if (jsonAnnot == null) {
        schema.setTitle(beanProperty.getName());
    } else {
        if (jsonAnnot.title() != null) {
            schema.setTitle(jsonAnnot.title());
        } else {
            schema.setTitle(beanProperty.getName());
        }
        schema.setDescription(jsonAnnot.description());
        schema.setRequired(jsonAnnot.required());
        String def = jsonAnnot.defaultJsonExp();
        if (def != null) {
            try {
                Object defaultValue = JsonCodec.getInstance().parse(def, beanProperty.getType().getRawClass());
                Method method = schema.getClass().getMethod("setDef", Object.class);
                method.invoke(schema, defaultValue);
            } catch (Exception parseException) {
                throw new Error("Error setting default value for " + beanProperty.getFullName(),
                        parseException);
            }
        }
        String valuesMethodName = jsonAnnot.valuesMethod();
        if (valuesMethodName != null && !valuesMethodName.isEmpty()) {
            try {
                Method valuesMethod = beanProperty.getMember().getDeclaringClass().getMethod(valuesMethodName,
                        null);
                valuesMethod.setAccessible(true);
                Object valuesValue = valuesMethod.invoke(null, null);
                Method method = schema.getClass().getMethod("setValues", List.class);
                method.invoke(schema, valuesValue);
            } catch (Exception ex) {
                throw new Error("Error setting enum value from @JsonProperty.valuesMethod() for "
                        + beanProperty.getFullName(), ex);
            }
        } else {
            String values = jsonAnnot.values();
            if (values != null && !values.isEmpty()) {
                try {
                    Object valuesValue = JsonCodec.getInstance().parse(values, List.class);
                    Method method = schema.getClass().getMethod("setValues", List.class);
                    method.invoke(schema, valuesValue);
                } catch (Exception parseException) {
                    throw new Error("Error setting enum value from @JsonProperty.values() for "
                            + beanProperty.getFullName(), parseException);
                }
            }
        }
    }
    if (indexAnnot != null) {
        try {
            Method method = schema.getClass().getMethod("setIndex", IndexableProperty.IndexMode.class);
            method.invoke(schema, indexAnnot.mode());
        } catch (Exception parseException) {
            throw new Error("Error setting index value for " + beanProperty.getFullName(), parseException);
        }
    }
    if (dependsAnnot != null) {
        try {
            Method method = schema.getClass().getMethod("setDependsOn", String[].class);
            method.invoke(schema, (Object) dependsAnnot.dependsOn());
        } catch (Exception parseException) {
            throw new Error("Error setting dependsOn value for " + beanProperty.getFullName(), parseException);
        }
    }
}