当前位置: 首页>>代码示例>>Java>>正文


Java TypeSerializer.serialize方法代码示例

本文整理汇总了Java中ninja.leaping.configurate.objectmapping.serialize.TypeSerializer.serialize方法的典型用法代码示例。如果您正苦于以下问题:Java TypeSerializer.serialize方法的具体用法?Java TypeSerializer.serialize怎么用?Java TypeSerializer.serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ninja.leaping.configurate.objectmapping.serialize.TypeSerializer的用法示例。


在下文中一共展示了TypeSerializer.serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: serialize

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@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()));
        }
    }
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:25,代码来源:MultimapTypeSerializer.java

示例2: serializeTo

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException {
    try {
        Object fieldVal = this.field.get(instance);
        if (fieldVal == null) {
            node.setValue(null);
        } else {
            TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType);
            if (serial == null) {
                throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type "
                        + this.fieldType);
            }
            serial.serialize(this.fieldType, fieldVal, node);
        }

        if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) {
            CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node);
            if (!commentNode.getComment().isPresent()) {
                commentNode.setComment(this.comment);
            }
        }
    } catch (IllegalAccessException e) {
        throw new ObjectMappingException("Unable to serialize field " + field.getName(), e);
    }

}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:27,代码来源:ObjectMapper.java

示例3: setValue

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
/**
 * Set this node's value to the given value.
 * If the provided value is a {@link java.util.Collection} or a {@link java.util.Map}, it will be unwrapped into
 * the appropriate configuration node structure.
 * This method will also perform serialization using the appropriate TypeSerializer for the given type, or casting if no type serializer is found.
 *
 * @param type The type to use for serialization type information
 * @param value The value to set
 * @param <T> The type to serialize to
 * @return this
 */
