lamplet
lamplet

Reputation: 103

Can I make all setters invisible but one using Jackson?

I have an old class with many fields to be converted from/to JSON. The setters are not pure, so I make all setters invisible.

objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);

But there's one setter that actually set two fields, I need to make it visible.

private String a;

private transient String b;

public void setA(String a)
{
    this.a = a;
    this.b = convertFrom(a);
}

All setters are public. How can I make all setters invisible, but only one setter visible?

Upvotes: 1

Views: 231

Answers (1)

user404
user404

Reputation: 2028

Suggested edit: as ExceptionHandler suggested, you can do ignore properties in two ways:

  1. Class level
  2. field level

As you don't want to do tedious field level task, you can do it by class level ignoring fields like below:

You can ignore all fields or fields specified using @JsonIgnoreProperties annotation at class level. Just don't mention the field/s for which you want to deserialize. Like this:

@JsonIgnoreProperties({ "field1", "field2", "field3" })
public class YourClass{
    private String field1;
    private String field2;
    private String field3;
   ...
}

Upvotes: 1

Related Questions