I have this class that I persist by creating a JSON object based on its annotation:
@Entity(name = "user")
public class User {
@Id
private String id;
@Column
private String name;
@Column
private Integer age;
public User() {}
public User(String id, String name, String age) {}
// ... code omitted
}
boolean ok = createEntity(new User("uid1", "eli", 25));
The method above will convert the User object into a Map respresenting a JSON object like:
map.put("ID", "uid1");
map.put("name", "eli");
map.put("age", 25);
This works fine. However I need to create a User object based on a response map containling values in similar form like the above, first I get the response from the server in form of JSON string:
{
"id" : "uid2",
"name" : "ben",
"age" : 20
}
I mean, if I parse this JSON string into a map:
map.put("id", "uid2");
map.put("name", "ben");
map.put("age", 20);
How can I construct a User object filling the values from the map to the correct @Column field?
Update:
I know about Jackson library, but the idea here is that I need to work with annotations as I am building a library that needs classes to be annotated. I am just after the way to create object from json/map and assign the right values to the right field
Use the Jackson library. It converts your objects to json without the need to annotate them.