Commits

Jason R. Coombs committed a63d4ed

Fixed issue where nested objects were being doubly-serialized

  • Participants
  • Parent commits 35535b0

Comments (0)

Files changed (2)

jaraco/json/main.py

 Also, custom objects can be encoded.
 >>> MyCustom = type('MyCustom', (), dict()) # an simple class
 >>> encode(MyCustom())
-'{"__python_module__": "jaraco.json.main", "state": "{}", "args": "[]", "__python_class__": "MyCustom"}'
+'{"__python_module__": "jaraco.json.main", "state": {}, "args": [], "__python_class__": "MyCustom"}'
 
 """
 
 	the pickle module works.
 	"""
 	def default(self, object):
+		if object.__class__.__module__ == '__builtin__':
+			# Builtin objects are handled in the parent
+			return object
 		# use the pickle protocol 2 to serialize the object
 		reduced = object.__reduce_ex__(2)
 		return self.save_reduce(*reduced, obj = object)
 		return dict(
 			__python_class__ = cls.__name__,
 			__python_module__ = cls.__module__,
-			args = self.encode(args),
-			state = self.encode(state),
+			args = self.default(args),
+			state = self.default(state),
 			)
 
 encode = GenericEncoder().encode
 		return object
 	class_name = object['__python_class__']
 	mod_name = object['__python_module__']
-	args = decode(object['args'])
-	state = decode(object['state'])
+	args = object['args']
+	state = object['state']
 	mod = _find_module_by_name(mod_name)
 	cls = getattr(mod, class_name)
 	ob = cls.__new__(cls, *args)

jaraco/json/test_encoding.py

 
 class Satchel(object):
 	"""
-	A collection of items, specifically not subclassed from list to demonstrate
-	a custom collection.
+	A collection of items, specifically not subclassed from list to
+	demonstrate a custom collection.
 	"""
 	def __init__(self):
 		self.items = []