【问题标题】:GSON: Serialize object with java.util.TreeSetGSON:使用 java.util.TreeSet 序列化对象
【发布时间】:2015-10-05 10:08:08
【问题描述】:

如何正确序列化 TreeSet?为了让您了解什么不起作用,我设置了这个小演示项目。主要目标是打印我的 QData 对象的 JSON 字符串。

App.java

package de.company.gsonserializer;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

public class App 
{
    public static void main( String[] args )
    {
        QData qdata = new QData();

        ArrayList<LData> arrayList = new ArrayList<LData>(1);

        LData l = new LData();
        Map<String, String> unsortedBuabList = new HashMap<String, String>();
        for (int i = 0; i < 5; i++) {
            unsortedBuabList.put("Key-" + i, "Value" + i);
        }
        SortedSet<Map.Entry<String, String>> sortedBuabList = new TreeSet<Map.Entry<String, String>>(
                new Comparator<Map.Entry<String, String>>() {
                    public int compare(Map.Entry<String, String> e1, Map.Entry<String, String> e2) {
                        return e1.getValue().compareTo(e2.getValue());
                    }
                });
        sortedBuabList.addAll(unsortedBuabList.entrySet());
        l.setBuabList(sortedBuabList);

        arrayList.add(l);
        qdata.setLocations(arrayList);

        System.out.println( qdata.toString() );
    }
}

QData.java

package de.it2media.gsonserializer;

import java.util.ArrayList;

import com.google.gson.Gson;

public class QData {

    private ArrayList<LData> locations = new ArrayList<LData>(0);

    public ArrayList<LData> getLocations() {
        return locations;
    }

    public void setLocations(ArrayList<LData> locations) {
        this.locations = locations;
    }

    @Override
    public String toString(){
        Gson gson = new Gson();
        String thisObj = gson.toJson(this);
        return thisObj;     
    }

}

LData.java

package de.it2media.gsonserializer;

import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Map.Entry;

public class LData {

    private SortedSet<Entry<String, String>> buabList = new TreeSet<Map.Entry<String, String>>();

    public SortedSet<Entry<String, String>> getBuabList() {
        return buabList;
    }

    public void setBuabList(SortedSet<Entry<String, String>> buabList) {
        this.buabList = buabList;
    }

}

输出:{"locations":[{"buabList":[{},{},{},{},{}]}]}

预期输出类似于:{"locations":[{"buabList":[{"key":"Key-0","value":"Value0"},{"key":"Key-1","value":"Value1"},{"key":"Key-2","value":"Value2"},{"key":"Key-3","value":"Value3"},{"key":"Key-4","value":"Value4"}]}]}

您可能知道为什么 GSON 没有像我预期的那样工作吗?

感谢您的帮助,非常感谢!

【问题讨论】:

    标签: java json gson treeset sortedset


    【解决方案1】:

    您遇到的问题与TreeSet 无关,而是与GSON 不知道如何以您想要的方式序列化地图Entry 的事实有关。因此,您需要为其编写一个自定义序列化程序,如下所示:

    public static class EntrySerializer implements JsonSerializer<Entry<String, String>> {
        @Override
        public JsonElement serialize(Entry<String, String> entry, Type typeOfSrc, JsonSerializationContext context) {
            JsonElement serializedKey = context.serialize(entry.getKey());
            JsonElement serializedValue = context.serialize(entry.getValue());
    
            JsonObject jsonObject = new JsonObject();
            jsonObject.add("key", serializedKey);
            jsonObject.add("value", serializedValue);
            return jsonObject;
        }
    }
    

    当您创建Gson 对象时,您需要注册此自定义序列化程序:

    Gson gson = new GsonBuilder()
            .registerTypeAdapter(Entry.class, new EntrySerializer())
            .create();
    

    您可以在GSON documentation 中阅读有关自定义序列化程序和反序列化程序的更多信息。

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多