FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

@JacksonInject useInput property not work #153

Closed starskys closed 5 years ago

starskys commented 5 years ago
public class Writer {
    private Integer id;
    @JacksonInject(useInput = OptBoolean.TRUE)
    private String name="json";

    public static void main(String[] args) throws IOException {
        String jsonData = "{\"id\":111}";
        ObjectMapper mapper = new ObjectMapper();
        InjectableValues.Std injectableValues = new InjectableValues.Std();
        injectableValues.addValue(String.class, "Default Value");
        mapper.setInjectableValues(injectableValues);
        Writer writer = mapper.readValue(jsonData, Writer.class);
        System.out.println(writer);
    }
 ....get set....
}

After deserializing the value of "name" property is always "Default Value",whether OptBoolean is TRUE or FALSE ,so what does "OptBoolean" means? And how to use it?
version: 2.9.8 jdk version:1.8

cowtowncoder commented 5 years ago

"User input" refers to incoming JSON content, and in this example case JSON only contains value of id, not name. Given this the injected value is always used.

Question here is: what are you trying to achieve?

starskys commented 5 years ago

"User input" refers to incoming JSON content, and in this example case JSON only contains value of id, not name. Given this the injected value is always used.

Question here is: what are you trying to achieve?

I just want know how to use "Use input",I've searched many examples but none of them show the usage about that . I also tried that

public class Writer {
    private Integer id;
    @JacksonInject(useInput = OptBoolean.FALSE)
    private String name = "json";

    public static void main(String[] args) throws IOException {
        String jsonData = "{\"id\":111,\"name\":\"tom\"}";
        ObjectMapper mapper = new ObjectMapper();
        InjectableValues.Std injectableValues = new InjectableValues.Std();
        injectableValues.addValue(String.class, "Default Value");
        mapper.setInjectableValues(injectableValues);
        Writer writer = mapper.readValue(jsonData, Writer.class);
        System.out.println(writer);
    }
...get....set 
}

and the "name" is always "tom",whether OptBoolean is TRUE or FALSE. So I'm really confused about "useInput". Can you give a right example ?

cowtowncoder commented 5 years ago

@starskys I still do not understand what you are trying to do. At this point best you can do is to have a look at one unit test that uses this setting:

src/test/java/com/fasterxml/jackson/failing/SkipInjectableIntrospection962Test.java

if you can not explain what you hope to gain by using this setting. If you have no specific reason to use it, don't use it.