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

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

Introduction

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

Prototype

public void registerSubtypes(NamedType... types) 

Source Link

Document

Method for registering specified class as a subtype, so that typename-based resolution can link supertypes to subtypes (as an alternative to using annotations).

Usage

From source file:com.nextdoor.bender.config.BenderConfig.java

public static ObjectMapper getObjectMapper(String filename) {
    String extension = FilenameUtils.getExtension(filename);
    ObjectMapper mapper = null;
    switch (extension) {
    case "yaml":
        mapper = new ObjectMapper(new YAMLFactory());
        break;/*from w  w w  . j av a2  s .  co  m*/
    default:
        mapper = new ObjectMapper();
    }

    mapper.registerSubtypes(BenderConfig.subtypes);

    return mapper;
}

From source file:com.twitter.ambrose.util.JSONUtil.java

private static ObjectMapper newMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
    mapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
    mapper.disable(SerializationFeature.CLOSE_CLOSEABLE);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    Reflections reflections = new Reflections("com.twitter.ambrose");
    Set<Class<? extends Job>> jobSubTypes = reflections.getSubTypesOf(Job.class);
    mapper.registerSubtypes(jobSubTypes.toArray(new Class<?>[jobSubTypes.size()]));
    return mapper;
}

From source file:org.kordamp.javatrove.example01.impl.ObjectMapperProvider.java

@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerSubtypes(Repository.class);
    return objectMapper;
}

From source file:org.kordamp.javatrove.todo01.module.ObjectMapperProvider.java

@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerSubtypes(Todos.class);
    objectMapper.registerSubtypes(Todo.class);
    return objectMapper;
}

From source file:org.kordamp.javatrove.example04.AppConfig.java

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerSubtypes(Repository.class);
    return objectMapper;
}

From source file:org.kordamp.javatrove.chat03.impl.ObjectMapperProvider.java

@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
    objectMapper.registerSubtypes(Command.class);
    return objectMapper;
}

From source file:com.flipkart.foxtrot.core.querystore.impl.DistributedTableMetadataManagerTest.java

@Before
public void setUp() throws Exception {
    HazelcastConnection hazelcastConnection = Mockito.mock(HazelcastConnection.class);
    hazelcastInstance = new TestHazelcastInstanceFactory(1).newHazelcastInstance();
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerSubtypes(GroupResponse.class);
    when(hazelcastConnection.getHazelcast()).thenReturn(hazelcastInstance);
    Config config = new Config();
    when(hazelcastConnection.getHazelcastConfig()).thenReturn(config);

    //Create index for table meta. Not created automatically
    elasticsearchServer = new MockElasticsearchServer(UUID.randomUUID().toString());
    CreateIndexRequest createRequest = new CreateIndexRequest(TableMapStore.TABLE_META_INDEX);
    Settings indexSettings = ImmutableSettings.settingsBuilder().put("number_of_replicas", 0).build();
    createRequest.settings(indexSettings);
    elasticsearchServer.getClient().admin().indices().create(createRequest).actionGet();
    elasticsearchServer.getClient().admin().cluster().prepareHealth().setWaitForGreenStatus().execute()
            .actionGet();/*  w w w  .  j  a  v a 2s  .  c o m*/
    ElasticsearchConnection elasticsearchConnection = Mockito.mock(ElasticsearchConnection.class);
    when(elasticsearchConnection.getClient()).thenReturn(elasticsearchServer.getClient());
    ElasticsearchUtils.initializeMappings(elasticsearchServer.getClient());

    String DATA_MAP = "tablemetadatamap";
    tableDataStore = hazelcastInstance.getMap(DATA_MAP);
    distributedTableMetadataManager = new DistributedTableMetadataManager(hazelcastConnection,
            elasticsearchConnection);
    distributedTableMetadataManager.start();
}

From source file:org.elasticsoftware.elasticactors.base.serialization.ObjectMapperBuilder.java

private void registerSubtypes(Reflections reflections, ObjectMapper objectMapper) {
    Set<Class<?>> subTypes = reflections.getTypesAnnotatedWith(JsonTypeName.class);
    objectMapper.registerSubtypes(subTypes.toArray(new Class<?>[subTypes.size()]));
}

From source file:org.bonitasoft.web.designer.config.DesignerConfig.java

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    //By default all properties without explicit view definition are included in serialization.
    //To use JsonView we have to change this parameter
    objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

    //We don't have to serialize null values
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.registerModule(new JodaModule());
    objectMapper.registerSubtypes(jacksonSubTypes());

    //add Handler to migrate old json
    objectMapper.addHandler(new JacksonDeserializationProblemHandler());

    return objectMapper;
}

From source file:io.druid.indexing.common.task.AppenderatorDriverRealtimeIndexTaskTest.java

