Skip to content

Commit

Permalink
Implement DataTranslatable TypeSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Nov 14, 2024
1 parent 8c20e20 commit 7a43d1a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.config;

import io.leangen.geantyref.GenericTypeReflector;
import org.spongepowered.api.data.DataManager;
import org.spongepowered.api.data.persistence.DataTranslator;
import org.spongepowered.common.data.persistence.ConfigurateTranslator;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;

import java.lang.reflect.Type;

import javax.inject.Inject;

public final class DataTranslatableTypeSerializer implements TypeSerializer<Object> {

private final DataManager dataManager;

@Inject
DataTranslatableTypeSerializer(final DataManager dataManager) {
this.dataManager = dataManager;
}

public boolean accepts(final Type x) {
return this.dataManager.translator(GenericTypeReflector.erase(x)).isPresent();
}

@SuppressWarnings("unchecked")
@Override
public Object deserialize(final Type type, final ConfigurationNode value) throws SerializationException {
final Class<Object> clazz = (Class<Object>) GenericTypeReflector.erase(type);
final DataTranslator<Object> translator = this.dataManager.translator(clazz)
.orElseThrow(() -> new SerializationException("Could not translate of DataTranslatable type: " + clazz.getName()));
return translator.translate(ConfigurateTranslator.instance().translate(value));
}

@SuppressWarnings("unchecked")
@Override
public void serialize(final Type type, final Object obj, final ConfigurationNode value) throws SerializationException {
if (obj == null) {
value.raw(null);
} else {
final Class<Object> clazz = (Class<Object>) GenericTypeReflector.erase(type);
final DataTranslator<Object> translator = this.dataManager.translator(clazz)
.orElseThrow(() -> new SerializationException("Could not translate of DataTranslatable type: " + clazz.getName()));
ConfigurateTranslator.instance().translateDataToNode(value, translator.translate(obj));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class PluginConfigManager implements ConfigManager {
private final WatchServiceListener listener;

@Inject
PluginConfigManager(final DataSerializableTypeSerializer dataSerializableSerializer) throws IOException {
PluginConfigManager(final DataSerializableTypeSerializer dataSerializableSerializer, final DataTranslatableTypeSerializer dataTranslatableSerializer) throws IOException {
// TODO: Move this onto the async scheduler, rather than shared FJ pool?
this.listener = WatchServiceListener.builder()
.threadFactory(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Sponge-WatchService-%d").build())
Expand All @@ -60,6 +60,7 @@ public final class PluginConfigManager implements ConfigManager {
// See https://github.com/SpongePowered/SpongeCommon/issues/1348
.register(DataSerializableTypeSerializer::accepts, dataSerializableSerializer)
.registerAll(SpongeAdventure.CONFIGURATE.serializers())
.register(dataTranslatableSerializer::accepts, dataTranslatableSerializer)
.build();
}

Expand Down

0 comments on commit 7a43d1a

Please sign in to comment.