On 24/02/2012 10:49, Chris Withers wrote:
(Does SA have a JSONType now? we're using a homebrew one, that may be
where the problem lies...)

For reference, I've included the code for this type at the end of this email, it uses MutableType, so the following should work:

obj = session.query(MyModel).one()
obj.config['foo'] += 1
session.commit()

...on my dev machine, the change gets persisted.
Of course, on production machines, it does not :-(

Are there known bugs in this? I see there's now a "new way" in 0.7, but that means you have to know the type of the object stored in the column rather than just "any json-serializable value" being okay.

cheers,

Chris

PS:

from sqlalchemy import String
from sqlalchemy.types import  MutableType, TypeDecorator

import simplejson

class JSONType(MutableType, TypeDecorator):
    """Column type which stores Python objects in JSON form.

    PickleType applies simplejson's ``dumps()`` to incoming objects,
    and ``loads()`` on the way out, allowing simple Python objects to
    be stored as a serialized text field.

    As it extends MutableType, changes in mutable objects will be
    detected and handled correctly. It is safe to use this column type
    to store lists, dicts and other mutable types.
    """

    impl = String

    def process_bind_param(self, value, _):
        if value is None:
            return None
        return self._dumps(value)

    def process_result_value(self, value, _):
        if value is None:
            return None
        return self._loads(value)

    def copy_value(self, value):
        return self._loads(self._dumps(value))

    def is_mutable(self):
        return True

    def _loads(self, strvalue):
        return simplejson.loads(str(strvalue), use_decimal=True)

    def _dumps(self, value):
        return simplejson.dumps(value, use_decimal=True)

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to