Search in sources :

Example 1 with TypeSerializer

use of ninja.leaping.configurate.objectmapping.serialize.TypeSerializer in project LanternServer by LanternPowered.

the class MultimapTypeSerializer method deserialize.

@Override
public Multimap deserialize(TypeToken<?> type, ConfigurationNode node) throws ObjectMappingException {
    final Multimap<Object, Object> multimap = LinkedHashMultimap.create();
    if (node.hasMapChildren()) {
        TypeToken<?> key = type.resolveType(Multimap.class.getTypeParameters()[0]);
        TypeToken<?> value = type.resolveType(Multimap.class.getTypeParameters()[1]);
        TypeSerializer keySerial = node.getOptions().getSerializers().get(key);
        TypeSerializer valueSerial = node.getOptions().getSerializers().get(value);
        if (keySerial == null) {
            throw new ObjectMappingException("No type serializer available for type " + key);
        }
        if (valueSerial == null) {
            throw new ObjectMappingException("No type serializer available for type " + value);
        }
        for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) {
            Object keyValue = keySerial.deserialize(key, SimpleConfigurationNode.root().setValue(ent.getKey()));
            Object valueValue = valueSerial.deserialize(value, ent.getValue());
            if (keyValue == null || valueValue == null) {
                continue;
            }
            multimap.put(keyValue, valueValue);
        }
    }
    return multimap;
}
Also used : TypeSerializer(ninja.leaping.configurate.objectmapping.serialize.TypeSerializer) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 2 with TypeSerializer

use of ninja.leaping.configurate.objectmapping.serialize.TypeSerializer in project LanternServer by LanternPowered.

the class MultimapTypeSerializer method serialize.

@Override
public void serialize(TypeToken<?> type, Multimap obj, ConfigurationNode node) throws ObjectMappingException {
    TypeToken<?> key = type.resolveType(Multimap.class.getTypeParameters()[0]);
    TypeToken<?> value = type.resolveType(Multimap.class.getTypeParameters()[1]);
    TypeSerializer keySerial = node.getOptions().getSerializers().get(key);
    TypeSerializer valueSerial = node.getOptions().getSerializers().get(value);
    if (keySerial == null) {
        throw new ObjectMappingException("No type serializer available for type " + key);
    }
    if (valueSerial == null) {
        throw new ObjectMappingException("No type serializer available for type " + value);
    }
    node.setValue(ImmutableMap.of());
    for (Object k : obj.keySet()) {
        for (Object v : obj.get(k)) {
            SimpleConfigurationNode keyNode = SimpleConfigurationNode.root();
            keySerial.serialize(key, k, keyNode);
            valueSerial.serialize(value, v, node.getNode(keyNode.getValue()));
        }
    }
}
Also used : TypeSerializer(ninja.leaping.configurate.objectmapping.serialize.TypeSerializer) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode)

Aggregations

ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)2 TypeSerializer (ninja.leaping.configurate.objectmapping.serialize.TypeSerializer)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 SimpleConfigurationNode (ninja.leaping.configurate.SimpleConfigurationNode)1