private void makeToolboxFactory(final File directory) {
    taskStorage = new HeapMemoryTaskStorage(new TaskStorageConfig(null));
    taskLockbox = new TaskLockbox(taskStorage);

    publishedSegments = new CopyOnWriteArrayList<>();

    ObjectMapper mapper = new DefaultObjectMapper();
    mapper.registerSubtypes(LinearShardSpec.class);
    mapper.registerSubtypes(NumberedShardSpec.class);
    IndexerSQLMetadataStorageCoordinator mdc = new IndexerSQLMetadataStorageCoordinator(mapper,
            derbyConnectorRule.metadataTablesConfigSupplier().get(), derbyConnectorRule.getConnector()) {
        @Override//from ww  w  .  j a v a 2  s. c o m
        public Set<DataSegment> announceHistoricalSegments(Set<DataSegment> segments) throws IOException {
            Set<DataSegment> result = super.announceHistoricalSegments(segments);

            Assert.assertFalse("Segment latch not initialized, did you forget to call expectPublishSegments?",
                    segmentLatch == null);

            publishedSegments.addAll(result);
            segments.forEach(s -> segmentLatch.countDown());

            return result;
        }

        @Override
        public SegmentPublishResult announceHistoricalSegments(Set<DataSegment> segments,
                DataSourceMetadata startMetadata, DataSourceMetadata endMetadata) throws IOException {
            SegmentPublishResult result = super.announceHistoricalSegments(segments, startMetadata,
                    endMetadata);

            Assert.assertFalse("Segment latch not initialized, did you forget to call expectPublishSegments?",
                    segmentLatch == null);

            publishedSegments.addAll(result.getSegments());
            result.getSegments().forEach(s -> segmentLatch.countDown());

            return result;
        }
    };
    final TaskConfig taskConfig = new TaskConfig(directory.getPath(), null, null, 50000, null, false, null,
            null);

    final TaskActionToolbox taskActionToolbox = new TaskActionToolbox(taskLockbox, mdc, emitter,
            EasyMock.createMock(SupervisorManager.class));
    final TaskActionClientFactory taskActionClientFactory = new LocalTaskActionClientFactory(taskStorage,
            taskActionToolbox);
    IntervalChunkingQueryRunnerDecorator queryRunnerDecorator = new IntervalChunkingQueryRunnerDecorator(null,
            null, null) {
        @Override
        public <T> QueryRunner<T> decorate(QueryRunner<T> delegate,
                QueryToolChest<T, ? extends Query<T>> toolChest) {
            return delegate;
        }
    };
    final QueryRunnerFactoryConglomerate conglomerate = new DefaultQueryRunnerFactoryConglomerate(
            ImmutableMap.<Class<? extends Query>, QueryRunnerFactory>of(TimeseriesQuery.class,
                    new TimeseriesQueryRunnerFactory(new TimeseriesQueryQueryToolChest(queryRunnerDecorator),
                            new TimeseriesQueryEngine(), new QueryWatcher() {
                                @Override
                                public void registerQuery(Query query, ListenableFuture future) {
                                    // do nothing
                                }
                            })));
    handOffCallbacks = new ConcurrentHashMap<>();
    final SegmentHandoffNotifierFactory handoffNotifierFactory = new SegmentHandoffNotifierFactory() {
        @Override
        public SegmentHandoffNotifier createSegmentHandoffNotifier(String dataSource) {
            return new SegmentHandoffNotifier() {
                @Override
                public boolean registerSegmentHandoffCallback(SegmentDescriptor descriptor, Executor exec,
                        Runnable handOffRunnable) {
                    handOffCallbacks.put(descriptor, new Pair<>(exec, handOffRunnable));
                    handoffLatch.countDown();
                    return true;
                }

                @Override
                public void start() {
                    //Noop
                }

                @Override
                public void close() {
                    //Noop
                }

            };
        }
    };
    final TestUtils testUtils = new TestUtils();
    rowIngestionMetersFactory = testUtils.getRowIngestionMetersFactory();
    SegmentLoaderConfig segmentLoaderConfig = new SegmentLoaderConfig() {
        @Override
        public List<StorageLocationConfig> getLocations() {
            return Lists.newArrayList();
        }
    };

    taskToolboxFactory = new TaskToolboxFactory(taskConfig, taskActionClientFactory, emitter,
            new TestDataSegmentPusher(), new TestDataSegmentKiller(), null, // DataSegmentMover
            null, // DataSegmentArchiver
            new TestDataSegmentAnnouncer(), EasyMock.createNiceMock(DataSegmentServerAnnouncer.class),
            handoffNotifierFactory, () -> conglomerate, MoreExecutors.sameThreadExecutor(), // queryExecutorService
            EasyMock.createMock(MonitorScheduler.class),
            new SegmentLoaderFactory(new SegmentLoaderLocalCacheManager(null, segmentLoaderConfig,
                    testUtils.getTestObjectMapper())),
            testUtils.getTestObjectMapper(), testUtils.getTestIndexIO(), MapCache.create(1024),
            new CacheConfig(), testUtils.getTestIndexMergerV9(),
            EasyMock.createNiceMock(DruidNodeAnnouncer.class), EasyMock.createNiceMock(DruidNode.class),
            new LookupNodeService("tier"), new DataNodeService("tier", 1000, ServerType.INDEXER_EXECUTOR, 0),
            new TaskReportFileWriter(reportsFile));
}