Search in sources :

Example 1 with BeanProperty

use of com.fasterxml.jackson.databind.BeanProperty in project druid by druid-io.

the class WorkerBehaviorConfigTest method testSerde.

@Test
public void testSerde() throws Exception {
    WorkerBehaviorConfig config = new WorkerBehaviorConfig(new FillCapacityWithAffinityWorkerSelectStrategy(new FillCapacityWithAffinityConfig(ImmutableMap.of("foo", Arrays.asList("localhost")))), new EC2AutoScaler(7, 11, new EC2EnvironmentConfig("us-east-1a", new EC2NodeData("amiid", "instanceType", 3, 5, Arrays.asList("securityGroupIds"), "keyNames", "subnetId", null, null), new StringEC2UserData("availZone", "replace", "version")), null, null));
    final ObjectMapper mapper = new DefaultObjectMapper();
    mapper.setInjectableValues(new InjectableValues() {

        @Override
        public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) {
            return null;
        }
    });
    Assert.assertEquals(config, mapper.readValue(mapper.writeValueAsBytes(config), WorkerBehaviorConfig.class));
}
Also used : EC2EnvironmentConfig(io.druid.indexing.overlord.autoscaling.ec2.EC2EnvironmentConfig) InjectableValues(com.fasterxml.jackson.databind.InjectableValues) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) StringEC2UserData(io.druid.indexing.overlord.autoscaling.ec2.StringEC2UserData) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) EC2NodeData(io.druid.indexing.overlord.autoscaling.ec2.EC2NodeData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 2 with BeanProperty

use of com.fasterxml.jackson.databind.BeanProperty in project druid by druid-io.

the class EC2AutoScalerSerdeTest method testSerde.

@Test
public void testSerde() throws Exception {
    final ObjectMapper objectMapper = new DefaultObjectMapper();
    objectMapper.setInjectableValues(new InjectableValues() {

        @Override
        public Object findInjectableValue(Object o, DeserializationContext deserializationContext, BeanProperty beanProperty, Object o1) {
            return null;
        }
    });
    final EC2AutoScaler autoScaler = objectMapper.readValue(json, EC2AutoScaler.class);
    verifyAutoScaler(autoScaler);
    final EC2AutoScaler roundTripAutoScaler = objectMapper.readValue(objectMapper.writeValueAsBytes(autoScaler), EC2AutoScaler.class);
    verifyAutoScaler(roundTripAutoScaler);
    Assert.assertEquals("Round trip equals", autoScaler, roundTripAutoScaler);
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) InjectableValues(com.fasterxml.jackson.databind.InjectableValues) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) Test(org.junit.Test)

Example 3 with BeanProperty

use of com.fasterxml.jackson.databind.BeanProperty in project torodb by torodb.

the class DescriptionFactoryWrapper method expectObjectFormat.

@Override
public JsonObjectFormatVisitor expectObjectFormat(JavaType convertedType) {
    final JsonPointer jsonPointer = getJsonPointer();
    return new JsonObjectFormatVisitor.Base(getProvider()) {

        @Override
        public void property(BeanProperty prop) throws JsonMappingException {
            visitProperty(prop, false);
        }

        @Override
        public void optionalProperty(BeanProperty prop) throws JsonMappingException {
            visitProperty(prop, true);
        }

        private void visitProperty(BeanProperty prop, boolean optional) throws JsonMappingException {
            JsonPointer propPointer = jsonPointer.append(JsonPointer.valueOf("/" + prop.getName()));
            document(propPointer, prop);
            SerializerProvider p = getProvider();
            JsonSerializer<Object> s = p.findValueSerializer(prop.getType(), prop);
            s.acceptJsonFormatVisitor(new DescriptionFactoryWrapper(DescriptionFactoryWrapper.this, propPointer, p), prop.getType());
        }
    };
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Example 4 with BeanProperty

use of com.fasterxml.jackson.databind.BeanProperty in project dropwizard by dropwizard.

the class ConfigurationMetadata method expectObjectFormat.

@Override
public JsonObjectFormatVisitor expectObjectFormat(JavaType type) throws JsonMappingException {
    // store the pointer to the own instance
    final ConfigurationMetadata thiss = this;
    return new JsonObjectFormatVisitor.Base() {

        @Override
        public void optionalProperty(BeanProperty prop) throws JsonMappingException {
            // don't run into an infinite loop with circular dependencies
            if (currentDepth >= MAX_DEPTH) {
                return;
            }
            // check if we already visited the same property
            if (parentProps.contains(prop)) {
                return;
            }
            if (prop.getAnnotation(JsonIgnore.class) != null) {
                return;
            }
            // build the complete field path
            String name = !currentPrefix.isEmpty() ? currentPrefix + "." + prop.getName() : prop.getName();
            // set state for the recursive traversal
            int oldFieldSize = fields.size();
            String oldPrefix = currentPrefix;
            currentPrefix = name;
            currentDepth++;
            // the type of the field
            JavaType fieldType = prop.getType();
            // to the path
            if (fieldType.isCollectionLikeType() || fieldType.isArrayType()) {
                fieldType = fieldType.getContentType();
                currentPrefix += "[*]";
            }
            // get the type deserializer
            TypeDeserializer typeDeserializer = mapper.getDeserializationConfig().findTypeDeserializer(fieldType);
            // get the default impl if available
            Class<?> defaultImpl = typeDeserializer != null ? typeDeserializer.getDefaultImpl() : null;
            // remember current property
            parentProps.add(prop);
            // visit the type of the property (or its defaultImpl).
            try {
                mapper.acceptJsonFormatVisitor(defaultImpl == null ? fieldType.getRawClass() : defaultImpl, thiss);
            } catch (NoClassDefFoundError | TypeNotPresentException e) {
                // that case, just ignore the default implementation
                if (defaultImpl != null) {
                    return;
                } else {
                    // implementation, so re-throw it
                    throw e;
                }
            } finally {
                // reset state after the recursive traversal
                parentProps.remove(prop);
                currentDepth--;
                currentPrefix = oldPrefix;
            }
            // if no new fields are discovered, we assume that we are at an primitive field
            if (oldFieldSize == fields.size()) {
                fields.put(name, prop.getType());
            }
        }
    };
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Example 5 with BeanProperty

use of com.fasterxml.jackson.databind.BeanProperty in project beam by apache.

the class PipelineOptionsFactory method computeCustomSerializerForMethod.

private static Optional<JsonSerializer<Object>> computeCustomSerializerForMethod(Method method) {
    try {
        BeanProperty prop = createBeanProperty(method);
        AnnotatedMember annotatedMethod = prop.getMember();
        Object maybeSerializerClass = SERIALIZER_PROVIDER.getAnnotationIntrospector().findSerializer(annotatedMethod);
        return Optional.fromNullable(SERIALIZER_PROVIDER.serializerInstance(annotatedMethod, maybeSerializerClass));
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Aggregations

BeanProperty (com.fasterxml.jackson.databind.BeanProperty)11 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)5 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Test (org.junit.Test)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)3 JavaType (com.fasterxml.jackson.databind.JavaType)2 Module (com.fasterxml.jackson.databind.Module)2 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)2 TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)2 EC2AutoScaler (io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler)2 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)2 IOException (java.io.IOException)2 AutoScaler (org.apache.druid.indexing.overlord.autoscaling.AutoScaler)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 PropertyName (com.fasterxml.jackson.databind.PropertyName)1