Skip to content

Instantly share code, notes, and snippets.

@gythialy
Last active July 11, 2021 07:30
Show Gist options
  • Save gythialy/75e13e8d4594118809a3 to your computer and use it in GitHub Desktop.
Save gythialy/75e13e8d4594118809a3 to your computer and use it in GitHub Desktop.
Gson support Guava MutiMap and Table
package com.wescon.cv.utilities;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.nutz.castor.Castors;
import com.google.common.base.Supplier;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
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;
@SuppressWarnings("rawtypes")
public class Gsons {
static Gson gson;
static {
gson = new GsonBuilder().registerTypeAdapter(Multimap.class,
new JsonSerializer<Multimap>() {
public JsonElement serialize(Multimap multimap,
Type type,
JsonSerializationContext jsonSerializationContext) {
return jsonSerializationContext.serialize(multimap.asMap());
}
})
.registerTypeAdapter(Multimap.class,
new JsonDeserializer<Multimap>() {
public Multimap deserialize(JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
final SetMultimap<String, String> map = Multimaps.newSetMultimap(new HashMap<String, Collection<String>>(),
new Supplier<Set<String>>() {
public Set<String> get() {
return Sets.newHashSet();
}
});
for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) {
for (JsonElement element : (JsonArray) entry.getValue()) {
map.get(entry.getKey())
.add(element.getAsString());
}
}
return map;
}
})
.registerTypeAdapter(Table.class, new JsonDeserializer<Table>() {
@SuppressWarnings("unchecked")
@Override
public Table deserialize(JsonElement json,
Type type,
JsonDeserializationContext context)
throws JsonParseException {
Table table = HashBasedTable.create();
JsonObject object = (JsonObject) json;
JsonElement element = object.get("backingMap");
for (Map.Entry<String, JsonElement> entry : ((JsonObject) element).entrySet()) {
String row = entry.getKey();
for (Map.Entry<String, JsonElement> column : ((JsonObject) entry.getValue()).entrySet()) {
table.put(row, column.getKey(), column.getValue());
}
}
return table;
}
})
.registerTypeAdapter(BiMap.class, new JsonDeserializer<BiMap>() {
@SuppressWarnings("unchecked")
@Override
public BiMap deserialize(JsonElement json,
Type type,
JsonDeserializationContext context)
throws JsonParseException {
BiMap mapping = HashBiMap.create();
Type[] typeParameters = ((ParameterizedType) type).getActualTypeArguments();
JsonObject object = (JsonObject) json;
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
String value = entry.getValue().getAsString();
mapping.put(entry.getKey(),
Castors.me()
.castTo(value,
(Class) typeParameters[1]));
}
return mapping;
}
})
.setPrettyPrinting()
.create();
}
public static final Gson gson() {
return gson;
}
private Gsons() {}
}
@liamzdenek
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment