Example usage for com.fasterxml.jackson.databind ObjectMapper setInjectableValues

List of usage examples for com.fasterxml.jackson.databind ObjectMapper setInjectableValues

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper setInjectableValues.

Prototype

public ObjectMapper setInjectableValues(InjectableValues injectableValues) 

Source Link

Document

Method for configuring InjectableValues which used to find values to inject.

Usage

From source file:com.sonatype.nexus.perftest.PerformanceTestRunner.java

public static void main(String[] args) throws Exception {
    final Nexus nexus = new Nexus();
    ObjectMapper mapper = new XmlMapper();
    mapper.setInjectableValues(new InjectableValues() {
        @Override/*from   w  ww .  j av  a2s .c  o  m*/
        public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty,
                Object beanInstance) {
            if (Nexus.class.getName().equals(valueId)) {
                return nexus;
            }
            return null;
        }
    });
    File src = new File(args[0]).getCanonicalFile();
    System.out.format("Using test configuration %s\n", src);
    PerformanceTest test = mapper.readValue(src, PerformanceTest.class);
    test.run();
    System.out.println("Exit");
    System.exit(0);
}

From source file:com.sonatype.nexus.perftest.PerformanceTestAsserter.java

public static void main(String[] args) throws Exception {

    final Nexus nexus = new Nexus();
    ObjectMapper mapper = new XmlMapper();

    mapper.setInjectableValues(new InjectableValues() {
        @Override/*from  w  ww .j a v a  2  s.c  o m*/
        public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty,
                Object beanInstance) {
            if (Nexus.class.getName().equals(valueId)) {
                return nexus;
            }
            return null;
        }
    });

    File src = new File(args[0]).getCanonicalFile();
    String name = src.getName().substring(0, src.getName().lastIndexOf("."));

    Collection<ClientSwarm> swarms = mapper.readValue(src, PerformanceTest.class).getSwarms();

    List<Metric> metrics = new ArrayList<>();
    for (ClientSwarm swarm : swarms) {
        metrics.add(swarm.getMetric());
    }

    System.out.println("Test " + name + " metrics:" + metrics);
    assertTest(name, metrics);
    System.out.println("Exit");
    System.exit(0);
}

From source file:com.arpnetworking.clusteraggregator.configuration.EmitterConfiguration.java

/**
 * Create an <code>ObjectMapper</code> for Emitter configuration.
 *
 * @param injector The Guice <code>Injector</code> instance.
 * @return An <code>ObjectMapper</code> for Pipeline configuration.
 *//*www. ja v  a  2  s . co m*/
public static ObjectMapper createObjectMapper(final Injector injector) {
    final ObjectMapper objectMapper = ObjectMapperFactory.createInstance();

    final GuiceAnnotationIntrospector guiceIntrospector = new GuiceAnnotationIntrospector();
    objectMapper.setInjectableValues(new GuiceInjectableValues(injector));
    objectMapper.setAnnotationIntrospectors(
            new AnnotationIntrospectorPair(guiceIntrospector,
                    objectMapper.getSerializationConfig().getAnnotationIntrospector()),
            new AnnotationIntrospectorPair(guiceIntrospector,
                    objectMapper.getDeserializationConfig().getAnnotationIntrospector()));

    return objectMapper;
}

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

public static ObjectMapper newObjectMapper(PluginRegistry pluginRegistry) {
    CodecModule codecModule = new CodecModule(pluginRegistry);
    Config globalConfig = pluginRegistry.config();
    ObjectMapper objectMapper = new ObjectMapper();
    toggleObjectMapperOptions(objectMapper);
    objectMapper.registerModule(codecModule);
    registerExtraModules(objectMapper);//  ww w  .  j av a  2 s  . co  m
    allowCommentsAndUnquotedFields(objectMapper);
    objectMapper.setInjectableValues(new BasicInjectableValues());
    return objectMapper;
}

From source file:com.arpnetworking.metrics.mad.configuration.PipelineConfiguration.java

/**
 * Create an <code>ObjectMapper</code> for Pipeline configuration.
 *
 * @param injector The Guice <code>Injector</code> instance.
 * @return An <code>ObjectMapper</code> for Pipeline configuration.
 *///from  ww w  . j  a  v a 2 s  .  c  om
public static ObjectMapper createObjectMapper(final Injector injector) {
    final ObjectMapper objectMapper = ObjectMapperFactory.createInstance();

    final SimpleModule module = new SimpleModule("Pipeline");
    module.addDeserializer(Statistic.class, new StatisticDeserializer());

    objectMapper.registerModules(module);

    final GuiceAnnotationIntrospector guiceIntrospector = new GuiceAnnotationIntrospector();
    objectMapper.setInjectableValues(new GuiceInjectableValues(injector));
    objectMapper.setAnnotationIntrospectors(
            new AnnotationIntrospectorPair(guiceIntrospector,
                    objectMapper.getSerializationConfig().getAnnotationIntrospector()),
            new AnnotationIntrospectorPair(guiceIntrospector,
                    objectMapper.getDeserializationConfig().getAnnotationIntrospector()));

    return objectMapper;
}

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

