Anonymous avatar Anonymous committed 4e162c4

Renames serialize/deserialize to dumps/loads

Comments (0)

Files changed (2)

anyjson/__init__.py

 
 # -eof meta-
 
+#: The json implementation object. This is probably not useful to you,
+#: except to get the name of the implementation in use. The name is
+#: available through ``implementation.name``.
 implementation = None
 
-"""
-.. function:: serialize(obj)
-
-    Serialize the object to JSON.
-
-.. function:: deserialize(str)
-
-    Deserialize JSON-encoded object to a Python object.
-
-.. function:: force_implementation(name)
-
-    Load a specific json module. This is useful for testing and not much else
-
-.. attribute:: implementation
-
-    The json implementation object. This is probably not useful to you,
-    except to get the name of the implementation in use. The name is
-    available through `implementation.name`.
-
-.. data:: _modules
-
-    List of known json modules, and the names of their serialize/unserialize
-    methods, as well as the exception they throw. Exception can be either
-    an exception class or a string.
-"""
+#: List of known json modules, and the names of their loads/dumps
+#: methods, as well as the exceptions they throw.  Exception can be either
+#: an exception class or a string.
 _modules = [("yajl", "dumps", TypeError, "loads", ValueError),
             ("jsonlib2", "write", "WriteError", "read", "ReadError"),
             ("jsonlib", "write", "WriteError", "read", "ReadError"),
         __import__(modname)
         return sys.modules[modname]
 
-    def serialize(self, data):
+    def dumps(self, data):
         """Serialize the datastructure to json. Returns a string. Raises
         TypeError if the object could not be serialized."""
         try:
             return self._encode(data)
         except self._encode_error, exc:
             raise TypeError(*exc.args)
+    serialize = dumps
 
-    def deserialize(self, s):
+    def loads(self, s):
         """deserialize the string to python data types. Raises
         ValueError if the string vould not be parsed."""
         try:
             return self._decode(s)
         except self._decode_error, exc:
             raise ValueError(*exc.args)
+    deserialize = loads
 
 
 def force_implementation(modname):
     else:
         raise ImportError("No supported JSON module found")
 
-    serialize = lambda value: implementation.serialize(value)
-    deserialize = lambda value: implementation.deserialize(value)
-    dumps = serialize
-    loads = deserialize
+
+    def loads(value):
+        """Serialize the object to JSON."""
+        return implementation.loads(value)
+    deserialize = loads   # compat
+
+
+    def dumps(value):
+        """Deserialize JSON-encoded object to a Python object."""
+        return implementation.dumps(value)
+    serialize = dumps

tests/test_implementations.py

 modnames = [e[0] for e in anyjson._modules]
 
 def test_default_serialization():
+    assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
     assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]"
-    assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
 
 
 def test_default_deserialization():
+    assert anyjson.loads("[1,2,3]") == [1,2,3]
     assert anyjson.deserialize("[1,2,3]") == [1,2,3]
-    assert anyjson.loads("[1,2,3]") == [1,2,3]
 
 
 def test_forced_serialization():
         except ImportError:
             continue # module can't be tested, try next
 
+        assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
         assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]"
-        assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
 
 
 def test_forced_deserialization():
         except ImportError:
             continue # module can't be tested, try next
 
+        assert anyjson.loads("[1,2,3]") == [1,2,3]
         assert anyjson.deserialize("[1,2,3]") == [1,2,3]
-        assert anyjson.loads("[1,2,3]") == [1,2,3]
 
 
 def test_exceptions():
         except ImportError:
             continue # module can't be tested, try next
 
+        assert_raises(TypeError, anyjson.dumps, [object()])
         assert_raises(TypeError, anyjson.serialize, [object()])
-        assert_raises(TypeError, anyjson.dumps, [object()])
+        assert_raises(ValueError, anyjson.loads, "[")
         assert_raises(ValueError, anyjson.deserialize, "[")
-        assert_raises(ValueError, anyjson.loads, "[")
-
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 ProjectModifiedEvent.java.
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.