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

com.google.code.morphia.converters.SerializedObjectConverter Maven / Gradle / Ivy

package com.google.code.morphia.converters;


import java.io.IOException;

import org.bson.types.Binary;
import com.google.code.morphia.annotations.Serialized;
import com.google.code.morphia.mapping.MappedField;
import com.google.code.morphia.mapping.MappingException;
import com.google.code.morphia.mapping.Serializer;


/**
 * @author Uwe Schaefer, ([email protected])
 */
public class SerializedObjectConverter extends TypeConverter {
  @Override
  protected boolean isSupported(final Class c, final MappedField optionalExtraInfo) {
    return optionalExtraInfo != null && (optionalExtraInfo.hasAnnotation(Serialized.class));
  }

  @Override
  public Object decode(final Class targetClass, final Object fromDBObject, final MappedField f) throws MappingException {
    if (fromDBObject == null) {
      return null;
    }

    if (!((fromDBObject instanceof Binary) || (fromDBObject instanceof byte[]))) {
      throw new MappingException(
        "The stored data is not a DBBinary or byte[] instance for " + f.getFullName() + " ; it is a " + fromDBObject.getClass().getName());
    }

    try {
      final boolean useCompression = !f.getAnnotation(Serialized.class).disableCompression();
      return Serializer.deserialize(fromDBObject, useCompression);
    } catch (IOException e) {
      throw new MappingException("While deserializing to " + f.getFullName(), e);
    } catch (ClassNotFoundException e) {
      throw new MappingException("While deserializing to " + f.getFullName(), e);
    }
  }

  @Override
  public Object encode(final Object value, final MappedField f) {
    if (value == null) {
      return null;
    }
    try {
      final boolean useCompression = !f.getAnnotation(Serialized.class).disableCompression();
      return Serializer.serialize(value, useCompression);
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy