Question I want to config jackson Feature JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, I try to many ways:
1.src/main/resources/application.properties ,config:
spring.jackson.parser.allow_unquoted_field_names=true spring.jackson.parser.allow_single_quotes=true it does't work!
2.type config:
@Configuration public class JacksonMapperConfig {
@Bean
public MappingJackson2HttpMessageConverter convert() {
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return new MappingJackson2HttpMessageConverter(mapper);
}
} it does't work! or /* * @return xx / @Bean public ObjectMapper objectMapper() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.featuresToEnable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
return builder.build();
}
it does't work too! or @Bean public Jackson2ObjectMapperBuilder objectMapperBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.featuresToEnable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES); return builder; } it does't work too!
I have been desperate! who can tell me how to config jackson feature "spring.jackson.parser.allow_unquoted_field_names"!!!!
Comment From: snicoll
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: anandvarkeyphilips
Has anyone found how to do it?? SO didnt help me find it.. I came here by Google Search!
Comment From: fransflippo
Yeah, with all due respect, it's really hard to find Spring Boot answers for Spring Boot 2.0 on SO. And there's really no way to tell if the Spring Boot 1.x answers still apply or not.
I've got the same question. I've added spring.jackson.parser.ALLOW_NON_NUMERIC_NUMBERS=true
to my application.properties
, but if I inspect the ObjectMapper
's DeserializationConfig
, the _parserFeatures
value is 0 meaning my feature (whose mask value would be 2^9=512) is not enabled.
Comment From: bclozel
@fransflippo feel free to point us to an SO question and we can take a look.
Comment From: fransflippo
I've created a SO question: https://stackoverflow.com/questions/63899402/how-to-customize-jackson-objectmapper-to-allow-nan-in-spring-boot-2-0.
Actually, in my case I can see the feature I need is being picked up and applied by Spring Boot's Jackson auto configuration -- I was looking at ObjectMapper._deserializationConfig._parserFeatures
but it's on ObjectMapper._jsonFactory._parserFeatures
. Why Jackson still isn't parsing "NaN" is then a Jackson issue that I need to debug.
I've added some detail in the SO issue that will hopefully help other people landing here find a starting point to diagnose their issue.
Comment From: NicolaFusco91
If you're using Spring Boot, you can autowire the ObjectMapper:
@Configuration
public class JacksonConfig {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void configureObjectMapper() {
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
}
}
Comment From: wilkinsona
Thanks for the suggestion, @NicolaFusco91. Mutating a bean in @PostConstruct
isn't really recommended as components that consume and use it earlier won't see the change. A better approach is to use an application property or a JacksonObjectMapperBuilderCustomizer
. This will ensure that the configuration is applied to the ObjectMapper
before it's injected anywhere.