Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 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 static com.esotericsoftware.minlog.Log.*;
 
 
Serializes objects using direct field assignment, with versioning backward compatibility. Allows fields to have a @Since(int) annotation to indicate the version they were added. For a particular field, the value in @Since should never change once created. This is less flexible than FieldSerializer, which can handle most classes without needing annotations, but it provides backward compatibility. This means that new fields can be added, but removing, renaming or changing the type of any field will invalidate previous serialized bytes. VersionFieldSerializer has very little overhead (a single additional varint) compared to FieldSerializer. Forward compatibility is not supported.

Author(s):
Tianyi HE <hty0807@gmail.com>
See also:
TaggedFieldSerializer
 
 public class VersionFieldSerializer<T> extends FieldSerializer<T> {
 	private int typeVersion = 0; // Version of current type.
 	private int[] fieldVersion// Version of each field.
 	private boolean compatible = true// Whether current type is compatible with serialized objects with different version.
 
 	public VersionFieldSerializer (Kryo kryoClass type) {
 		super(kryotype);
 		// Make sure this is done before any read / write operations.
 	}
 
 	public VersionFieldSerializer (Kryo kryoClass typeboolean compatible) {
 		this(kryotype);
 		this. = compatible;
 	}
 
 	protected void initializeCachedFields () {
 		CachedField[] fields = getFields();
 		 = new int[fields.length];
 		for (int i = 0, n = fields.lengthi < ni++) {
 			Field field = fields[i].getField();
 			Since since = field.getAnnotation(Since.class);
 			if (since != null) {
 				[i] = since.value();
 				// Use the maximum version among fields as the entire type's version.
 			} else {
 				[i] = 0;
 			}
 		}
 		if (debug("Version for type " + getType().getName() + " is " + );
 	}
 
 	public void removeField (String fieldName) {
 		super.removeField(fieldName);
 	}
 
 	public void removeField (CachedField field) {
 		super.removeField(field);
 	}
 
 	public void write (Kryo kryoOutput output, T object) {
 		CachedField[] fields = getFields();
 		// Write type version.
 		output.writeVarInt(true);
 		// Write fields.
 		for (int i = 0, n = fields.lengthi < ni++) {
 			fields[i].write(outputobject);
 		}
 	}
	public T read (Kryo kryoInput inputClass<T> type) {
object = create(kryoinputtype);
		kryo.reference(object);
		// Read input version.
		int version = input.readVarInt(true);
		if (! && version != ) {
			// Reject to read
			throw new KryoException("Version not compatible: " + version + " <-> " + );
		}
		CachedField[] fields = getFields();
		for (int i = 0, n = fields.lengthi < ni++) {
			// Field is not present in input, skip it.
			if ([i] > version) {
				if (debug("Skip field " + fields[i].getField().getName());
				continue;
			}
			fields[i].read(inputobject);
		}
		return object;
	}

Incremental modification of serialized objects must add VersionFieldSerializer.Since for new fields.
	public @interface Since {
Version of annotated field, default is 0, and must be incremental to maintain compatibility.
		int value() default 0;
	}
New to GrepCode? Check out our FAQ X