Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson : Updating (not creating new object) JavaObject from Json?

Tags:

java

jackson

OK I have a json say

userjson = { fname : "ABC", lname : "DEF" }

and a User Pojo Object

User {
 String id, 
 String email,
 String fname,
 String lname
}

now using the Jackson, I know how to create User instance from userjson, but how do i update existing User instance from userjson, because my user instance has some properties already set by some other Module.

For now what i am doing is converting userjson to userHasMap and then set all the values manually

userInstance.setFName(userHasMap.get('fname'))
userInstance.setLName(userHasMap.get('lname'))

which works fine, but I could have done some thing for converting userInstance to userjson when needed which would have made use of jackson-lib meaningless.

like image 477
LNT Avatar asked Feb 13 '23 19:02

LNT


1 Answers

Ok found the answer, http://jira.codehaus.org/browse/JACKSON-857 http://jira.codehaus.org/browse/JACKSON-824

mapper.readerForUpdating(object).readValue(json);
like image 162
LNT Avatar answered Feb 15 '23 09:02

LNT