Skip to content

Commit

Permalink
Adds Charset serializer, also set as default serializer
Browse files Browse the repository at this point in the history
With registration required == true, users still have to register
the required charsets, e.g. via `kryo.register(StandardCharsets.ISO_8859_1.getClass());`.

Resolves #364
  • Loading branch information
magro committed Mar 4, 2016
1 parent 617a57f commit d7e924b
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 10 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.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -72,6 +73,7 @@
import com.esotericsoftware.kryo.serializers.DefaultSerializers.ByteSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.CalendarSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.CharSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.CharsetSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.ClassSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.CollectionsEmptyListSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.CollectionsEmptyMapSerializer;
Expand Down Expand Up @@ -213,6 +215,7 @@ public Kryo (ClassResolver classResolver, ReferenceResolver referenceResolver, S
addDefaultSerializer(TimeZone.class, TimeZoneSerializer.class);
addDefaultSerializer(Calendar.class, CalendarSerializer.class);
addDefaultSerializer(Locale.class, LocaleSerializer.class);
addDefaultSerializer(Charset.class, CharsetSerializer.class);
OptionalSerializers.addDefaultSerializers(this);
TimeSerializers.addDefaultSerializers(this);
lowPriorityDefaultSerializerCount = defaultSerializers.size();
Expand Down
23 changes: 20 additions & 3 deletions src/com/esotericsoftware/kryo/serializers/DefaultSerializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo.serializers;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -836,4 +837,20 @@ protected static boolean isSameLocale(Locale locale, String language, String cou
}
}
}
}

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

{ setImmutable(true); }

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

public Charset read(Kryo kryo, Input input, Class<Charset> type) {
return Charset.forName(input.readString());
}

}

}
31 changes: 27 additions & 4 deletions test/com/esotericsoftware/kryo/DefaultSerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.util.Locale;

/** @author Nathan Sweet <misc@n4te.com> */
public class DefaultSerializersTest extends KryoTestCase {
Expand Down Expand Up @@ -342,6 +345,26 @@ public void testLocaleSerializer () {
roundTrip(16, 16, new Locale("es", "ES", "áéíóú"));
}

public void testCharset() {
List<String> css = Arrays.asList("ISO-8859-1", "US-ASCII", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE");

for(String cs : css) {
Charset charset = Charset.forName(cs);
kryo.register(charset.getClass());
int expectedLength = 1 + cs.length();
roundTrip(expectedLength, expectedLength, charset);
}

kryo = new Kryo();
kryo.setRegistrationRequired(false);

for(String cs : css) {
Charset charset = Charset.forName(cs);
int expectedLength = 3 + charset.getClass().getName().length() + cs.length();
roundTrip(expectedLength, expectedLength, charset);
}
}

public enum TestEnum {
a, b, c
}
Expand Down Expand Up @@ -373,4 +396,4 @@ public BigIntegerSubclass(String val) {
}
}

}
}
9 changes: 6 additions & 3 deletions test/com/esotericsoftware/kryo/SerializationCompatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.esotericsoftware.kryo.SerializationCompatTestData.TestData;
import com.esotericsoftware.kryo.SerializationCompatTestData.TestDataJava8;
import com.esotericsoftware.kryo.io.*;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.minlog.Log;
import org.objenesis.strategy.StdInstantiatorStrategy;

Expand All @@ -32,6 +33,7 @@
import java.lang.reflect.Field;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;

Expand Down Expand Up @@ -72,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 ? 33 : 51;
private static final int EXPECTED_DEFAULT_SERIALIZER_COUNT = JAVA_VERSION < 8 ? 34 : 52;
private static final List<TestDataDescription<?>> TEST_DATAS = new ArrayList<TestDataDescription<?>>();

static {
TEST_DATAS.add(new TestDataDescription<TestData>("3.0.0", new TestData(), 1646, 1754));
if(JAVA_VERSION >= 8) TEST_DATAS.add(new TestDataDescription<TestDataJava8>("3.1.0", new TestDataJava8(), 1806, 1958));
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));
};

@Override
Expand Down Expand Up @@ -213,6 +215,7 @@ private void runTest(TestDataDescription description, String variant, Function1<
// if anything failed (e.g. the initial test), we should delete the file as it may be empty or corruped
out.close();
file.delete();
throw e;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.time.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -112,6 +113,7 @@ public static class TestData implements Serializable {
private TimeZone _timeZone;
private Calendar _calendar;
private Locale _locale;
List<Charset> _charsets;

private Gender _enum;
private EnumSet<Gender> _enumSet;
Expand Down Expand Up @@ -184,6 +186,8 @@ public TestData () {

_timeZone = TimeZone.getTimeZone("America/Los_Angeles");
_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")));

_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 d7e924b

Please sign in to comment.