本文整理汇总了Java中ninja.leaping.configurate.objectmapping.serialize.TypeSerializer类的典型用法代码示例。如果您正苦于以下问题:Java TypeSerializer类的具体用法?Java TypeSerializer怎么用?Java TypeSerializer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeSerializer类属于ninja.leaping.configurate.objectmapping.serialize包,在下文中一共展示了TypeSerializer类的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()));
}
}
}
示例2: deserializeFrom
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入依赖的package包/类
public void deserializeFrom(Object instance, ConfigurationNode node) throws ObjectMappingException {
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);
}
Object newVal = node.isVirtual() ? null : serial.deserialize(this.fieldType, node);
try {
if (newVal == null) {
Object existingVal = field.get(instance);
if (existingVal != null) {
serializeTo(instance, node);
}
} else {
field.set(instance, newVal);
}
} catch (IllegalAccessException e) {
throw new ObjectMappingException("Unable to deserialize field " + field.getName(), e);
}
}
示例3: 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);
}
}
示例4: getValue
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> T getValue(TypeToken<T> type, T def) throws ObjectMappingException {
Object value = getValue();
if (value == null) {
if (def != null && getOptions().shouldCopyDefaults()) {
setValue(type, def);
}
return def;
}
TypeSerializer serial = getOptions().getSerializers().get(type);
if (serial == null) {
if (type.getRawType().isInstance(value)) {
return (T) type.getRawType().cast(value);
} else {
if (def != null && getOptions().shouldCopyDefaults()) {
setValue(type, def);
}
return def;
}
}
return (T) serial.deserialize(type, this);
}
示例5: 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;
}
示例6: 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());
}
示例7: testEnumValueSerializer
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入依赖的package包/类
@Test
public void testEnumValueSerializer() throws ObjectMappingException {
final TypeToken<TestEnum> enumType = TypeToken.of(TestEnum.class);
final TypeSerializer<TestEnum> enumSerializer = SERIALIZERS.get(enumType);
SimpleConfigurationNode node = SimpleConfigurationNode.root();
node.getNode("present_val").setValue("first");
node.getNode("another_present_val").setValue("SECOND");
node.getNode(("casematters_val")).setValue("tHiRd");
node.getNode(("casematters_val_lowercase")).setValue("third");
node.getNode("invalid_val").setValue("3rd");
assertEquals(TestEnum.FIRST, enumSerializer.deserialize(enumType, node.getNode("present_val")));
assertEquals(TestEnum.SECOND, enumSerializer.deserialize(enumType, node.getNode("another_present_val")));
assertEquals(TestEnum.Third, enumSerializer.deserialize(enumType, node.getNode("casematters_val")));
assertEquals(TestEnum.third, enumSerializer.deserialize(enumType, node.getNode("casematters_val_lowercase")));
expectedException.expect(ObjectMappingException.class);
enumSerializer.deserialize(enumType, node.getNode("invalid_val"));
}
示例8: 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());
}
示例9: 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());
}
示例10: 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);
}
示例11: 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());
}
示例12: deserialize
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入依赖的package包/类
@Override
public Multimap deserialize(TypeToken<?> type, ConfigurationNode node) throws ObjectMappingException {
final Multimap<Object, Object> multimap = LinkedHashMultimap.create();
if (node.hasMapChildren()) {
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);
}
for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) {
Object keyValue = keySerial.deserialize(key, SimpleConfigurationNode.root().setValue(ent.getKey()));
Object valueValue = valueSerial.deserialize(value, ent.getValue());
if (keyValue == null || valueValue == null) {
continue;
}
multimap.put(keyValue, valueValue);
}
}
return multimap;
}
示例13: 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());
}
示例14: testBooleanSerializer
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; //导入依赖的package包/类
@Test
public void testBooleanSerializer() throws ObjectMappingException {
final TypeToken<Boolean> booleanType = TypeToken.of(Boolean.class);
final TypeSerializer<Boolean> booleanSerializer = SERIALIZERS.get(booleanType);
SimpleConfigurationNode node = SimpleConfigurationNode.root();
node.getNode("direct").setValue(true);
node.getNode("fromstring").setValue("true");
assertEquals(true, booleanSerializer.deserialize(booleanType, node.getNode("direct")));
assertEquals(true, booleanSerializer.deserialize(booleanType, node.getNode("fromstring")));
}
示例15: 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());
}