Merge branch 'newlayout' into newlayout-stage
[mediagoblin.git] / mediagoblin / db / sql / extratypes.py
index 3a594728bf5d4785aba387b2df93ecf6bb819fcf..8e078f14a32100d6ea38d43fb02545d8d6224f1f 100644 (file)
@@ -15,7 +15,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from sqlalchemy.types import TypeDecorator, Unicode
+from sqlalchemy.types import TypeDecorator, Unicode, VARCHAR
+import json
 
 
 class PathTupleWithSlashes(TypeDecorator):
@@ -35,3 +36,28 @@ class PathTupleWithSlashes(TypeDecorator):
         if value is not None:
             value = tuple(value.split('/'))
         return value
+
+
+# The following class and only this one class is in very
+# large parts based on example code from sqlalchemy.
+#
+# The original copyright notice and license follows:
+#     Copyright (C) 2005-2011 the SQLAlchemy authors and contributors <see AUTHORS file>
+#
+#     This module is part of SQLAlchemy and is released under
+#     the MIT License: http://www.opensource.org/licenses/mit-license.php
+#
+class JSONEncoded(TypeDecorator):
+    "Represents an immutable structure as a json-encoded string."
+
+    impl = VARCHAR
+
+    def process_bind_param(self, value, dialect):
+        if value is not None:
+            value = json.dumps(value)
+        return value
+
+    def process_result_value(self, value, dialect):
+        if value is not None:
+            value = json.loads(value)
+        return value