简体   繁体   中英

Passing String value to Jackson Custom Deserializer in Java

I have created a Jackson Custom Deserializer to deserialize a JSON string :

public class TestMapper extends StdDeserializer<Test> {

    public TestMapper() {
        this(null);
    }

    public TestMapper(Class<?> vc) {
        super(vc);
    }

    @Override
    public Test deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

I want to pass a "String argument" to the deserialize method that I want to use during deserialization. Is there a way to do that?

I'm calling the deserializer as follows in my code:

new ObjectMapper().readValue(json, Test.class)

and the Test Class is :

@JsonDeserialize(using = TestMapper.class)
public class Test {

You need to create constructor which takes your extra argument which will be used during deserialisation:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        SimpleModule customModule = new SimpleModule();
        customModule.addDeserializer(Test.class, new TestMapper("Extra value!!!"));

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(customModule);

        Test test = new Test();
        test.setValue("Value");

        String json = mapper.writeValueAsString(test);
        System.out.println(json);
        System.out.println(mapper.readValue(json, Test.class));
    }
}

class TestMapper extends StdDeserializer<Test> {

    private String extraConfig;

    public TestMapper() {
        this(null);
    }

    public TestMapper(String extraConfig) {
        super(Test.class);
        this.extraConfig = extraConfig;
    }

    @Override
    public Test deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        Test test = new Test();
        test.setValue(extraConfig);

        return test;
    }
}

class Test {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Test{" +
                "value='" + value + '\'' +
                '}';
    }
}

Above code prints:

{"value":"Value"}
Test{value='Extra value!!!'}

You should always provide to super constructor your POJO class , for example, Test.class . If you need more complex initialisation, take a look on ContextualDeserializer .

Also, take a look:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM