`@JacksonInject` will try to fetch deserializer for injected creator property if there is visible field
See original GitHub issueEverything works as expected if you remove
.withVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
or
mapper.setDefaultSetterInfo(JsonSetter.Value.construct(Nulls.AS_EMPTY, Nulls.AS_EMPTY));
Otherwise error thrown: Cannot create empty instance of [simple type, class dk.xakeps.jacksontest.Test$Internal], no default Creator
jackson-databind 2.9.9.3 Test class:
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setDefaultSetterInfo(JsonSetter.Value.construct(Nulls.AS_EMPTY, Nulls.AS_EMPTY));
mapper.setVisibility(mapper.getVisibilityChecker().withVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY));
TestCase o = mapper.reader(new InjectableValues.Std().addValue(Internal.class, new Internal("test")))
.forType(TestCase.class)
.readValue("{\"id\":3}");
System.out.println(o.str);
}
public static final class TestCase {
private final Internal str;
private final int id;
@JsonCreator
public TestCase(@JacksonInject Internal str, @JsonProperty("id") int id) {
this.str = str;
this.id = id;
}
}
public static final class Internal {
private final String val;
public Internal(String val) {
this.val = val;
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
java - Injecting ObjectMapper into a deserialized object using ...
I've been trying to get an ObjectMapper injected into a Jackson-deserialized object using @JacksonInject and @JsonCreator, but to no avail.
Read more >Jackson Annotation Examples - Baeldung
In this tutorial, we'll do a deep dive into Jackson Annotations. ... It's very useful when we need to deserialize some JSON that...
Read more >Using @JacksonInject to inject values during deserialization
@JacksonInject annotation is used to indicate that value of annotated property will be injected during deserialization. This is useful if we ...
Read more >Jackson Annotations for JSON - Spring Framework Guru
You can also use @JsonProperty annotation during deserialization when the property names of the JSON and the field names of the Java object...
Read more >Index (Jackson-annotations 2.13.0 API) - FasterXML
Method called to check whether this generator instance can be used for ... any of the properties results in `true`, and names of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found this issue on my own (see the now-closed #3124), and here’s a smaller test, if you want that:
I used some parts of what I found in #3124, and the existing test case you had.
One sidenote: Jackson 3.0 does not seem to fail for the test, for whatever reason.