@Override public Object decode(Class targetClass, Object fromDBObject, 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 { 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); } }
public void fromDBObject(final DBObject dbObj, final MappedField mf, final Object targetEntity) { Object object = mf.getDbObjectValue(dbObj); if (object == null) { processMissingField(mf); } else { TypeConverter enc = getEncoder(mf); Object decodedValue = enc.decode(mf.getType(), object, mf); try { mf.setFieldValue(targetEntity, decodedValue); } catch (IllegalArgumentException e) { throw new MappingException( "Error setting value from converter (" + enc.getClass().getSimpleName() + ") for " + mf.getFullName() + " to " + decodedValue); } } }
private TypeConverter getEncoder(Object val, MappedField mf) { List<TypeConverter> tcs = null; if (val != null) tcs = tcMap.get(val.getClass()); if (tcs == null || (tcs.size() > 0 && tcs.get(0) instanceof PassthroughConverter)) tcs = tcMap.get(mf.getType()); if (tcs != null) { if (tcs.size() > 1) log.warning( "Duplicate converter for " + mf.getType() + ", returning first one from " + tcs); return tcs.get(0); } for (TypeConverter tc : untypedTypeEncoders) if (tc.canHandle(mf) || (val != null && tc.isSupported(val.getClass(), mf))) return tc; throw new ConverterNotFoundException( "Cannot find encoder for " + mf.getType() + " as need for " + mf.getFullName()); }