Commits

Rune Halvorsen  committed 80c3c2e

Added some docstrings
Removed custom exception in lieu of built-in ones

  • Participants
  • Parent commits 1a8c9f6

Comments (0)

Files changed (1)

 _ser_error = None
 _des_error = None
 
+"""
+.. 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.
+"""
 _modules = [("cjson", "encode", "EncodeError", "decode", "DecodeError"),
             ("jsonlib2", "write", "WriteError", "read", "ReadError"),
             ("jsonlib", "write", "WriteError", "read", "ReadError"),
 
 
 def _attempt_load(modname, encname, encerror, decname, decerror):
+    """Tries to load a module and assign the enc/dec stuff to globals"""
     try:
         global _serialize, _deserialize, _ser_error, _des_error
         global implementation
 
 
 def force_implementation(modname):
+    """Forces anyjson to use a specific json module if it's available"""
     for name, spec in [(e[0], e) for e in _modules]:
         print "spce:", spec
         if name == modname and _attempt_load(*spec):
     raise ImportError(modname)
 
 
-class JsonValueError(ValueError):
-    pass
+def serialize(data):
+    """Serialise the object data into a json string"""
+    try:
+        return _serialize(data)
+    except _ser_error:
+        raise TypeError()
 
 
-class JsonTypeError(TypeError):
-    pass
-
-
-def serialize(s):
+def deserialize(string):
+    """Deserialize a string of json into python data types"""
     try:
-        return _serialize(s)
-    except _ser_error:
-        raise JsonTypeError()
-
-
-def deserialize(s):
-    try:
-        return _deserialize(s)
+        return _deserialize(string)
     except _des_error:
-        raise JsonValueError()
+        raise ValueError()
 
 for modspec in _modules:
     if _attempt_load(*modspec):