问题
How can I serialize a TreeSet properly? In order to give you an idea of what's not working I've set up this little demo project. The main goal is to print a JSON string of my QData object.
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;
}
}
The output: {"locations":[{"buabList":[{},{},{},{},{}]}]}
Expected output would be something like: {"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"}]}]}
Do you might know why GSON is not working as I'd expect it to work?
Thanks for any help, highly appreciated!
回答1:
The problem you are running into has nothing to do with the TreeSet
, but rather with the fact that GSON does not know how to serialize a map Entry
in the way that you would like. You therefore need to write a custom serializer for it, which looks something like this:
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;
}
}
When you create the Gson
object, you then need to register this custom serializer:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Entry.class, new EntrySerializer())
.create();
You can read more about custom serializers and deserializers in the GSON documentation.
来源:https://stackoverflow.com/questions/32945979/gson-serialize-object-with-java-util-treeset