google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

@Converters annotation does not work, bypassed by PassthroughConverter #412

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Morphia v0.99.

Using @Converters does not work, because the built-in PassthroughConverter is 
always chosen. Custom converters should be ordered first.

Example:

@Embedded
@Converters({BigDecimalConverter.class})
public class BookingDetail {
    private String id;
    private String productId;
    private Long magentoProductId;
    private String shopId;
    private String currencyId;
    private BigDecimal price; // exception when decoding this property
    private Double qty;
    private Date created;
    private Date lastModified;
    private Date expire;
    private String status;
    private String name;
    private String localSku;
    private String attributes;

Original issue reported on code.google.com by atang.su...@gmail.com on 11 Jun 2012 at 10:56

GoogleCodeExporter commented 9 years ago
The converters aren't chosen by the order they were added but by rather by the 
type they can convert. I created a BigDecimal converter which works fine.

public class BigDecimalConverter extends TypeConverter implements 
SimpleValueConverter {

    public BigDecimalConverter() {
        super(BigDecimal.class);
    }

    @Override
    public Object encode(Object value, MappedField optionalExtraInfo) {
        return value.toString();
    }

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

        return new BigDecimal(fromDBObject.toString());
    }
}

Original comment by sebastia...@otto.de on 8 Jul 2012 at 10:08

GoogleCodeExporter commented 9 years ago
Sebastian, thanks!
Adding:

implements SimpleValueConverter

then Changing :

    @Override
    protected boolean isSupported(Class<?> c, MappedField optionalExtraInfo) {
        return BigDecimal.class.isAssignableFrom(c);
    }

to:

    public BigDecimalConverter() {
        super(BigDecimal.class);
    }

Made it work!

Original comment by ceefour666@gmail.com on 10 Sep 2012 at 10:33

GoogleCodeExporter commented 9 years ago
Dear developer: Please mark this issue as "Invalid"

Original comment by ceefour666@gmail.com on 10 Sep 2012 at 10:36