jackson-dataformats-text
jackson-dataformats-text copied to clipboard
TOML - PropertyNamingStrategies with Record, Jackson doesn't find fields
When attempting to deserialize a TOML file into a record with the ObjectMapper set to use the snake_case strategy, Jackson (2.13.4) fails to set the fields.
// Config.java
public record Config(int port, int gossipDelay, String initialValue, List<Peer> peers) {
public Config {
Objects.requireNonNull(peers);
Objects.requireNonNull(initialValue);
}
}
// Attempting to deserialize
ObjectMapper objectMapper = new TomlMapper()
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
objectMapper.readValue(new File("config.toml"), Config.class);
config.toml
port = 3200
gossip_delay = 10
initial_value = "I'm not sus"
peers = [
{ addr = "127.0.0.1", port = 3201 },
]
Logs when attempting to run this:
Exception in thread "main" com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `com.tomff.pheme.Config`, problem: `java.lang.NullPointerException`
at [Source: UNKNOWN; byte offset: #UNKNOWN]
at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:2047)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:587)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:610)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:293)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:288)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:202)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:519)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1405)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:352)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3494)
at com.tomff.pheme.PhemeMain.loadConfig(PhemeMain.java:64)
at com.tomff.pheme.PhemeMain.main(PhemeMain.java:69)
Caused by: java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:208)
at com.tomff.pheme.Config.<init>(Config.java:9)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:128)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:291)
... 11 more
Process finished with exit code 1
I think it is likely this is not specific to TOML module but would also occur with JSON.
probably same as https://github.com/FasterXML/jackson-databind/issues/3102 ?
Yes, duplicate of that one. If and when that gets fixed (requires a rewrite of property introspection, alas, but there's hope this could be done for 2.15) this would also be resolved. TOML module itself does not implement functionality, it just exposes token streams.
closing as fixed by https://github.com/FasterXML/jackson-databind/issues/3102