com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer Maven / Gradle / Ivy
Show all versions of kryo-shaded Show documentation
/* Copyright (c) 2008, 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 com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.InputChunked;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.io.OutputChunked;
import com.esotericsoftware.kryo.util.ObjectMap;
import static com.esotericsoftware.minlog.Log.*;
/** Serializes objects using direct field assignment, with limited support for forward and backward compatibility. Fields can be
* added or removed without invalidating previously serialized bytes. Note that changing the type of a field is not supported.
*
* Removing fields when {@link Kryo#setReferences(boolean) references} are enabled can cause compatibility issues. See here.
*
* There is additional overhead compared to {@link FieldSerializer}. A header is output the first time an object of a given type
* is serialized. The header consists of an int for the number of fields, then a String for each field name. Also, to support
* skipping the bytes for a field that no longer exists, for each field value an int is written that is the length of the value in
* bytes.
*
* Note that the field data is identified by name. The situation where a super class has a field with the same name as a subclass
* must be avoided.
* @author Nathan Sweet */
public class CompatibleFieldSerializer extends FieldSerializer {
public CompatibleFieldSerializer (Kryo kryo, Class type) {
super(kryo, type);
}
public void write (Kryo kryo, Output output, T object) {
CachedField[] fields = getFields();
ObjectMap context = kryo.getGraphContext();
if (!context.containsKey(this)) {
context.put(this, null);
if (TRACE) trace("kryo", "Write " + fields.length + " field names.");
output.writeVarInt(fields.length, true);
for (int i = 0, n = fields.length; i < n; i++)
output.writeString(fields[i].field.getName());
}
OutputChunked outputChunked = new OutputChunked(output, 1024);
for (int i = 0, n = fields.length; i < n; i++) {
fields[i].write(outputChunked, object);
outputChunked.endChunks();
}
}
public T read (Kryo kryo, Input input, Class type) {
T object = create(kryo, input, type);
kryo.reference(object);
ObjectMap context = kryo.getGraphContext();
CachedField[] fields = (CachedField[])context.get(this);
if (fields == null) {
int length = input.readVarInt(true);
if (TRACE) trace("kryo", "Read " + length + " field names.");
String[] names = new String[length];
for (int i = 0; i < length; i++)
names[i] = input.readString();
fields = new CachedField[length];
CachedField[] allFields = getFields();
outer:
for (int i = 0, n = names.length; i < n; i++) {
String schemaName = names[i];
for (int ii = 0, nn = allFields.length; ii < nn; ii++) {
if (allFields[ii].field.getName().equals(schemaName)) {
fields[i] = allFields[ii];
continue outer;
}
}
if (TRACE) trace("kryo", "Ignore obsolete field: " + schemaName);
}
context.put(this, fields);
}
InputChunked inputChunked = new InputChunked(input, 1024);
boolean hasGenerics = getGenerics() != null;
for (int i = 0, n = fields.length; i < n; i++) {
CachedField cachedField = fields[i];
if (cachedField != null && hasGenerics) {
// Generic type used to instantiate this field could have
// been changed in the meantime. Therefore take the most
// up-to-date definition of a field
cachedField = getField(cachedField.field.getName());
}
if (cachedField == null) {
if (TRACE) trace("kryo", "Skip obsolete field.");
inputChunked.nextChunks();
continue;
}
cachedField.read(inputChunked, object);
inputChunked.nextChunks();
}
return object;
}
}