com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator Java Examples

The following examples show how to use com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator. 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: MonetaryAmountDeserializerTest.java    From jackson-datatype-money with MIT License 6 votes vote down vote up
@ParameterizedTest
@MethodSource("data")
void shouldDeserializeWithTypeInformation(final Class<M> type, final Configurer configurer) throws IOException {
    final ObjectMapper unit = unit(configurer)
            .activateDefaultTyping(
                    BasicPolymorphicTypeValidator.builder().build(),
                    DefaultTyping.OBJECT_AND_NON_CONCRETE,
                    JsonTypeInfo.As.EXISTING_PROPERTY)
            .disable(FAIL_ON_UNKNOWN_PROPERTIES);

    final String content = "{\"type\":\"org.javamoney.moneta.Money\",\"amount\":29.95,\"currency\":\"EUR\"}";
    final M amount = unit.readValue(content, type);

    // type information is ignored?!
    assertThat(amount, is(instanceOf(type)));
}
 
Example #2
Source File: RedisConfig.java    From ueboot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(type = {"org.springframework.data.redis.core.RedisTemplate"})
public RedisTemplate<?, ?> redisTemplate(
        LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(),ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setConnectionFactory(redisConnectionFactory);
    template.afterPropertiesSet();
    return template;
}
 
Example #3
Source File: TestRange.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
public void testUntyped() throws Exception
{
    // Default settings do not allow possibly unsafe base type
    final ObjectMapper polyMapper = builderWithModule()
            .polymorphicTypeValidator(BasicPolymorphicTypeValidator
                    .builder()
                    .allowIfBaseType(Object.class)
                    .build()
            ).build();

    String json = polyMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new UntypedWrapper(RangeFactory.open(1, 10)));
    UntypedWrapper out = polyMapper.readValue(json, UntypedWrapper.class);
    assertNotNull(out);
    assertEquals(Range.class, out.range.getClass());
}
 
Example #4
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 #5
Source File: CurrencyUnitDeserializerTest.java    From jackson-datatype-money with MIT License 5 votes vote down vote up
@Test
void shouldDeserializeWithTyping() throws IOException {
    unit.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build());

    final CurrencyUnit actual = unit.readValue("\"EUR\"", CurrencyUnit.class);
    final CurrencyUnit expected = CurrencyUnitBuilder.of("EUR", "default").build();

    assertThat(actual, is(expected));
}
 
Example #6
Source File: MonetaryAmountDeserializerTest.java    From jackson-datatype-money with MIT License 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data")
void shouldDeserializeWithoutTypeInformation(final Class<M> type, final Configurer configurer) throws IOException {
    final ObjectMapper unit = unit(configurer).activateDefaultTyping(
            BasicPolymorphicTypeValidator.builder().build());

    final String content = "{\"amount\":29.95,\"currency\":\"EUR\"}";
    final M amount = unit.readValue(content, type);

    assertThat(amount, is(instanceOf(type)));
}
 
Example #7
Source File: MonetaryAmountSerializerTest.java    From jackson-datatype-money with MIT License 5 votes vote down vote up
@ParameterizedTest
@MethodSource("amounts")
void shouldSerializeWithType(final MonetaryAmount amount) throws JsonProcessingException {
    final ObjectMapper unit = unit(module()).activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build());

    final String expected = "{\"amount\":{\"amount\":29.95,\"currency\":\"EUR\"}}";
    final String actual = unit.writeValueAsString(new Price(amount));

    assertThat(actual, is(expected));
}
 
Example #8
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 4 votes vote down vote up
protected DummyTypeIdResolver() {
	super(SimpleType.constructUnsafe(Object.class), TypeFactory.defaultInstance(), BasicPolymorphicTypeValidator
		.builder().build());
}
 
Example #9
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." );
        }
    }
}