Search in sources :

Example 1 with TypeSerializerCollection

use of org.spongepowered.configurate.serialize.TypeSerializerCollection in project Configurate by SpongePowered.

the class DfuSerializersTest method testCodecSerializer.

@Test
void testCodecSerializer() throws SerializationException {
    final TypeSerializerCollection serializers = TypeSerializerCollection.defaults().childBuilder().register(VEC3I_TYPE, DfuSerializers.serializer(VEC3I_CODEC)).build();
    final ConfigurationNode testElement = BasicConfigurationNode.root(ConfigurationOptions.defaults().serializers(serializers), n -> {
        n.appendListNode().raw(4);
        n.appendListNode().raw(5);
        n.appendListNode().raw(8);
    });
    final Vector3i pos = testElement.get(VEC3I_TYPE);
    assertEquals(new Vector3i(4, 5, 8), pos);
}
Also used : ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Vector3i(org.spongepowered.math.vector.Vector3i) TypeSerializerCollection(org.spongepowered.configurate.serialize.TypeSerializerCollection) Test(org.junit.jupiter.api.Test)

Example 2 with TypeSerializerCollection

use of org.spongepowered.configurate.serialize.TypeSerializerCollection in project Nucleus by NucleusPowered.

the class ConfigurateHelper method setupCore.

private static TypeSerializerCollection setupCore(final INucleusServiceCollection serviceCollection, final ObjectMapper.Factory factory, final ConfigurationOptions options) {
    final TypeSerializerCollection.Builder typeSerializerCollection = options.serializers().childBuilder();
    // Custom type serialisers for Nucleus
    typeSerializerCollection.registerAnnotatedObjects(factory);
    typeSerializerCollection.register(TypeToken.get(Vector3d.class), new Vector3dTypeSerialiser());
    typeSerializerCollection.register(TypeToken.get(Pattern.class), new PatternTypeSerialiser());
    typeSerializerCollection.register(TypeToken.get(NucleusTextTemplate.class), new NucleusTextTemplateTypeSerialiser(serviceCollection.textTemplateFactory()));
    typeSerializerCollection.register(TypeToken.get(Instant.class), new InstantTypeSerialiser());
    typeSerializerCollection.register(TypeTokens.ABSTRACT_DATA_OBJECT_TYPE_TOKEN, DataObjectTranslator.INSTANCE);
    typeSerializerCollection.register(TypeTokens.NAMED_LOCATION, new NamedLocationSerialiser());
    typeSerializerCollection.register(TypeTokens.LOCALE, new LocaleSerialiser());
    return typeSerializerCollection.build();
}
Also used : Pattern(java.util.regex.Pattern) InstantTypeSerialiser(io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.InstantTypeSerialiser) NamedLocationSerialiser(io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.NamedLocationSerialiser) LocaleSerialiser(io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.LocaleSerialiser) Vector3d(org.spongepowered.math.vector.Vector3d) PatternTypeSerialiser(io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.PatternTypeSerialiser) Vector3dTypeSerialiser(io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.Vector3dTypeSerialiser) Instant(java.time.Instant) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) TypeSerializerCollection(org.spongepowered.configurate.serialize.TypeSerializerCollection) NucleusTextTemplateTypeSerialiser(io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.NucleusTextTemplateTypeSerialiser)

Example 3 with TypeSerializerCollection

use of org.spongepowered.configurate.serialize.TypeSerializerCollection in project Configurate by SpongePowered.

the class DfuSerializersTest method testSerializerCodec.

@Test
void testSerializerCodec() throws IOException {
    final TypeSerializerCollection serializers = TypeSerializerCollection.defaults().childBuilder().register(VEC3I_TYPE, DfuSerializers.serializer(VEC3I_CODEC)).build();
    @Nullable final Codec<TestSerializable> codec = DfuSerializers.codec(TypeToken.get(TestSerializable.class), serializers);
    assertNotNull(codec);
    final DataResult<JsonElement> out = codec.encode(new TestSerializable(), JsonOps.INSTANCE, JsonOps.INSTANCE.empty());
    out.error().ifPresent(err -> {
        throw new RuntimeException(err.message());
    });
    final StringWriter buffer = new StringWriter();
    try (JsonWriter writer = new JsonWriter(buffer)) {
        writer.setIndent("    ");
        Streams.write(out.result().orElseThrow(() -> new RuntimeException("No result present!")), writer);
    }
    assertLinesMatch(Resources.readLines(this.getClass().getResource("test-serialize-codec.json"), StandardCharsets.UTF_8), Arrays.asList(buffer.toString().split("\n")));
}
Also used : StringWriter(java.io.StringWriter) JsonElement(com.google.gson.JsonElement) TypeSerializerCollection(org.spongepowered.configurate.serialize.TypeSerializerCollection) JsonWriter(com.google.gson.stream.JsonWriter) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Test(org.junit.jupiter.api.Test)

