com.esotericsoftware.kryo.kryo5.serializers.DefaultSerializers Maven / Gradle / Ivy
/* Copyright (c) 2008-2020, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* 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. */
package com.esotericsoftware.kryo.serializers;
import static com.esotericsoftware.kryo.Kryo.*;
import static com.esotericsoftware.kryo.util.Util.*;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.Registration;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Date;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListMap;
/** Contains many serializer classes that are provided by {@link Kryo#addDefaultSerializer(Class, Class) default}.
* @author Nathan Sweet */
public class DefaultSerializers {
public static class VoidSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Object object) {
}
public Object read (Kryo kryo, Input input, Class type) {
return null;
}
}
public static class BooleanSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Boolean object) {
output.writeBoolean(object);
}
public Boolean read (Kryo kryo, Input input, Class extends Boolean> type) {
return input.readBoolean();
}
}
public static class ByteSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Byte object) {
output.writeByte(object);
}
public Byte read (Kryo kryo, Input input, Class extends Byte> type) {
return input.readByte();
}
}
public static class CharSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Character object) {
output.writeChar(object);
}
public Character read (Kryo kryo, Input input, Class extends Character> type) {
return input.readChar();
}
}
public static class ShortSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Short object) {
output.writeShort(object);
}
public Short read (Kryo kryo, Input input, Class extends Short> type) {
return input.readShort();
}
}
public static class IntSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Integer object) {
output.writeInt(object, false);
}
public Integer read (Kryo kryo, Input input, Class extends Integer> type) {
return input.readInt(false);
}
}
public static class LongSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Long object) {
output.writeVarLong(object, false);
}
public Long read (Kryo kryo, Input input, Class extends Long> type) {
return input.readVarLong(false);
}
}
public static class FloatSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Float object) {
output.writeFloat(object);
}
public Float read (Kryo kryo, Input input, Class extends Float> type) {
return input.readFloat();
}
}
public static class DoubleSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Double object) {
output.writeDouble(object);
}
public Double read (Kryo kryo, Input input, Class extends Double> type) {
return input.readDouble();
}
}
/** @see Output#writeString(String) */
public static class StringSerializer extends ImmutableSerializer {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, String object) {
output.writeString(object);
}
public String read (Kryo kryo, Input input, Class extends String> type) {
return input.readString();
}
}
/** Serializer for {@link BigInteger} and any subclass.
* @author Tumi (enhacements) */
public static class BigIntegerSerializer extends ImmutableSerializer {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, BigInteger object) {
if (object == null) {
output.writeByte(NULL);
return;
}
// fast-path optimizations for BigInteger.ZERO constant
if (object == BigInteger.ZERO) {
output.writeByte(2);
output.writeByte(0);
return;
}
// default behaviour
byte[] bytes = object.toByteArray();
output.writeVarInt(bytes.length + 1, true);
output.writeBytes(bytes);
}
public BigInteger read (Kryo kryo, Input input, Class extends BigInteger> type) {
int length = input.readVarInt(true);
if (length == NULL) return null;
byte[] bytes = input.readBytes(length - 1);
if (type != BigInteger.class && type != null) {
// Use reflection for subclasses.
try {
Constructor extends BigInteger> constructor = type.getConstructor(byte[].class);
if (!constructor.isAccessible()) {
try {
constructor.setAccessible(true);
} catch (SecurityException ignored) {
}
}
return constructor.newInstance(bytes);
} catch (Exception ex) {
throw new KryoException(ex);
}
}
if (length == 2) {
// Fast-path optimizations for BigInteger constants.
switch (bytes[0]) {
case 0:
return BigInteger.ZERO;
case 1:
return BigInteger.ONE;
case 10:
return BigInteger.TEN;
}
}
return new BigInteger(bytes);
}
}
/** Serializer for {@link BigDecimal} and any subclass.
* @author Tumi (enhacements) */
public static class BigDecimalSerializer extends ImmutableSerializer {
private final BigIntegerSerializer bigIntegerSerializer = new BigIntegerSerializer();
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, BigDecimal object) {
if (object == null) {
output.writeByte(NULL);
return;
}
// fast-path optimizations for BigDecimal constants
if (object == BigDecimal.ZERO) {
bigIntegerSerializer.write(kryo, output, BigInteger.ZERO);
output.writeInt(0, false); // for backwards compatibility
return;
}
// default behaviour
bigIntegerSerializer.write(kryo, output, object.unscaledValue());
output.writeInt(object.scale(), false);
}
public BigDecimal read (Kryo kryo, Input input, Class extends BigDecimal> type) {
BigInteger unscaledValue = bigIntegerSerializer.read(kryo, input, BigInteger.class);
if (unscaledValue == null) return null;
int scale = input.readInt(false);
if (type != BigDecimal.class && type != null) {
// For subclasses, use reflection
try {
Constructor extends BigDecimal> constructor = type.getConstructor(BigInteger.class, int.class);
if (!constructor.isAccessible()) {
try {
constructor.setAccessible(true);
} catch (SecurityException ignored) {
}
}
return constructor.newInstance(unscaledValue, scale);
} catch (Exception ex) {
throw new KryoException(ex);
}
}
// fast-path optimizations for BigDecimal constants
if (unscaledValue == BigInteger.ZERO && scale == 0) {
return BigDecimal.ZERO;
}
// default behaviour
return new BigDecimal(unscaledValue, scale);
}
}
public static class ClassSerializer extends ImmutableSerializer {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, Class type) {
kryo.writeClass(output, type);
if (type != null && (type.isPrimitive() || isWrapperClass(type))) output.writeBoolean(type.isPrimitive());
}
public Class read (Kryo kryo, Input input, Class extends Class> ignored) {
Registration registration = kryo.readClass(input);
if (registration == null) return null;
Class type = registration.getType();
if (!type.isPrimitive() || input.readBoolean()) return type;
return getWrapperClass(type);
}
}
/** Serializer for {@link Date}, {@link java.sql.Date}, {@link Time}, {@link Timestamp} and any other subclass.
* @author Tumi */
public static class DateSerializer extends Serializer {
private Date create (Kryo kryo, Class extends Date> type, long time) throws KryoException {
if (type == Date.class || type == null) {
return new Date(time);
}
if (type == Timestamp.class) {
return new Timestamp(time);
}
if (type == java.sql.Date.class) {
return new java.sql.Date(time);
}
if (type == Time.class) {
return new Time(time);
}
// other cases, reflection
try {
// Try to avoid invoking the no-args constructor
// (which is expected to initialize the instance with the current time)
Constructor extends Date> constructor = type.getConstructor(long.class);
if (!constructor.isAccessible()) {
try {
constructor.setAccessible(true);
} catch (SecurityException ignored) {
}
}
return constructor.newInstance(time);
} catch (Exception ex) {
// default strategy
Date d = kryo.newInstance(type);
d.setTime(time);
return d;
}
}
public void write (Kryo kryo, Output output, Date object) {
output.writeVarLong(object.getTime(), true);
}
public Date read (Kryo kryo, Input input, Class extends Date> type) {
return create(kryo, type, input.readVarLong(true));
}
public Date copy (Kryo kryo, Date original) {
return create(kryo, original.getClass(), original.getTime());
}
}
public static class EnumSerializer extends ImmutableSerializer {
{
setAcceptsNull(true);
}
private Object[] enumConstants;
public EnumSerializer (Class extends Enum> type) {
enumConstants = type.getEnumConstants();
// We allow the serialization of the (abstract!) Enum.class (instead of an actual "user" enum),
// which also creates an EnumSerializer instance during Kryo.writeClass with the following trace:
// ClassSerializer.write -> Kryo.writeClass -> DefaultClassResolver.writeClass
// -> Kryo.getDefaultSerializer -> ReflectionSerializerFactory.makeSerializer(kryo, EnumSerializer, Enum.class)
// This EnumSerializer instance is expected to be never called for write/read.
if (enumConstants == null && !Enum.class.equals(type))
throw new IllegalArgumentException("The type must be an enum: " + type);
}
public void write (Kryo kryo, Output output, Enum object) {
if (object == null) {
output.writeVarInt(NULL, true);
return;
}
output.writeVarInt(object.ordinal() + 1, true);
}
public Enum read (Kryo kryo, Input input, Class extends Enum> type) {
int ordinal = input.readVarInt(true);
if (ordinal == NULL) return null;
ordinal--;
if (ordinal < 0 || ordinal > enumConstants.length - 1)
throw new KryoException("Invalid ordinal for enum \"" + type.getName() + "\": " + ordinal);
Object constant = enumConstants[ordinal];
return (Enum)constant;
}
}
public static class EnumSetSerializer extends Serializer {
public void write (Kryo kryo, Output output, EnumSet object) {
Serializer serializer;
if (object.isEmpty()) {
EnumSet tmp = EnumSet.complementOf(object);
if (tmp.isEmpty()) throw new KryoException("An EnumSet must have a defined Enum to be serialized.");
serializer = kryo.writeClass(output, tmp.iterator().next().getClass()).getSerializer();
} else {
serializer = kryo.writeClass(output, object.iterator().next().getClass()).getSerializer();
}
output.writeVarInt(object.size(), true);
for (Object element : object)
serializer.write(kryo, output, element);
}
public EnumSet read (Kryo kryo, Input input, Class extends EnumSet> type) {
Registration registration = kryo.readClass(input);
EnumSet object = EnumSet.noneOf(registration.getType());
Serializer serializer = registration.getSerializer();
int length = input.readVarInt(true);
for (int i = 0; i < length; i++)
object.add(serializer.read(kryo, input, null));
return object;
}
public EnumSet copy (Kryo kryo, EnumSet original) {
return EnumSet.copyOf(original);
}
}
/** @author Martin Grotzke */
public static class CurrencySerializer extends ImmutableSerializer {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, Currency object) {
output.writeString(object == null ? null : object.getCurrencyCode());
}
public Currency read (Kryo kryo, Input input, Class extends Currency> type) {
String currencyCode = input.readString();
if (currencyCode == null) return null;
return Currency.getInstance(currencyCode);
}
}
/** @author Martin Grotzke */
public static class StringBufferSerializer extends Serializer {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, StringBuffer object) {
output.writeString(object == null ? null : object.toString());
}
public StringBuffer read (Kryo kryo, Input input, Class extends StringBuffer> type) {
String value = input.readString();
if (value == null) return null;
return new StringBuffer(value);
}
public StringBuffer copy (Kryo kryo, StringBuffer original) {
return new StringBuffer(original);
}
}
/** @author Martin Grotzke */
public static class StringBuilderSerializer extends Serializer {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, StringBuilder object) {
output.writeString(object == null ? null : object.toString());
}
public StringBuilder read (Kryo kryo, Input input, Class extends StringBuilder> type) {
return input.readStringBuilder();
}
public StringBuilder copy (Kryo kryo, StringBuilder original) {
return new StringBuilder(original);
}
}
public static class KryoSerializableSerializer extends Serializer {
public void write (Kryo kryo, Output output, KryoSerializable object) {
object.write(kryo, output);
}
public KryoSerializable read (Kryo kryo, Input input, Class extends KryoSerializable> type) {
KryoSerializable object = kryo.newInstance(type);
kryo.reference(object);
object.read(kryo, input);
return object;
}
}
/** Serializer for lists created via {@link Collections#emptyList()} or that were just assigned the
* {@link Collections#EMPTY_LIST}.
* @author Martin Grotzke */
public static class CollectionsEmptyListSerializer extends ImmutableSerializer {
public void write (Kryo kryo, Output output, Collection object) {
}
public Collection read (Kryo kryo, Input input, Class extends Collection> type) {
return Collections.EMPTY_LIST;
}
}
/** Serializer for maps created via {@link Collections#emptyMap()} or that were just assigned the
* {@link Collections#EMPTY_MAP}.
* @author Martin Grotzke */
public static class CollectionsEmptyMapSerializer extends ImmutableSerializer © 2015 - 2025 Weber Informatics LLC | Privacy Policy