All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vavr.gson.MapConverter Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
/*                        __    __  __  __    __  ___
 *                       \  \  /  /    \  \  /  /  __/
 *                        \  \/  /  /\  \  \/  /  /
 *                         \____/__/  \__\____/__/.ɪᴏ
 * ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
 */
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy