Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#writerFor()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#writerFor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 6 votes vote down vote up
public static JettyReactiveHttpClient jettyClient(
		MethodMetadata methodMetadata,
		HttpClient httpClient,
		JsonFactory jsonFactory, ObjectMapper objectMapper) {

	final Type returnType = methodMetadata.returnType();
	Class returnPublisherType = (Class)((ParameterizedType) returnType).getRawType();
	Class returnActualType = getClass(resolveLastTypeParameter(returnType, returnPublisherType));
	Class bodyActualType = getClass(getBodyActualType(methodMetadata.bodyType()));

	return new JettyReactiveHttpClient(httpClient,
			bodyActualType, returnPublisherType, returnActualType,
			jsonFactory,
			objectMapper.writerFor(bodyActualType),
			objectMapper.readerFor(returnActualType));
}
 
Example 2
Source File: DocumentTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void checkJsonApiServerInfoSerialized() throws IOException {
	ObjectMapper objectMapper = new ObjectMapper();
	ObjectWriter writer = objectMapper.writerFor(Document.class);

	ObjectNode info = (ObjectNode) objectMapper.readTree("{\"a\" : \"b\"}");
	Document document = new Document();
	document.setJsonapi(info);
	Assert.assertSame(info, document.getJsonapi());

	String json = writer.writeValueAsString(document);
	Assert.assertEquals("{\"jsonapi\":{\"a\":\"b\"}}", json);
}
 
Example 3
Source File: JacksonCodecRegistry.java    From immutables with Apache License 2.0 5 votes vote down vote up
JacksonCodec(Class<T> clazz, ObjectMapper mapper) {
  this.clazz = Objects.requireNonNull(clazz, "clazz");
  Objects.requireNonNull(mapper, "mapper");
  this.reader = mapper.readerFor(clazz);
  this.writer = mapper.writerFor(clazz);
  this.ioContext =  new IOContext(new BufferRecycler(), null, false);
}
 
Example 4
Source File: ConfiguredCacheWriter.java    From data-prep with Apache License 2.0 5 votes vote down vote up
public void write(final ContentCacheKey key, final Object object) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectWriter objectWriter = mapper.writerFor(object.getClass());
    try (final OutputStream output = contentCache.put(key, ttl)) {
        objectWriter.writeValue(output, object);
        LOGGER.debug("New metadata cache entry -> {}.", key.getKey());
    }
}
 
Example 5
Source File: V2_35_2__Update_data_sync_job_parameters_with_system_setting_value.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public V2_35_2__Update_data_sync_job_parameters_with_system_setting_value()
{
    ObjectMapper mapper = new ObjectMapper();
    mapper.activateDefaultTyping( BasicPolymorphicTypeValidator.builder().allowIfBaseType( JobParameters.class ).build() );
    mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );

    JavaType resultingJavaType = mapper.getTypeFactory().constructType( JobParameters.class );
    reader = mapper.readerFor( resultingJavaType );
    writer = mapper.writerFor( resultingJavaType );
}
 
Example 6
Source File: BufferedBulkOperations.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@code com.fasterxml.jackson.databind.ObjectWriter} to serialize {@link BufferedIndex} instances
 */
protected ObjectWriter configuredWriter() {

    ObjectMapper objectMapper = new ExtendedObjectMapper(new JsonFactory())
            .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
            .configure(SerializationFeature.CLOSE_CLOSEABLE, false)
            .addMixIn(BufferedIndex.class, BulkableActionMixIn.class);

    for (JacksonMixIn mixIn: mixIns) {
        objectMapper.addMixIn(mixIn.getTargetClass(), mixIn.getMixInClass());
    }

    return objectMapper.writerFor(BufferedIndex.class);
}
 
Example 7
Source File: DocumentTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void checkJsonApiServerInfoNotSerializedIfNull() throws JsonProcessingException {
	Document document = new Document();
	document.setJsonapi(null);
	Assert.assertNull(document.getJsonapi());
	ObjectMapper objectMapper = new ObjectMapper();
	ObjectWriter writer = objectMapper.writerFor(Document.class);
	String json = writer.writeValueAsString(document);
	Assert.assertEquals("{}", json);
}
 
Example 8
Source File: DefaultPagedMetaInformationTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void nonNullMustBeSerialized() throws JsonProcessingException {
	ObjectMapper mapper = new ObjectMapper();
	ObjectWriter writer = mapper.writerFor(DefaultPagedMetaInformation.class);

	DefaultPagedMetaInformation metaInformation = new DefaultPagedMetaInformation();
	metaInformation.setTotalResourceCount(12L);

	String json = writer.writeValueAsString(metaInformation);
	Assert.assertEquals("{\"totalResourceCount\":12}", json);
}
 
Example 9
Source File: DefaultPagedMetaInformationTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void nullMustNotBeSerialized() throws JsonProcessingException {
	ObjectMapper mapper = new ObjectMapper();
	ObjectWriter writer = mapper.writerFor(DefaultPagedMetaInformation.class);

	DefaultPagedMetaInformation metaInformation = new DefaultPagedMetaInformation();
	String json = writer.writeValueAsString(metaInformation);
	Assert.assertEquals("{}", json);
}
 
