Skip to content

Commit

Permalink
Fix EsotericSoftware#412: Add default serializer for java.net.URL
Browse files Browse the repository at this point in the history
  • Loading branch information
magro committed Mar 24, 2016
1 parent 8fd3f89 commit c10fd76
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -43,6 +44,7 @@
import java.util.TreeMap;
import java.util.TreeSet;

import com.esotericsoftware.kryo.serializers.DefaultSerializers.URLSerializer;
import com.esotericsoftware.kryo.serializers.OptionalSerializers;
import com.esotericsoftware.kryo.serializers.GenericsResolver;
import com.esotericsoftware.kryo.serializers.TimeSerializers;
Expand Down Expand Up @@ -216,6 +218,7 @@ public Kryo (ClassResolver classResolver, ReferenceResolver referenceResolver, S
addDefaultSerializer(Calendar.class, CalendarSerializer.class);
addDefaultSerializer(Locale.class, LocaleSerializer.class);
addDefaultSerializer(Charset.class, CharsetSerializer.class);
addDefaultSerializer(URL.class, URLSerializer.class);
OptionalSerializers.addDefaultSerializers(this);
TimeSerializers.addDefaultSerializers(this);
lowPriorityDefaultSerializerCount = defaultSerializers.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -838,7 +840,7 @@ protected static boolean isSameLocale(Locale locale, String language, String cou
}
}

/** Serializer for {@link Charset}. Added as default serializer for java >= 7. */
/** Serializer for {@link Charset}. */
public static class CharsetSerializer extends Serializer<Charset> {

{ setImmutable(true); }
Expand All @@ -853,4 +855,23 @@ public Charset read(Kryo kryo, Input input, Class<Charset> type) {

}

/** Serializer for {@link URL}. */
public static class URLSerializer extends Serializer<URL> {

{ setImmutable(true); }

public void write(Kryo kryo, Output output, URL object) {
output.writeString(object.toExternalForm());
}

public URL read(Kryo kryo, Input input, Class<URL> type) {
try {
return new java.net.URL(input.readString());
} catch (MalformedURLException e) {
throw new KryoException(e);
}
}

}

}
11 changes: 11 additions & 0 deletions test/com/esotericsoftware/kryo/DefaultSerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -34,6 +35,7 @@

import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.objenesis.strategy.StdInstantiatorStrategy;

/** @author Nathan Sweet <[email protected]> */
public class DefaultSerializersTest extends KryoTestCase {
Expand Down Expand Up @@ -365,6 +367,15 @@ public void testCharset() {
}
}

public void testURLSerializer () throws Exception {
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
kryo.setRegistrationRequired(true);
kryo.register(URL.class);

roundTrip(41, 41, new URL("https://github.com/EsotericSoftware/kryo"));
roundTrip(78, 78, new URL("https://github.com:443/EsotericSoftware/kryo/pulls?utf8=%E2%9C%93&q=is%3Apr"));
}

public enum TestEnum {
a, b, c
}
Expand Down
6 changes: 3 additions & 3 deletions test/com/esotericsoftware/kryo/SerializationCompatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public class SerializationCompatTest extends KryoTestCase {

private static final String ENDIANNESS = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN ? "le" : "be";
private static final int JAVA_VERSION = Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
private static final int EXPECTED_DEFAULT_SERIALIZER_COUNT = JAVA_VERSION < 8 ? 34 : 52;
private static final int EXPECTED_DEFAULT_SERIALIZER_COUNT = JAVA_VERSION < 8 ? 35 : 53;
private static final List<TestDataDescription<?>> TEST_DATAS = new ArrayList<TestDataDescription<?>>();

static {
TEST_DATAS.add(new TestDataDescription<TestData>("3.0.0", new TestData(), 1824, 1932));
if(JAVA_VERSION >= 8) TEST_DATAS.add(new TestDataDescription<TestDataJava8>("3.1.0", new TestDataJava8(), 1984, 2136));
TEST_DATAS.add(new TestDataDescription<TestData>("3.0.0", new TestData(), 1865, 1973));
if(JAVA_VERSION >= 8) TEST_DATAS.add(new TestDataDescription<TestDataJava8>("3.1.0", new TestDataJava8(), 2025, 2177));
};

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.*;
import java.util.*;
Expand Down Expand Up @@ -114,6 +116,7 @@ public static class TestData implements Serializable {
private Calendar _calendar;
private Locale _locale;
List<Charset> _charsets;
private URL _url;

private Gender _enum;
private EnumSet<Gender> _enumSet;
Expand Down Expand Up @@ -188,6 +191,11 @@ public TestData () {
_locale = Locale.ENGLISH;
_charsets = new ArrayList<Charset>(Arrays.asList(Charset.forName("ISO-8859-1"), Charset.forName("US-ASCII"),
Charset.forName("UTF-8"), Charset.forName("UTF-16"), Charset.forName("UTF-16BE"), Charset.forName("UTF-16LE")));
try {
_url = new java.net.URL("https://github.com/EsotericSoftware/kryo");
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

_enum = Gender.FEMALE;
_enumSet = EnumSet.allOf(Gender.class);
Expand Down
Binary file modified test/resources/TestData-bytebuffer.ser
Binary file not shown.
Binary file modified test/resources/TestData-fast.ser
Binary file not shown.
Binary file modified test/resources/TestData-standard.ser
Binary file not shown.
Binary file modified test/resources/TestData-unsafe-le.ser
Binary file not shown.
Binary file modified test/resources/TestData-unsafeMemory-le.ser
Binary file not shown.
Binary file modified test/resources/TestDataJava8-bytebuffer.ser
Binary file not shown.
Binary file modified test/resources/TestDataJava8-fast.ser
Binary file not shown.
Binary file modified test/resources/TestDataJava8-standard.ser
Binary file not shown.
Binary file modified test/resources/TestDataJava8-unsafe-le.ser
Binary file not shown.
Binary file modified test/resources/TestDataJava8-unsafeMemory-le.ser
Binary file not shown.

0 comments on commit c10fd76

Please sign in to comment.