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}}
On Wednesday, 24 January 2018 05:12:13 UTC+8, Tatu Saloranta wrote:
>
> On Tue, Jan 23, 2018 at 2:20 AM, 黄浪 <[email protected] <javascript:>>
> 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 +-
>
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.