default <T> ConfigurationNode setValue(TypeToken<T> type, T value) throws ObjectMappingException {
    if (value == null) {
        setValue(null);
        return this;
    }
    TypeSerializer serial = getOptions().getSerializers().get(type);
    if (serial != null) {
        serial.serialize(type, value, this);
    } else if (getOptions().acceptsType(value.getClass())) {
        setValue(value); // Just write if no applicable serializer exists?
    } else {
        throw new ObjectMappingException("No serializer available for type " + type);
    }
    return this;
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:27,代码来源:ConfigurationNode.java

示例4: testNumberSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testNumberSerializer() throws ObjectMappingException {
    final TypeToken<Integer> intType = TypeToken.of(Integer.class);
    final TypeToken<Long> longType = TypeToken.of(Long.class);
    final TypeToken<Float> floatType = TypeToken.of(Float.class);
    final TypeToken<?> primitiveIntType = TypeToken.of(int.class);

    // They must all be the same serializer
    final TypeSerializer<Integer> numberSerializer = SERIALIZERS.get(intType);
    assertEquals(numberSerializer, SERIALIZERS.get(longType));
    assertEquals(numberSerializer, SERIALIZERS.get(floatType));
    assertEquals(numberSerializer, SERIALIZERS.get(primitiveIntType));

    SimpleConfigurationNode node = SimpleConfigurationNode.root().setValue(45f);
    assertEquals((Object) 45, numberSerializer.deserialize(intType, node));
    assertEquals((Object) 45L, numberSerializer.deserialize(longType, node));
    assertEquals((Object) 45f, numberSerializer.deserialize(floatType, node));
    assertEquals((Object) 45, numberSerializer.deserialize(primitiveIntType, node));

    numberSerializer.serialize(intType, 42, node);
    assertEquals(42, node.getValue());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:23,代码来源:TypeSerializersTest.java

示例5: testListSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testListSerializer() throws ObjectMappingException {
    final TypeToken<List<String>> stringListType = new TypeToken<List<String>>() {};
    final TypeSerializer<List<String>> stringListSerializer = SERIALIZERS.get(stringListType);
    final ConfigurationNode value = SimpleConfigurationNode.root();
    value.getAppendedNode().setValue("hi");
    value.getAppendedNode().setValue("there");
    value.getAppendedNode().setValue("beautiful");
    value.getAppendedNode().setValue("people");

    assertEquals(Arrays.asList("hi", "there", "beautiful", "people"), stringListSerializer.deserialize(stringListType, value));
    value.setValue(null);

    stringListSerializer.serialize(stringListType, Arrays.asList("hi", "there", "lame", "people"), value);
    assertEquals("hi", value.getNode(0).getString());
    assertEquals("there", value.getNode(1).getString());
    assertEquals("lame", value.getNode(2).getString());
    assertEquals("people", value.getNode(3).getString());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:20,代码来源:TypeSerializersTest.java

示例6: testMapSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testMapSerializer() throws ObjectMappingException {
    final TypeToken<Map<String, Integer>> mapStringIntType = new TypeToken<Map<String, Integer>>() {};
    final TypeSerializer<Map<String, Integer>> mapStringIntSerializer =
            SERIALIZERS.get(mapStringIntType);

    final ConfigurationNode value = SimpleConfigurationNode.root();
    value.getNode("fish").setValue(5);
    value.getNode("bugs").setValue("124880");
    value.getNode("time").setValue("-1");

    final Map<String, Integer> expectedValues = ImmutableMap.of("fish", 5, "bugs", 124880, "time", -1);

    assertEquals(expectedValues, mapStringIntSerializer.deserialize(mapStringIntType, value));

    value.setValue(null);

    mapStringIntSerializer.serialize(mapStringIntType, expectedValues, value);
    assertEquals(5, value.getNode("fish").getInt());
    assertEquals(124880, value.getNode("bugs").getInt());
    assertEquals(-1, value.getNode("time").getInt());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:23,代码来源:TypeSerializersTest.java

示例7: testInvalidMapValueTypes

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testInvalidMapValueTypes() throws ObjectMappingException {
    final TypeToken<Map<TestEnum, Integer>> mapTestEnumIntType = new TypeToken<Map<TestEnum, Integer>>() {};
    final TypeSerializer<Map<TestEnum, Integer>> mapTestEnumIntSerializer =
            SERIALIZERS.get(mapTestEnumIntType);

    final ConfigurationNode value = SimpleConfigurationNode.root();
    value.getNode("FIRST").setValue(5);
    value.getNode("SECOND").setValue(8);

    Map<TestEnum, Integer> des = mapTestEnumIntSerializer.deserialize(mapTestEnumIntType, value);
    final ConfigurationNode serialVal = SimpleConfigurationNode.root();
    mapTestEnumIntSerializer.serialize(mapTestEnumIntType, des, serialVal);
    assertEquals(value.getValue(), serialVal.getValue());
    //assertEquals(value, serialVal);
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:17,代码来源:TypeSerializersTest.java

示例8: testMapSerializerRemovesDeletedKeys

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testMapSerializerRemovesDeletedKeys() throws ObjectMappingException {
    final TypeToken<Map<String, Integer>> mapStringIntType = new TypeToken<Map<String, Integer>>() {};
    final TypeSerializer<Map<String, Integer>> mapStringIntSerializer = SERIALIZERS.get(mapStringIntType);

    final ConfigurationNode value = SimpleConfigurationNode.root();
    value.getNode("fish").setValue(5);
    value.getNode("bugs").setValue("124880");
    value.getNode("time").setValue("-1");

    @SuppressWarnings("unchecked")
    final Map<String, Integer> deserialized = mapStringIntSerializer.deserialize(mapStringIntType, value);
    deserialized.remove("fish");

    mapStringIntSerializer.serialize(mapStringIntType, deserialized, value);
    assertTrue(value.getNode("fish").isVirtual());
    assertFalse(value.getNode("bugs").isVirtual());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:19,代码来源:TypeSerializersTest.java

示例9: testStringSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testStringSerializer() throws ObjectMappingException {
    final TypeToken<String> stringType = TypeToken.of(String.class);
    final TypeSerializer<String> stringSerializer = SERIALIZERS.get(stringType);
    final ConfigurationNode node = SimpleConfigurationNode.root().setValue("foobar");

    assertEquals("foobar", stringSerializer.deserialize(stringType, node));
    stringSerializer.serialize(stringType, "foobarbaz", node);
    assertEquals("foobarbaz", node.getString());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:11,代码来源:TypeSerializersTest.java

示例10: testListSerializerPreservesEmptyList

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testListSerializerPreservesEmptyList() throws ObjectMappingException {
    final TypeToken<List<String>> listStringType = new TypeToken<List<String>>() {};
    final TypeSerializer<List<String>> listStringSerializer =
            SERIALIZERS.get(listStringType);

    final ConfigurationNode value = SimpleConfigurationNode.root();

    listStringSerializer.serialize(listStringType, ImmutableList.of(), value);

    assertTrue(value.hasListChildren());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:13,代码来源:TypeSerializersTest.java

示例11: testMapSerializerPreservesEmptyMap

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testMapSerializerPreservesEmptyMap() throws ObjectMappingException {
    final TypeToken<Map<String, Integer>> mapStringIntType = new TypeToken<Map<String, Integer>>() {};
    final TypeSerializer<Map<String, Integer>> mapStringIntSerializer =
            SERIALIZERS.get(mapStringIntType);

    final ConfigurationNode value = SimpleConfigurationNode.root();

    mapStringIntSerializer.serialize(mapStringIntType, ImmutableMap.of(), value);

    assertTrue(value.hasMapChildren());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:13,代码来源:TypeSerializersTest.java

示例12: testURISerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testURISerializer() throws ObjectMappingException {
     final TypeToken<URI> uriType = TypeToken.of(URI.class);
     final TypeSerializer<URI> uriSerializer = SERIALIZERS.get(uriType);

     final String uriString = "http://google.com";
     final URI testUri = URI.create(uriString);

     SimpleConfigurationNode node = SimpleConfigurationNode.root().setValue(uriString);
     assertEquals(testUri, uriSerializer.deserialize(uriType, node));

     uriSerializer.serialize(uriType, testUri, node);
     assertEquals(uriString, node.getValue());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:15,代码来源:TypeSerializersTest.java

示例13: testURLSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testURLSerializer() throws ObjectMappingException, MalformedURLException {
     final TypeToken<URL> urlType = TypeToken.of(URL.class);
     final TypeSerializer<URL> urlSerializer = SERIALIZERS.get(urlType);

     final String urlString = "http://google.com";
     final URL testUrl = new URL(urlString);

     SimpleConfigurationNode node = SimpleConfigurationNode.root().setValue(urlString);
     assertEquals(testUrl, urlSerializer.deserialize(urlType, node));

     urlSerializer.serialize(urlType, testUrl, node);
     assertEquals(urlString, node.getValue());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:15,代码来源:TypeSerializersTest.java

示例14: testUUIDSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testUUIDSerializer() throws ObjectMappingException {
    final TypeToken<UUID> uuidType = TypeToken.of(UUID.class);
    final TypeSerializer<UUID> uuidSerializer = SERIALIZERS.get(uuidType);

    final UUID testUuid = UUID.randomUUID();

    SimpleConfigurationNode serializeTo = SimpleConfigurationNode.root();
    uuidSerializer.serialize(uuidType, testUuid, serializeTo);
    assertEquals(testUuid.toString(), serializeTo.getValue());

    assertEquals(testUuid, uuidSerializer.deserialize(uuidType, serializeTo));

}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:15,代码来源:TypeSerializersTest.java

示例15: testPatternSerializer

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入方法依赖的package包/类
@Test
public void testPatternSerializer() throws ObjectMappingException {
    final TypeToken<Pattern> patternType = TypeToken.of(Pattern.class);
    final TypeSerializer<Pattern> patternSerializer = SERIALIZERS.get(patternType);

    final Pattern testPattern = Pattern.compile("(na )+batman");
    SimpleConfigurationNode serializeTo = SimpleConfigurationNode.root();
    patternSerializer.serialize(patternType, testPattern, serializeTo);
    assertEquals("(na )+batman", serializeTo.getValue());
    assertEquals(testPattern.pattern(), patternSerializer.deserialize(patternType, serializeTo).pattern());
}
 
开发者ID:SpongePowered,项目名称:configurate,代码行数:12,代码来源:TypeSerializersTest.java


注:本文中的ninja.leaping.configurate.objectmapping.serialize.TypeSerializer.serialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。