diff options
author | Ask Solem <ask@rabbitmq.com> | 2011-12-06 12:23:48 +0000 |
---|---|---|
committer | Ask Solem <ask@rabbitmq.com> | 2011-12-06 12:23:48 +0000 |
commit | b8c8c8a7076d8ecb932dad0c88e88137f6ee86e1 (patch) | |
tree | 44a4ffb964e58853d3ef349c495fc3105f3bf20f | |
parent | 5cd8a78dfee4cfa59849b17cd8b9c8efd740d797 (diff) | |
download | anyjson-b8c8c8a7076d8ecb932dad0c88e88137f6ee86e1.tar.gz |
Renames serialize/deserialize to dumps/loads
-rw-r--r-- | anyjson/__init__.py | 52 | ||||
-rw-r--r-- | tests/test_implementations.py | 13 |
2 files changed, 27 insertions, 38 deletions
diff --git a/anyjson/__init__.py b/anyjson/__init__.py index 6516249..e7974b0 100644 --- a/anyjson/__init__.py +++ b/anyjson/__init__.py @@ -12,33 +12,14 @@ __docformat__ = "restructuredtext" # -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"), @@ -86,21 +67,23 @@ class _JsonImplementation(object): __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): @@ -130,7 +113,14 @@ else: 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 diff --git a/tests/test_implementations.py b/tests/test_implementations.py index ceec00c..a884459 100644 --- a/tests/test_implementations.py +++ b/tests/test_implementations.py @@ -4,13 +4,13 @@ import anyjson modnames = [e[0] for e in anyjson._modules] def test_default_serialization(): - assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]" assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]" + assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]" def test_default_deserialization(): - assert anyjson.deserialize("[1,2,3]") == [1,2,3] assert anyjson.loads("[1,2,3]") == [1,2,3] + assert anyjson.deserialize("[1,2,3]") == [1,2,3] def test_forced_serialization(): @@ -20,8 +20,8 @@ def test_forced_serialization(): except ImportError: continue # module can't be tested, try next - assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]" assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]" + assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]" def test_forced_deserialization(): @@ -31,8 +31,8 @@ def test_forced_deserialization(): except ImportError: continue # module can't be tested, try next - assert anyjson.deserialize("[1,2,3]") == [1,2,3] assert anyjson.loads("[1,2,3]") == [1,2,3] + assert anyjson.deserialize("[1,2,3]") == [1,2,3] def test_exceptions(): @@ -42,8 +42,7 @@ def test_exceptions(): except ImportError: continue # module can't be tested, try next - assert_raises(TypeError, anyjson.serialize, [object()]) assert_raises(TypeError, anyjson.dumps, [object()]) - assert_raises(ValueError, anyjson.deserialize, "[") + assert_raises(TypeError, anyjson.serialize, [object()]) assert_raises(ValueError, anyjson.loads, "[") - + assert_raises(ValueError, anyjson.deserialize, "[") |