@Test
public void test() throws IOException {
    String spec = "{\"a\":\"aaa\", \"b\":\"bbb\"}";

    ObjectMapper mapper = new DefaultObjectMapper();
    final Map<String, Object> injectables = Maps.newHashMap();

    injectables.put("test", "test");
    injectables.put("b", "binjected");
    mapper.setInjectableValues(new InjectableValues() {
        @Override//from ww w  .j  a v a  2  s.  co m
        public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty,
                Object beanInstance) {
            return injectables.get(valueId);
        }
    });

    TestClass test = mapper.readValue(spec, new TypeReference<TestClass>() {
    });
    assertEquals(test.getTest(), "test");
    assertEquals(test.getB(), "bbb");
}

From source file:io.fineo.drill.exec.store.dynamo.physical.DynamoScanBatchCreator.java

@Override
public CloseableRecordBatch getBatch(FragmentContext context, DynamoSubScan subScan, List<RecordBatch> children)
        throws ExecutionSetupException {
    Preconditions.checkArgument(children.isEmpty());

    List<RecordReader> readers = Lists.newArrayList();
    List<SchemaPath> columns = subScan.getColumns();
    ClientProperties clientProps = subScan.getClient();
    ClientConfiguration client = clientProps.getConfiguration();
    AWSCredentialsProvider credentials = subScan.getCredentials();
    DynamoEndpoint endpoint = subScan.getEndpoint();
    DynamoTableDefinition table = subScan.getTable();
    DynamoKeyMapper key = null;/*from w  w w . j  a  va 2s.co  m*/
    if (table.getKeyMapper() != null) {
        DynamoKeyMapperSpec spec = table.getKeyMapper();
        Map<String, Object> args = spec.getArgs();
        ObjectMapper mapper = new ObjectMapper();
        try {
            String argString = mapper.writeValueAsString(args);
            InjectableValues inject = new InjectableValues.Std().addValue(DynamoKeyMapperSpec.class, spec);
            key = mapper.setInjectableValues(inject).readValue(argString, DynamoKeyMapper.class);
        } catch (IOException e) {
            throw new ExecutionSetupException(e);
        }
    }

    for (DynamoSubReadSpec scanSpec : subScan.getSpecs()) {
        try {
            DynamoRecordReader reader;
            if (scanSpec instanceof DynamoSubGetSpec) {
                reader = new DynamoGetRecordReader(credentials, client, endpoint, (DynamoSubGetSpec) scanSpec,
                        columns, clientProps.getConsistentRead(), table);
            } else if (scanSpec instanceof DynamoSubQuerySpec) {
                reader = new DynamoQueryRecordReader(credentials, client, endpoint,
                        (DynamoSubQuerySpec) scanSpec, columns, clientProps.getConsistentRead(),
                        subScan.getScanProps(), table);
            } else {
                reader = new DynamoScanRecordReader(credentials, client, endpoint, (DynamoSubScanSpec) scanSpec,
                        columns, clientProps.getConsistentRead(), subScan.getScanProps(), table);
            }
            if (key != null) {
                reader.setKeyMapper(key);
            }
            readers.add(reader);
        } catch (Exception e1) {
            throw new ExecutionSetupException(e1);
        }
    }
    return new ScanBatch(subScan, context, readers.iterator());
}

From source file:com.wealdtech.jackson.ObjectMapperFactory.java

/**
 * Build an objectmapper from a given configuration.
 *
 * @param configuration/*from   ww w .j a va2s. c  o m*/
 *          the objectmapper configuration
 * @return an objectmapper
 */
public ObjectMapper build(final ObjectMapperConfiguration configuration) {
    final ObjectMapper mapper = new ObjectMapper(configuration.getFactory().orNull());
    for (Module module : configuration.getModules()) {
        mapper.registerModule(module);
    }
    for (Map.Entry<JsonParser.Feature, Boolean> entry : configuration.getParserFeatures().entrySet()) {
        mapper.getFactory().configure(entry.getKey(), entry.getValue());
    }
    mapper.setInjectableValues(configuration.getInjectableValues());
    if (configuration.getPropertyNamingStrategy().isPresent()) {
        mapper.setPropertyNamingStrategy(configuration.getPropertyNamingStrategy().get());
    }
    if (configuration.getSerializationInclusion().isPresent()) {
        mapper.setSerializationInclusion(configuration.getSerializationInclusion().get());
    }

    return mapper;
}