I have a User object with this structure:
class User {
private String id;
private String name;
private Map<String, Object> properties;
// GETTERS & SETTERS
}
I have a JSON String with this structure:
{
"user": {
"id:"123456789",
"name:"azerty",
"emailHash":"123456789", // not used in User class
"properties": {
"p1":1,
"p2":"test",
"p3":[1, 2, 3, 4],
"p4":{
etc...
}
}
}
}
Properties' keys are String, Properties' values can be a String, int, Array, boolean, Map etc.
I try to deserialize this JSON string with Gson like that:
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonString);
JsonObject object = element.getAsJsonObject();
GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
Gson gson = builder.create();
User user = (User) gson.fromJson(object.get("user"), new TypeToken<User>() {}.getType());
Fields 'id' and 'name' are correctly injected but the field 'properties' stays null.
Do you know what I'm doing wrong? Thanks in advance for your help!
When we call the fromJson API on this Gson object, the parser invokes the custom deserializer and returns the desired Map instance: String jsonString = "{'Bob': '2017-06-01', 'Jennie':'2015-01-03'}"; Type type = new TypeToken<Map<String, Date>>(){}. getType(); Gson gson = new GsonBuilder() .
Deserialization – Read JSON using Gson. Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished ...
Both Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.
For me this code:
public class Main {
public static void main(String[] args) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
Gson gson = builder.create();
FileInputStream inputStream = new FileInputStream(new File("bobi.json"));
InputStreamReader reader = new InputStreamReader(inputStream);
User user = gson.fromJson(reader, User.class);
System.out.println(user.getName());
System.out.println(user.getId());
for (String property : user.getProperties().keySet()) {
System.out.println("Key: " + property + " value: " + user.getProperties().get(property));
}
reader.close();
}
Prints this:
azerty
123456789
Key: p1 value: 1.0
Key: p2 value: test
Key: p3 value: [1.0, 2.0, 3.0, 4.0]
Key: p4 value: {}
However, keep in mind that I have stripped the wrapping json object in the file I parse. The file is:
{
"id":"123456789",
"name" : "azerty",
"emailHash":"123456789",
"properties": {
"p1":1,
"p2":"test",
"p3":[1, 2, 3, 4],
"p4":{
}
}
}
Also I added closing double quote for name
and id
, which you did not have in your sample.
The User
class as requested by the OP. I have added getters and setters for the reason of printing:
import java.util.Map;
class User {
private String id;
private String name;
private Map<String, Object> properties;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Object> getProperties() {
return properties;
}
public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With