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

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#getSerializationConfig() . 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: JacksonResourceFieldInformationProvider.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
protected Optional<String> getName(Method method) {
	ObjectMapper objectMapper = context.getObjectMapper();
	SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
	if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
		String name = ClassUtils.getGetterFieldName(method);
		Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
		AnnotationMap annotationMap = buildAnnotationMap(declaredAnnotations);

		int paramsLength = method.getParameterAnnotations().length;
		AnnotationMap[] paramAnnotations = new AnnotationMap[paramsLength];
		for (int i = 0; i < paramsLength; i++) {
			AnnotationMap parameterAnnotationMap = buildAnnotationMap(method.getParameterAnnotations()[i]);
			paramAnnotations[i] = parameterAnnotationMap;
		}

		AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(method.getDeclaringClass(), serializationConfig);
		AnnotatedMethod annotatedField = AnnotatedMethodBuilder.build(annotatedClass, method, annotationMap, paramAnnotations);
		return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForGetterMethod(serializationConfig, annotatedField, name));
	}
	return Optional.empty();
}
 
Example 2
Source File: VirtualPropertiesWriterTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void overridenFixAccessIsNoop() {

    // given
    ObjectMapper objectMapper = new ObjectMapper();
    SerializationConfig config = objectMapper.getSerializationConfig();

    VirtualPropertiesWriter writer = spy(new VirtualPropertiesWriter(
            new VirtualProperty[0],
            mock(ValueResolver.class)
    ));

    // when
    writer.fixAccess(config);

    // then
    verify(writer, never()).getMember();

}
 
Example 3
Source File: JacksonResourceFieldInformationProvider.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
protected Optional<String> getName(Field field) {
	ObjectMapper objectMapper = context.getObjectMapper();
	SerializationConfig serializationConfig = objectMapper.getSerializationConfig();

	if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
		AnnotationMap annotationMap = buildAnnotationMap(field.getDeclaredAnnotations());

		AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(field.getDeclaringClass(), serializationConfig);
		AnnotatedField annotatedField = AnnotatedFieldBuilder.build(annotatedClass, field, annotationMap);
		return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForField(serializationConfig, annotatedField, field.getName()));
	}
	return Optional.empty();
}
 
Example 4
Source File: VirtualPropertiesWriterTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void withConfigReturnsConfiguredWriter() {

    // given
    ObjectMapper objectMapper = new ObjectMapper();
    SerializationConfig config = objectMapper.getSerializationConfig();

    VirtualPropertiesWriter writer = spy(new VirtualPropertiesWriter(
            new VirtualProperty[0],
            mock(ValueResolver.class),
            new VirtualPropertyFilter[0]
    ));

    JavaType javaType = config.constructType(LogEvent.class);
    AnnotatedClass annotatedClass = createTestAnnotatedClass(config, javaType);

    SimpleBeanPropertyDefinition simpleBeanPropertyDefinition =
            getTestBeanPropertyDefinition(config, javaType, annotatedClass);

    VirtualPropertiesWriter result = writer.withConfig(
            config,
            annotatedClass,
            simpleBeanPropertyDefinition,
            config.constructType(VirtualProperty.class)
    );

    // then
    assertArrayEquals(writer.virtualProperties, result.virtualProperties);
    assertEquals(writer.valueResolver, result.valueResolver);
    assertEquals(writer.filters, result.filters);

}
 
Example 5
Source File: VirtualPropertiesWriterTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void writerCreatedWithDeprecatedConstructorWritesGivenProperties() throws Exception {

    // given
    ObjectMapper objectMapper = new ObjectMapper();
    SerializationConfig config = objectMapper.getSerializationConfig();

    JavaType javaType = config.constructType(LogEvent.class);
    AnnotatedClass annotatedClass = createTestAnnotatedClass(config, javaType);

    SimpleBeanPropertyDefinition simpleBeanPropertyDefinition =
            getTestBeanPropertyDefinition(config, javaType, annotatedClass);

    String expectedName = UUID.randomUUID().toString();
    String expectedValue = UUID.randomUUID().toString();
    VirtualProperty virtualProperty = spy(createNonDynamicVirtualProperty(expectedName, expectedValue));

    ValueResolver valueResolver = createTestValueResolver(virtualProperty, expectedValue);

    VirtualPropertiesWriter writer = new VirtualPropertiesWriter(
            simpleBeanPropertyDefinition,
            new AnnotationCollector.OneAnnotation(
                    annotatedClass.getRawType(),
                    annotatedClass.getAnnotations().get(JsonAppend.class)
            ),
            javaType,
            new VirtualProperty[] { virtualProperty },
            valueResolver
    );

    JsonGenerator jsonGenerator = mock(JsonGenerator.class);

    // when
    writer.serializeAsField(new Object(), jsonGenerator, mock(SerializerProvider.class));

    // then
    verify(jsonGenerator).writeFieldName(eq(expectedName));
    verify(jsonGenerator).writeString(eq(expectedValue));

}
 