Example 4 with TypeSerializerCollection

use of org.spongepowered.configurate.serialize.TypeSerializerCollection in project Configurate by SpongePowered.

the class DfuSerializersTest method testCodecSerializer.

@Test
void testCodecSerializer() throws SerializationException {
    final TypeSerializerCollection serializers = TypeSerializerCollection.defaults().childBuilder().register(VEC3I_TYPE, DfuSerializers.serializer(VEC3I_CODEC)).build();
    final ConfigurationNode testElement = BasicConfigurationNode.root(ConfigurationOptions.defaults().serializers(serializers), n -> {
        n.appendListNode().raw(4);
        n.appendListNode().raw(5);
        n.appendListNode().raw(8);
    });
    @Nullable final Vector3i pos = testElement.get(VEC3I_TYPE);
    assertEquals(new Vector3i(4, 5, 8), pos);
}
Also used : ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Vector3i(org.spongepowered.math.vector.Vector3i) TypeSerializerCollection(org.spongepowered.configurate.serialize.TypeSerializerCollection) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Test(org.junit.jupiter.api.Test)

Example 5 with TypeSerializerCollection

use of org.spongepowered.configurate.serialize.TypeSerializerCollection in project Configurate by SpongePowered.

the class DfuSerializersTest method testSerializerCodec.

@Test
void testSerializerCodec() throws IOException {
    final TypeSerializerCollection serializers = TypeSerializerCollection.defaults().childBuilder().register(VEC3I_TYPE, DfuSerializers.serializer(VEC3I_CODEC)).build();
    @Nullable final Codec<TestSerializable> codec = DfuSerializers.codec(TypeToken.get(TestSerializable.class), serializers);
    assertNotNull(codec);
    final DataResult<JsonElement> out = codec.encode(new TestSerializable(), JsonOps.INSTANCE, JsonOps.INSTANCE.empty());
    out.error().ifPresent(err -> {
        throw new RuntimeException(err.message());
    });
    final StringWriter buffer = new StringWriter();
    try (JsonWriter writer = new JsonWriter(buffer)) {
        writer.setIndent("    ");
        Streams.write(out.result().orElseThrow(() -> new RuntimeException("No result present!")), writer);
    }
    assertLinesMatch(Resources.readLines(this.getClass().getResource("test-serialize-codec.json"), StandardCharsets.UTF_8), Arrays.asList(buffer.toString().split("\n")));
}
Also used : StringWriter(java.io.StringWriter) JsonElement(com.google.gson.JsonElement) TypeSerializerCollection(org.spongepowered.configurate.serialize.TypeSerializerCollection) JsonWriter(com.google.gson.stream.JsonWriter) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Test(org.junit.jupiter.api.Test)

Aggregations

TypeSerializerCollection (org.spongepowered.configurate.serialize.TypeSerializerCollection)5 Test (org.junit.jupiter.api.Test)4 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 JsonElement (com.google.gson.JsonElement)2 JsonWriter (com.google.gson.stream.JsonWriter)2 StringWriter (java.io.StringWriter)2 BasicConfigurationNode (org.spongepowered.configurate.BasicConfigurationNode)2 ConfigurationNode (org.spongepowered.configurate.ConfigurationNode)2 Vector3i (org.spongepowered.math.vector.Vector3i)2 NucleusTextTemplate (io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate)1 InstantTypeSerialiser (io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.InstantTypeSerialiser)1 LocaleSerialiser (io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.LocaleSerialiser)1 NamedLocationSerialiser (io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.NamedLocationSerialiser)1 NucleusTextTemplateTypeSerialiser (io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.NucleusTextTemplateTypeSerialiser)1 PatternTypeSerialiser (io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.PatternTypeSerialiser)1 Vector3dTypeSerialiser (io.github.nucleuspowered.nucleus.core.configurate.typeserialisers.Vector3dTypeSerialiser)1 Instant (java.time.Instant)1 Pattern (java.util.regex.Pattern)1 Vector3d (org.spongepowered.math.vector.Vector3d)1