google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

Add support for value converters #28

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice if there was possibility to transparently convert types 
not supported by Datastore to it's core types. 
For example, we have problem if we use BigDecimal. In datastore we can 
persist a number only as double or long, what can cause loss of precision. 
Converting BigDecimal to string will do the trick, but problem is that we 
have to write conversion logic in our model classes.

My vision for resolution of this problem:

Objectify defines a simple interface for value converter:

interface ValueConverter {
Object toDatastoreValue(Object v);
Object fromDatastoreValue(Object v);
}

We write simple converter for BigDecimal (we can write more sophisticated 
one which will prevent order)

class BigDecimalConverter {
Object toDatastoreValue(Object v) {
   return ((BigDecimal)v).toPlainString();
}
Object fromDatastoreValue(Object v) {
   return new BigDecimal((String)v);
}
}

We register our converter:

factory.register(java.math.BigDecimal.class, new BigDecimalConverter());

Now we simply use BigDecimal fields in our classes and Objectify does the 
rest.

Original issue reported on code.google.com by szcze...@gmail.com on 28 Mar 2010 at 6:52

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 28 Mar 2010 at 7:07

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 30 Jun 2010 at 1:53

GoogleCodeExporter commented 9 years ago
Something similar to this interface is now implemented in trunk.  Look for 
ObjectifyFactory.getConversions().

Original comment by lhori...@gmail.com on 22 Oct 2010 at 6:08