How to use JsonMerge with required fields?

625 views
Skip to first unread message

黄浪

unread,
Jan 23, 2018, 10:39:02 AM1/23/18
to jackson-user
As far as I know,  required in JsonProperty(value="xxx", required=true) only works in JsonCreator.

But JsonMerge is not compatible with JsonCreator.

Now I want to do with required fields and deep merge for object updating, is there a way to satisfy both?

Tatu Saloranta

unread,
Jan 23, 2018, 4:12:13 PM1/23/18
to jackson-user
On Tue, Jan 23, 2018 at 2:20 AM, 黄浪 <res.w...@gmail.com> wrote:
> As far as I know, required in JsonProperty(value="xxx", required=true) only
> works in JsonCreator.
>
> But JsonMerge is not compatible with JsonCreator.

Correct: merge is meant for updating existing objects, and creators
are for creating new objects.

> Now I want to do with required fields and deep merge for object updating, is
> there a way to satisfy both?

If you mean that you would want to verify that even in merging case,
certain property is required for any update,
that is not supported. In future (3.x) required-checks may be
implemented for setters/fields as well, but even then
implementation with merge might be tricky... and it's complicated by
the fact that some use cases could prefer not
to apply requirements on merge case (to allow partial updates).

Your best bet as is would probably be to implement merge part
manually, within setter. Setter on existing
POJO would know its own state, be able to update it based on incoming
value created by Jackson.

-+ Tatu +-

黄浪

unread,
Jan 24, 2018, 3:41:25 AM1/24/18
to jackson-user
Thans for reply.

Maybe my description is not clear enough.

What I exactly need is,

When building a new Java object, I use the JsonCreator with some required fields.

And then I do partial update with JsonMerge annotation.

I have done some tests and found that: 

    without JsonCreator annation to build Java Object, partial update works fine.
    but when I use JsonMerge with JsonCreator, JsonMerge is not working, when I do partial update, the other fields became null in the pojo object.

I have no idea if it's a bug or is designed like this.

My test:
User.java
public class User {
@JsonMerge
private Address address;

public User() {

}

@JsonCreator
public User(@JsonProperty(value = "address", required = true) Address address) {
this.address = address;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public void updateFromJson(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.readerForUpdating(this).readValue(json);
}
}

Address.java
public class Address {
private String street;
private Integer code;

public Address() {

}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}
}

and main
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();

Address address = new Address();
address.setStreet("street1");
address.setCode(1111);
User user = new User();
user.setAddress(address);

JSONObject ajson = new JSONObject();
ajson.put("street", "street2");
JSONObject ujson = new JSONObject();
ujson.put("address", ajson);

System.out.println("user: " + mapper.writeValueAsString(user));
user.updateFromJson(ujson.toString());
System.out.println("------------------------------ after update");
System.out.println("user: " + mapper.writeValueAsString(user));
}

with JsonCreator output:
user: {"address":{"street":"street1","code":1111}}
------------------------------ after update
user: {"address":{"street":"street2","code":null}}

comment JsonCreator constructor output:
user: {"address":{"street":"street1","code":1111}}
------------------------------ after update
user: {"address":{"street":"street2","code":1111}}
Reply all
Reply to author
Forward
0 new messages