GSON:编码对象图并在解码时保留类型

时间:2019-05-28 11:10:29

标签: java json gson deserialization json-deserialization

我有一个类似的课程:

public class MyClass {
    private final Map<Property, Object> properties;
} 

其中Propertyenum
假设属性包含2个元素,一个元素的值为Double,另一个元素的值为只有一个名为ownerName的属性的类实例。当我序列化此类时,我得到以下字符串:

{"properties":{"NAME":{"ownerName":"MyBucket"},"DIVISOR":33.0}}

问题是,当我尝试从上面的字符串中获取MyClass实例时,NAME属性的值将是Map,而不是具有{ {1}}属性。我尝试编写一个自定义ownerName,但仅对serializer/deserializer属性无法做到。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要为整个Map编写自定义反序列化器。自定义反序列化器可能如下所示:

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);
    }
}

用法示例:

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
}

上面的代码显示:

{
  "properties": {
    "NAME": {
      "ownerName": "MyBucket"
    },
    "DIVISOR": 33.0
  }
}

MyClass{properties={NAME=Owner{ownerName='MyBucket'}, DIVISOR=33.0}}