Search in sources :

Example 1 with BeanPropertyWriter

use of com.fasterxml.jackson.databind.ser.BeanPropertyWriter in project eureka by Netflix.

the class AbstractEurekaJacksonCodec method bindAmazonInfoFilter.

private void bindAmazonInfoFilter(ObjectMapper mapper) {
    SimpleFilterProvider filters = new SimpleFilterProvider();
    final String filterName = "exclude-amazon-info-entries";
    mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {

        @Override
        public Object findFilterId(Annotated a) {
            if (Map.class.isAssignableFrom(a.getRawType())) {
                return filterName;
            }
            return super.findFilterId(a);
        }
    });
    filters.addFilter(filterName, new SimpleBeanPropertyFilter() {

        @Override
        protected boolean include(BeanPropertyWriter writer) {
            return true;
        }

        @Override
        protected boolean include(PropertyWriter writer) {
            return MINI_AMAZON_INFO_INCLUDE_KEYS.contains(writer.getName());
        }
    });
    mapper.setFilters(filters);
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) Map(java.util.Map) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) PropertyWriter(com.fasterxml.jackson.databind.ser.PropertyWriter)

Example 2 with BeanPropertyWriter

use of com.fasterxml.jackson.databind.ser.BeanPropertyWriter in project jackson-databind by FasterXML.

the class BeanAsArraySerializer method serializeAsArray.

protected final void serializeAsArray(Object bean, JsonGenerator gen, SerializerProvider provider) throws IOException {
    final BeanPropertyWriter[] props;
    if (_filteredProps != null && provider.getActiveView() != null) {
        props = _filteredProps;
    } else {
        props = _props;
    }
    int i = 0;
    try {
        for (final int len = props.length; i < len; ++i) {
            BeanPropertyWriter prop = props[i];
            if (prop == null) {
                // can have nulls in filtered list; but if so, MUST write placeholders
                gen.writeNull();
            } else {
                prop.serializeAsElement(bean, gen, provider);
            }
        }
    // NOTE: any getters can not be supported either
    //if (_anyGetterWriter != null) {
    //    _anyGetterWriter.getAndSerialize(bean, gen, provider);
    //}
    } catch (Exception e) {
        String name = (i == props.length) ? "[anySetter]" : props[i].getName();
        wrapAndThrow(provider, e, bean, name);
    } catch (StackOverflowError e) {
        JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
        String name = (i == props.length) ? "[anySetter]" : props[i].getName();
        mapE.prependPath(new JsonMappingException.Reference(bean, name));
        throw mapE;
    }
}
Also used : BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) IOException(java.io.IOException)

Example 3 with BeanPropertyWriter

use of com.fasterxml.jackson.databind.ser.BeanPropertyWriter in project jackson-databind by FasterXML.

the class JacksonAnnotationIntrospector method findAndAddVirtualProperties.

@Override
public void findAndAddVirtualProperties(MapperConfig<?> config, AnnotatedClass ac, List<BeanPropertyWriter> properties) {
    JsonAppend ann = _findAnnotation(ac, JsonAppend.class);
    if (ann == null) {
        return;
    }
    final boolean prepend = ann.prepend();
    JavaType propType = null;
    // First: any attribute-backed properties?
    JsonAppend.Attr[] attrs = ann.attrs();
    for (int i = 0, len = attrs.length; i < len; ++i) {
        if (propType == null) {
            propType = config.constructType(Object.class);
        }
        BeanPropertyWriter bpw = _constructVirtualProperty(attrs[i], config, ac, propType);
        if (prepend) {
            properties.add(i, bpw);
        } else {
            properties.add(bpw);
        }
    }
    // Then: general-purpose virtual properties?
    JsonAppend.Prop[] props = ann.props();
    for (int i = 0, len = props.length; i < len; ++i) {
        BeanPropertyWriter bpw = _constructVirtualProperty(props[i], config, ac);
        if (prepend) {
            properties.add(i, bpw);
        } else {
            properties.add(bpw);
        }
    }
}
Also used : BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) VirtualBeanPropertyWriter(com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter)

Example 4 with BeanPropertyWriter

use of com.fasterxml.jackson.databind.ser.BeanPropertyWriter in project jackson-module-afterburner by FasterXML.

the class TestAccessorGeneration method testLotsaIntAccessorGeneration.

// And then test to ensure Switch-table construction also works...
public void testLotsaIntAccessorGeneration() throws Exception {
    PropertyAccessorCollector coll = new PropertyAccessorCollector(BeanN.class);
    String[] methodNames = new String[] { "getX", "getY", "get3", "get4", "get5", "get6", "get7" };
    for (String methodName : methodNames) {
        Method method = BeanN.class.getDeclaredMethod(methodName);
        AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
        coll.addIntGetter(new BeanPropertyWriter(SimpleBeanPropertyDefinition.construct(null, annMethod, new PropertyName(methodName)), annMethod, null, null, null, null, null, false, null));
    }
    BeanPropertyAccessor acc = coll.findAccessor(null);
    BeanN bean = new BeanN();
    assertEquals(bean.getX(), acc.intGetter(bean, 0));
    assertEquals(bean.getY(), acc.intGetter(bean, 1));
    assertEquals(bean.get3(), acc.intGetter(bean, 2));
    assertEquals(bean.get4(), acc.intGetter(bean, 3));
    assertEquals(bean.get5(), acc.intGetter(bean, 4));
    assertEquals(bean.get6(), acc.intGetter(bean, 5));
    assertEquals(bean.get7(), acc.intGetter(bean, 6));
}
Also used : PropertyName(com.fasterxml.jackson.databind.PropertyName) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) Method(java.lang.reflect.Method) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter)

Example 5 with BeanPropertyWriter

use of com.fasterxml.jackson.databind.ser.BeanPropertyWriter in project Rosetta by HubSpot.

the class StoredAsJsonBeanSerializerModifier method changeProperties.

@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
    for (BeanPropertyWriter beanProperty : beanProperties) {
        StoredAsJson storedAsJson = beanProperty.getAnnotation(StoredAsJson.class);
        if (storedAsJson != null && !StoredAsJson.NULL.equals(storedAsJson.empty())) {
            final JsonSerializer<Object> nullSerializer;
            if (storedAsJson.binary()) {
                nullSerializer = new ConstantBinarySerializer(storedAsJson.empty());
            } else {
                nullSerializer = new ConstantSerializer(storedAsJson.empty());
            }
            beanProperty.assignNullSerializer(nullSerializer);
        }
    }
    return super.changeProperties(config, beanDesc, beanProperties);
}
Also used : BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) StoredAsJson(com.hubspot.rosetta.annotations.StoredAsJson)

Aggregations

BeanPropertyWriter (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)7 PropertyName (com.fasterxml.jackson.databind.PropertyName)3 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)3 Method (java.lang.reflect.Method)3 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)1 PropertyWriter (com.fasterxml.jackson.databind.ser.PropertyWriter)1 VirtualBeanPropertyWriter (com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter)1 SimpleBeanPropertyFilter (com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 StoredAsJson (com.hubspot.rosetta.annotations.StoredAsJson)1 IOException (java.io.IOException)1 Map (java.util.Map)1