Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't register a TypeSerializer for arbitrary Number subclasses #122

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand Down Expand Up @@ -69,7 +70,7 @@ public static TypeSerializerCollection newCollection() {
DEFAULT_SERIALIZERS.registerType(TypeToken.of(URL.class), new URLSerializer());
DEFAULT_SERIALIZERS.registerType(TypeToken.of(UUID.class), new UUIDSerializer());
DEFAULT_SERIALIZERS.registerPredicate(input -> input.getRawType().isAnnotationPresent(ConfigSerializable.class), new AnnotatedObjectSerializer());
DEFAULT_SERIALIZERS.registerType(TypeToken.of(Number.class), new NumberSerializer());
DEFAULT_SERIALIZERS.registerPredicate(NumberSerializer.getPredicate(), new NumberSerializer());
DEFAULT_SERIALIZERS.registerType(TypeToken.of(String.class), new StringSerializer());
DEFAULT_SERIALIZERS.registerType(TypeToken.of(Boolean.class), new BooleanSerializer());
DEFAULT_SERIALIZERS.registerType(new TypeToken<Map<?, ?>>() {}, new MapSerializer());
Expand All @@ -91,6 +92,20 @@ public void serialize(@NonNull TypeToken<?> type, @Nullable String obj, @NonNull
}

private static class NumberSerializer implements TypeSerializer<Number> {

public static Predicate<TypeToken<Number>> getPredicate() {
return (type) -> {
type = type.wrap();
Class<?> clazz = type.getRawType();
return Integer.class.equals(clazz)
|| Long.class.equals(clazz)
|| Short.class.equals(clazz)
|| Byte.class.equals(clazz)
|| Float.class.equals(clazz)
|| Double.class.equals(clazz);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected more types then this being supported by default. BigDecimal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intent with this PR is just to fix the behavior of the existing serialized. We could probably add support for BigDecimal in another PR

};
}

@Override
public Number deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws InvalidTypeException {
type = type.wrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public void testNumberSerializer() throws ObjectMappingException {
assertEquals(42, node.getValue());
}

@Test
public void testSerializeCustomNumber() throws ObjectMappingException {
final TypeToken<CustomNumber> customNumberType = TypeToken.of(CustomNumber.class);
final TypeSerializer<?> serializer = SERIALIZERS.get(customNumberType);
assertNull("Type serializer for custom number class should be null!", serializer);
}

@Test
public void testBooleanSerializer() throws ObjectMappingException {
final TypeToken<Boolean> booleanType = TypeToken.of(Boolean.class);
Expand Down Expand Up @@ -316,4 +323,27 @@ public void testPatternSerializer() throws ObjectMappingException {
assertEquals("(na )+batman", serializeTo.getValue());
assertEquals(testPattern.pattern(), patternSerializer.deserialize(patternType, serializeTo).pattern());
}

private static class CustomNumber extends Number {

@Override
public int intValue() {
return 0;
}

@Override
public long longValue() {
return 0;
}

@Override
public float floatValue() {
return 0;
}

@Override
public double doubleValue() {
return 0;
}
}
}