|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.common.config; |
| 26 | + |
| 27 | +import io.leangen.geantyref.GenericTypeReflector; |
| 28 | +import org.spongepowered.api.data.DataManager; |
| 29 | +import org.spongepowered.api.data.persistence.DataTranslator; |
| 30 | +import org.spongepowered.common.data.persistence.ConfigurateTranslator; |
| 31 | +import org.spongepowered.configurate.ConfigurationNode; |
| 32 | +import org.spongepowered.configurate.serialize.SerializationException; |
| 33 | +import org.spongepowered.configurate.serialize.TypeSerializer; |
| 34 | + |
| 35 | +import java.lang.reflect.Type; |
| 36 | + |
| 37 | +import javax.inject.Inject; |
| 38 | + |
| 39 | +public final class DataTranslatableTypeSerializer implements TypeSerializer<Object> { |
| 40 | + |
| 41 | + private final DataManager dataManager; |
| 42 | + |
| 43 | + @Inject |
| 44 | + DataTranslatableTypeSerializer(final DataManager dataManager) { |
| 45 | + this.dataManager = dataManager; |
| 46 | + } |
| 47 | + |
| 48 | + public boolean accepts(final Type x) { |
| 49 | + return this.dataManager.translator(GenericTypeReflector.erase(x)).isPresent(); |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("unchecked") |
| 53 | + @Override |
| 54 | + public Object deserialize(final Type type, final ConfigurationNode value) throws SerializationException { |
| 55 | + final Class<Object> clazz = (Class<Object>) GenericTypeReflector.erase(type); |
| 56 | + final DataTranslator<Object> translator = this.dataManager.translator(clazz) |
| 57 | + .orElseThrow(() -> new SerializationException("Could not translate of DataTranslatable type: " + clazz.getName())); |
| 58 | + return translator.translate(ConfigurateTranslator.instance().translate(value)); |
| 59 | + } |
| 60 | + |
| 61 | + @SuppressWarnings("unchecked") |
| 62 | + @Override |
| 63 | + public void serialize(final Type type, final Object obj, final ConfigurationNode value) throws SerializationException { |
| 64 | + if (obj == null) { |
| 65 | + value.raw(null); |
| 66 | + } else { |
| 67 | + final Class<Object> clazz = (Class<Object>) GenericTypeReflector.erase(type); |
| 68 | + final DataTranslator<Object> translator = this.dataManager.translator(clazz) |
| 69 | + .orElseThrow(() -> new SerializationException("Could not translate of DataTranslatable type: " + clazz.getName())); |
| 70 | + ConfigurateTranslator.instance().translateDataToNode(value, translator.translate(obj)); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments