Search in sources :

Example 1 with PropertyNamingStrategy

use of com.fasterxml.jackson.databind.PropertyNamingStrategy in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilderTests method propertyNamingStrategy.

@Test
void propertyNamingStrategy() {
    PropertyNamingStrategy strategy = new PropertyNamingStrategy.SnakeCaseStrategy();
    ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().propertyNamingStrategy(strategy).build();
    assertThat(objectMapper.getSerializationConfig().getPropertyNamingStrategy()).isSameAs(strategy);
    assertThat(objectMapper.getDeserializationConfig().getPropertyNamingStrategy()).isSameAs(strategy);
}
Also used : PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with PropertyNamingStrategy

use of com.fasterxml.jackson.databind.PropertyNamingStrategy in project alien4cloud by alien4cloud.

the class PropertyConstraintDeserializer method deserialize.

@Override
public PropertyConstraint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    ObjectNode node = mapper.readTree(jp);
    String constraintName = null;
    Iterator<String> fieldIterator = node.fieldNames();
    // First field is also the constraint name ?
    if (fieldIterator.hasNext()) {
        constraintName = fieldIterator.next();
    } else {
        throw JsonMappingException.from(jp, "Constraint definition must contain one field");
    }
    PropertyNamingStrategy namingStrategy = mapper.getDeserializationConfig().getPropertyNamingStrategy();
    Map<String, Class<? extends PropertyConstraint>> constraintsMapping = getTranslatedConstraintsMap(namingStrategy);
    if (!constraintsMapping.containsKey(constraintName)) {
        if ("rangeMinValue".equals(constraintName) || "rangeMaxValue".equals(constraintName)) {
            return mapper.treeToValue(node, InRangeConstraint.class);
        } else {
            throw JsonMappingException.from(jp, "Constraint not found [" + constraintName + "], expect one of [" + this.constraints.keySet() + "]");
        }
    }
    Class<? extends PropertyConstraint> constraintClass = constraintsMapping.get(constraintName);
    return mapper.treeToValue(node, constraintClass);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with PropertyNamingStrategy

use of com.fasterxml.jackson.databind.PropertyNamingStrategy in project spring-framework by spring-projects.

the class Jackson2ObjectMapperFactoryBeanTests method propertyNamingStrategy.

@Test
public void propertyNamingStrategy() {
    PropertyNamingStrategy strategy = new PropertyNamingStrategy.SnakeCaseStrategy();
    this.factory.setPropertyNamingStrategy(strategy);
    this.factory.afterPropertiesSet();
    assertThat(this.factory.getObject().getSerializationConfig().getPropertyNamingStrategy()).isSameAs(strategy);
    assertThat(this.factory.getObject().getDeserializationConfig().getPropertyNamingStrategy()).isSameAs(strategy);
}
Also used : PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Test (org.junit.jupiter.api.Test)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)1