2

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.

Magus
  • 14,796
  • 3
  • 36
  • 51

1 Answers1

2

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

Community
  • 1
  • 1
Ross Taylor-Turner
  • 3,687
  • 2
  • 24
  • 33