All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.pdx.PdxSerializer Maven / Gradle / Ivy

/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.pdx;

/**
 * The PdxSerializer interface allows domain classes to be 
 * serialized and deserialized as PDXs without modification 
 * of the domain class. It only requires that the domain class 
 * have a public zero-arg constructor and that it provides read and write access
 * to the PDX serialized fields.
 * 

GemFire allows a single PdxSerializer to be configured on a cache * using {@link com.gemstone.gemfire.cache.CacheFactory#setPdxSerializer(PdxSerializer) setPdxSerializer} * or {@link com.gemstone.gemfire.cache.client.ClientCacheFactory#setPdxSerializer(PdxSerializer) client setPdxSerializer}. * It can also be configured in cache.xml using the pdx-serializer element. * The same PdxSerializer should be configured on each member of a distributed * system that can serialize or deserialize PDX data. *

The {@link #toData toData} method is used for serialization; {@link #fromData fromData} is used for deserialization. * The order in which fields are serialized and deserialized in these methods for the same class must match. * For the same class toData and fromData must write the same fields each time they are called. *

If you can modify your domain class then use {@link PdxSerializable} instead. * *

Simple example: *

public class User {
  final public String name;
  final public int userId;

  public User(String name, int userId) {
    this.name = name;
    this.userId = userId;
  }
}

public class MyPdxSerializer implements PdxSerializer {
  public boolean toData(Object o, PdxWriter out) {
    if (o instanceof User) {
      User u = (User)o;
      out.writeString("name", u.name);
      out.writeInt("userId", u.userId);
      return true;
    } else {
      return false;
    }
  }
  public Object fromData(Class clazz, PdxReader in) {
    if (User.class.isAssignableFrom(clazz)) {
      return new User(in.readString("name"), in.readInt("userId"));
    } else {
      return null;
    }
  }
}
 * 
* * @author darrel * @since 6.6 */ public interface PdxSerializer { /** * This method is given an object to serialize as a PDX using the given writer. * If it chooses to do the serialization then it must return true; * otherwise it must return false in which case it will be serialized using * standard serialization. * * @param o the object to consider serializing as a PDX * @param out the {@link PdxWriter} to use to serialize the object * @return true if the method serialized the object; otherwise false */ public boolean toData(Object o, PdxWriter out); /** * This method is given an class that should be * instantiated and deserialized using the given reader. * @param clazz the Class of the object that should be deserialized * @param in the reader to use to obtain the field data * @return the deserialized object. null indicates that this * PdxSerializer does not know how to deserialize the given class. */ public Object fromData(Class clazz, PdxReader in); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy