类com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator源码实例Demo

下面列出了怎么用com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator的API类实例代码及写法,或者点击链接到github查看源代码。

@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)));
}
 
源代码2 项目: ueboot   文件: RedisConfig.java
@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;
}
 
源代码3 项目: jackson-datatypes-collections   文件: TestRange.java
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());
}
 
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 );
}
 
@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));
}
 
@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)));
}
 
@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));
}
 
源代码8 项目: bowman   文件: RestOperationsFactoryTest.java
protected DummyTypeIdResolver() {
	super(SimpleType.constructUnsafe(Object.class), TypeFactory.defaultInstance(), BasicPolymorphicTypeValidator
		.builder().build());
}
 
@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." );
        }
    }
}
 
 同包方法