Max Noel committed e0c447c

* Split DynamoDBModel.to_dict() into to_db_dict() and to_json_dict(), since everyone (and most importantly Ludia) is doing JSON serialization, which doesn't have sets.

Comments (0)

Files changed (3)


     To redefine serialization/deserialization semantics (e.g. to have more
     complex schemas, like auto-serialized JSON data structures), override the
-    from_dict (deserialization) and to_dict (serialization) methods.
+    from_dict (deserialization) and to_db_dict (serialization) methods.
     *Important implementation note regarding sets:* DynamoDB can't store empty
     sets. Therefore, since we have schema information available to us, we're
         for (name, type_) in self.__schema__.iteritems():
             setattr(self, name, type_())
-    def to_dict(self):
-        """Return a dict representation of the object,
-        according to the class's schema.
+    def to_db_dict(self):
+        """Return a dict representation of the object according to the class's
+        schema, suitable for direct storage in DynamoDB.
+        This means the values must all be numbers, strings or sets thereof.
         return {name: getattr(self, name) for name in self.__schema__}
+    def to_json_dict(self):
+        """Return a dict representation of the object, suitable for JSON
+        serialization.
+        This means the values must all be valid JSON object types
+        (in particular, sets must be converted to lists), but types not
+        suitable for DynamoDB (e.g. nested data structures) may be used.
+        Note that this method is never used for interaction with the database
+        (:meth:`to_db_dict` is).
+        """
+        out = {}
+        for name in self.__schema__:
+            value = getattr(self, name)
+            if isinstance(value, (set, frozenset)):
+                out[name] = list(value)
+            else:
+                out[name] = value
+        return out
     def _save_autoincrement_hash_key(self, item):
         """Compute an autoincremented hash_key for an item and save it to the DB.
         # Yes, that part is horrible. DynamoDB can't store empty sets,
         # so we're representing them as missing attributes on the DB side.
         item_data = {
-            key: value for (key, value) in self.to_dict().iteritems() if (
+            key: value for (key, value) in self.to_db_dict().iteritems() if (
                 value or not isinstance(value, (set, frozenset)))
         item = Item(table, attrs=item_data)


         self.assertEquals(, 0)
         self.assertEquals(, u"")
-        d_dict = d.to_dict()
+        d_dict = d.to_db_dict()
 name = dynamodb-mapper
-version = 1.0.2
+version = 1.1.0
 summary = Object mapper for Amazon DynamoDB
 description_file = README.rst
 author = Max Noel
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.