简体   繁体   中英

Jackson serialization and @JsonValue : back to the default behavior

I have a simple POJO :

public class Test {

    private String id;
    private boolean asString;

    // getters and setters

    @JsonValue
    public Object getValue() {
        if (asString) {
            return id;
        }

        // I want to use the default behavior of Jackson but i don't know what to do here
    }
}

As you can see, i'm trying to use @JsonValue to make the following behavior :

  • If asString is true , i want to serialize my object as a simple string.
  • If asString is false , i want to serialize my object with the default behavior of Jackson. But i don't know how to do this.

I also tried to make a custom serializer, but i can't reach the "default serializer" of Jackson and call it.

How can i achieve this ? I want to serialize my object as a string on a certain condition but i want to back on the default behavior of Jackson if i want to.

You could write a custom deserializer for this class, which inspects asString and if it is false, itself calls the default jackson deserializer.

There's a good answer on how to do this at How do I call the default deserializer from a custom deserializer in Jackson

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