Reputation: 1260
I have a class like:
public class MyClass {
private final Map<Property, Object> properties;
}
where Property
is an enum
.
Let's say that properties contains 2 elements, one whose value is a Double
and one whose value is a class instance having only one attribute called ownerName
. When I serialise this class I get the following string:
{"properties":{"NAME":{"ownerName":"MyBucket"},"DIVISOR":33.0}}
The problem is that when I tried to obtain a MyClass
instance from the string above, the value for NAME
property will be a Map
instead of an instance of the class having ownerName
attribute. I tried to write a custom serializer/deserializer
but I was not able to do that only for NAME
property. Any ideas?
Upvotes: 1
Views: 338
Reputation: 38645
You need to write custom deserialiser for the whole Map
. Custom deserialiser could look like below:
class PropertyJsonDeserializer implements JsonDeserializer<Map<Property, Object>>, JsonSerializer<Map<Property, Object>> {
@Override
public Map<Property, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!json.isJsonObject()) {
return Collections.emptyMap();
}
JsonObject root = json.getAsJsonObject();
Map<Property, Object> result = new LinkedHashMap<>();
root.entrySet().forEach(entry -> {
Property property = Property.valueOf(entry.getKey());
switch (property) {
case DIVISOR:
result.put(property, entry.getValue().getAsDouble());
break;
case NAME:
Object owner = context.deserialize(entry.getValue(), Owner.class);
result.put(property, owner);
}
});
return result;
}
@Override
public JsonElement serialize(Map<Property, Object> src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src, Map.class);
}
}
Example usage:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.JsonAdapter;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class GsonApp {
public static void main(String[] args) throws Exception {
Map<Property, Object> properties = new EnumMap<>(Property.class);
properties.put(Property.DIVISOR, new BigDecimal("33.0"));
properties.put(Property.NAME, new Owner());
MyClass myClass = new MyClass(properties);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(myClass);
System.out.println(json);
myClass = gson.fromJson(json, MyClass.class);
System.out.println(myClass);
}
}
class MyClass {
@JsonAdapter(PropertyJsonDeserializer.class)
private final Map<Property, Object> properties;
public MyClass(Map<Property, Object> properties) {
this.properties = properties;
}
// getters, setters, toString
}
class Owner {
private String ownerName = "MyBucket";
// getters, setters, toString
}
enum Property {
NAME, DIVISOR
}
Above code prints:
{
"properties": {
"NAME": {
"ownerName": "MyBucket"
},
"DIVISOR": 33.0
}
}
MyClass{properties={NAME=Owner{ownerName='MyBucket'}, DIVISOR=33.0}}
Upvotes: 1