Example 6
Source File: KatharsisFeature.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean configure(final FeatureContext context) {
	ObjectMapper objectMapper = boot.getObjectMapper();
	ResourceFieldNameTransformer resourceFieldNameTransformer = new ResourceFieldNameTransformer(
			objectMapper.getSerializationConfig());

	PropertiesProvider propertiesProvider = new PropertiesProvider() {

		@Override
		public String getProperty(String key) {
			return (String) context.getConfiguration().getProperty(key);
		}
	};

	boot.setDefaultServiceUrlProvider(new UriInfoServiceUrlProvider());
	boot.setPropertiesProvider(propertiesProvider);
	boot.setResourceFieldNameTransformer(resourceFieldNameTransformer);
	boot.addModule(new JaxrsModule(securityContext));
	boot.boot();

	KatharsisFilter katharsisFilter;
	try {
		RequestContextParameterProviderRegistry parameterProviderRegistry = buildParameterProviderRegistry();

		String webPathPrefix = boot.getWebPathPrefix();
		ResourceRegistry resourceRegistry = boot.getResourceRegistry();
		RequestDispatcher requestDispatcher = boot.getRequestDispatcher();
		katharsisFilter = createKatharsisFilter(resourceRegistry, parameterProviderRegistry, webPathPrefix,
				requestDispatcher);
	}
	catch (Exception e) {
		throw new WebApplicationException(e);
	}
	context.register(katharsisFilter);
	
	registerActionRepositories(context, boot);

	return true;
}
 
Example 7
Source File: Jackson2Parser.java    From typescript-generator with MIT License 5 votes vote down vote up
private static TypeProcessor createSpecificTypeProcessor() {
    return new TypeProcessor.Chain(
            new ExcludingTypeProcessor(Arrays.asList(JsonNode.class.getName())),
            new TypeProcessor() {
                @Override
                public TypeProcessor.Result processType(Type javaType, TypeProcessor.Context context) {
                    if (context.getTypeContext() instanceof Jackson2TypeContext) {
                        final Jackson2TypeContext jackson2TypeContext = (Jackson2TypeContext) context.getTypeContext();
                        if (!jackson2TypeContext.disableObjectIdentityFeature) {
                            final Type resultType = jackson2TypeContext.parser.processIdentity(javaType, jackson2TypeContext.beanPropertyWriter);
                            if (resultType != null) {
                                return context.withTypeContext(null).processType(resultType);
                            }
                        }
                        // Map.Entry
                        final Class<?> rawClass = Utils.getRawClassOrNull(javaType);
                        if (rawClass != null && Map.Entry.class.isAssignableFrom(rawClass)) {
                            final ObjectMapper objectMapper = jackson2TypeContext.parser.objectMapper;
                            final SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
                            final BeanDescription beanDescription = serializationConfig
                                    .introspect(TypeFactory.defaultInstance().constructType(rawClass));
                            final JsonFormat.Value formatOverride = serializationConfig.getDefaultPropertyFormat(Map.Entry.class);
                            final JsonFormat.Value formatFromAnnotation = beanDescription.findExpectedFormat(null);
                            final JsonFormat.Value format = JsonFormat.Value.merge(formatFromAnnotation, formatOverride);
                            if (format.getShape() != JsonFormat.Shape.OBJECT) {
                                final Type mapType = Utils.replaceRawClassInType(javaType, Map.class);
                                return context.processType(mapType);
                            }
                        }
                    }
                    return null;
                }
            }
    );
}
 
Example 8
Source File: ResourceFieldNameTransformerTest.java    From katharsis-framework with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    objectMapper = new ObjectMapper();
    sut = new ResourceFieldNameTransformer(objectMapper.getSerializationConfig());
}
 
Example 9
Source File: Qzr.java    From qzr with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures that the {@link SerializationConfig} provided by our
 * {@link ObjectMapper} bean is used throughout the application.
 */
@Bean
public SerializationConfig serializationConfig(ObjectMapper objectMapper) {
    return objectMapper.getSerializationConfig();
}