Skip to content

Commit 7a43d1a

Browse files
committed
Implement DataTranslatable TypeSerializer
1 parent 8c20e20 commit 7a43d1a

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

src/main/java/org/spongepowered/common/config/PluginConfigManager.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final class PluginConfigManager implements ConfigManager {
4949
private final WatchServiceListener listener;
5050

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

0 commit comments

Comments
 (0)