io.vavr.gson.MapConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vavr-gson Show documentation
Show all versions of vavr-gson Show documentation
A set of GSON serialiser/deserialisers for Vavr.io
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/.ɪᴏ
* ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
*/
package io.vavr.gson;
import com.google.gson.*;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.collection.Map;
import java.lang.reflect.Type;
import java.util.function.Function;
import java.util.stream.Collectors;
class MapConverter> extends JsonObjectConverter {
private final Function>, Map, ?>> factory;
MapConverter(Function>, Map, ?>> factory) {
this.factory = factory;
}
@Override
@SuppressWarnings("unchecked")
T fromJsonObject(JsonObject obj, Type type, Type[] subTypes, JsonDeserializationContext ctx) throws JsonParseException {
Function, Tuple2> mapper;
if (subTypes.length == 2) {
mapper = e -> Tuple.of(ctx.deserialize(new JsonPrimitive(e.getKey()), subTypes[0]), ctx.deserialize(e.getValue(), subTypes[1]));
} else {
mapper = e -> Tuple.of(e.getKey(), e.getValue());
}
return (T) factory.apply(obj.entrySet().stream().map(mapper).collect(Collectors.toList()));
}
@Override
Map toMap(T src) {
return src;
}
}