public <T> T toObject(final Object object) {
    if (!(object instanceof Data)) {
      return (T) object;
    }

    Data data = (Data) object;

    if (data.bufferSize() == 0 && data.isDataSerializable()) {
      return null;
    }
    try {
      final int typeId = data.type;
      final SerializerAdapter serializer = serializerFor(typeId);
      if (serializer == null) {
        if (active) {
          throw new HazelcastSerializationException(
              "There is no suitable de-serializer for type " + typeId);
        }
        throw new HazelcastInstanceNotActiveException();
      }
      if (typeId == SerializationConstants.CONSTANT_TYPE_PORTABLE) {
        serializationContext.registerClassDefinition(data.classDefinition);
      }
      Object obj = serializer.read(data);
      if (managedContext != null) {
        obj = managedContext.initialize(obj);
      }
      return (T) obj;
    } catch (Throwable e) {
      handleException(e);
    }
    return null;
  }
 @SuppressWarnings("unchecked")
 public Data toData(Object obj, PartitioningStrategy strategy) {
   if (obj == null) {
     return null;
   }
   if (obj instanceof Data) {
     return (Data) obj;
   }
   try {
     final SerializerAdapter serializer = serializerFor(obj.getClass());
     if (serializer == null) {
       if (active) {
         throw new HazelcastSerializationException(
             "There is no suitable serializer for " + obj.getClass());
       }
       throw new HazelcastInstanceNotActiveException();
     }
     final byte[] bytes = serializer.write(obj);
     final Data data = new Data(serializer.getTypeId(), bytes);
     if (obj instanceof Portable) {
       final Portable portable = (Portable) obj;
       data.classDefinition =
           serializationContext.lookup(portable.getFactoryId(), portable.getClassId());
     }
     if (strategy == null) {
       strategy = globalPartitioningStrategy;
     }
     if (strategy != null) {
       Object pk = strategy.getPartitionKey(obj);
       if (pk != null && pk != obj) {
         final Data partitionKey = toData(pk, EMPTY_PARTITIONING_STRATEGY);
         data.partitionHash = (partitionKey == null) ? -1 : partitionKey.getPartitionHash();
       }
     }
     return data;
   } catch (Throwable e) {
     handleException(e);
   }
   return null;
 }
 public PortableReader createPortableReader(Data data) {
   return new DefaultPortableReader(
       portableSerializer, createObjectDataInput(data), data.getClassDefinition());
 }