Example 10
Source File: AmazonLambdaRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void setHandlerClass(Class<? extends RequestHandler<?, ?>> handler, BeanContainer container) {
    handlerClass = handler;
    beanContainer = container;
    ObjectMapper objectMapper = AmazonLambdaMapperRecorder.objectMapper;
    Method handlerMethod = discoverHandlerMethod(handlerClass);
    objectReader = objectMapper.readerFor(handlerMethod.getParameterTypes()[0]);
    objectWriter = objectMapper.writerFor(handlerMethod.getReturnType());
}
 
Example 11
Source File: FunqyLambdaBindingRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void init(BeanContainer bc) {
    beanContainer = bc;
    FunctionConstructor.CONTAINER = bc;
    ObjectMapper objectMapper = AmazonLambdaMapperRecorder.objectMapper;
    for (FunctionInvoker invoker : FunctionRecorder.registry.invokers()) {
        if (invoker.hasInput()) {
            ObjectReader reader = objectMapper.readerFor(invoker.getInputType());
            invoker.getBindingContext().put(ObjectReader.class.getName(), reader);
        }
        if (invoker.hasOutput()) {
            ObjectWriter writer = objectMapper.writerFor(invoker.getOutputType());
            invoker.getBindingContext().put(ObjectWriter.class.getName(), writer);
        }
    }
}
 
Example 12
Source File: ControlTunnel.java    From Bats with Apache License 2.0 5 votes vote down vote up
public JacksonSerDe(Class<MSG> clazz, JsonSerializer<MSG> serializer, JsonDeserializer<MSG> deserializer) {
  ObjectMapper mapper = new ObjectMapper();
  SimpleModule module = new SimpleModule();
  mapper.registerModule(module);
  module.addSerializer(clazz, serializer);
  module.addDeserializer(clazz, deserializer);
  writer = mapper.writerFor(clazz);
  reader = mapper.readerFor(clazz);
}
 
Example 13
Source File: ControlTunnel.java    From Bats with Apache License 2.0 4 votes vote down vote up
public JacksonSerDe(Class<MSG> clazz) {
  ObjectMapper mapper = new ObjectMapper();
  writer = mapper.writerFor(clazz);
  reader = mapper.readerFor(clazz);
}
 
Example 14
Source File: JacksonUtils.java    From native-navigation with MIT License 4 votes vote down vote up
public static ObjectWriter writerForType(ObjectMapper mapper, Type type) {
  JavaType javaType = mapper.getTypeFactory().constructType(type);
  return mapper.writerFor(javaType);
}
 
Example 15
Source File: V2_34_7__Convert_push_analysis_job_parameters_into_list_of_string.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void migrate( Context context ) throws Exception
{
    String pushAnalysisUid = null;

    try ( Statement statement = context.getConnection().createStatement() )
    {
        ResultSet resultSet = statement.executeQuery( "select jsonbjobparameters->1->'pushAnalysis' from public.jobconfiguration where jobtype = '" +
            JobType.PUSH_ANALYSIS.name() + "';" );

        if ( resultSet.next() )
        {
            pushAnalysisUid = resultSet.getString( 1 );
            pushAnalysisUid = StringUtils.strip( pushAnalysisUid, "\"" );
        }
    }

    if ( pushAnalysisUid != null )
    {
        ObjectMapper mapper = new ObjectMapper();
        mapper.activateDefaultTyping( BasicPolymorphicTypeValidator.builder().allowIfBaseType( JobParameters.class ).build() );
        mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );

        JavaType resultingJavaType = mapper.getTypeFactory().constructType( JobParameters.class );
        ObjectWriter writer = mapper.writerFor( resultingJavaType );

        try ( PreparedStatement ps = context.getConnection().prepareStatement( "UPDATE jobconfiguration SET jsonbjobparameters = ? where  jobtype = ?;" ) )
        {
            PushAnalysisJobParameters jobParameters = new PushAnalysisJobParameters( pushAnalysisUid );

            PGobject pg = new PGobject();
            pg.setType( "jsonb" );
            pg.setValue( writer.writeValueAsString( jobParameters ) );

            ps.setObject( 1, pg );
            ps.setString( 2, JobType.PUSH_ANALYSIS.name() );

            ps.execute();

            log.info( "JobType " + JobType.PUSH_ANALYSIS.name() + " has been updated." );
        }
    }
}
 
Example 16
Source File: JacksonModelSerializer.java    From curator with Apache License 2.0 4 votes vote down vote up
public JacksonModelSerializer(ObjectMapper mapper, JavaType type)
{
    reader = mapper.readerFor(type);
    writer = mapper.writerFor(type);
}
 
Example 17
Source File: JacksonModelSerializer.java    From curator with Apache License 2.0 4 votes vote down vote up
public JacksonModelSerializer(ObjectMapper mapper, TypeReference type)
{
    reader = mapper.readerFor(type);
    writer = mapper.writerFor(type);
}