import re
import json
from datetime import datetime

from . import Serializer


class CellarDoorJSONEncoder(json.JSONEncoder):
	
	def default(self, obj):
		try:
			iterable = iter(obj)
		except TypeError:
			pass
		else:
			return list(iterable)
		
		if isinstance(obj, datetime):
			return obj.isoformat()
		
		return super(CellarDoorJSONEncoder, self).default(obj)
		
		
def as_date(obj):
	if '_date' in obj:
		return datetime(*map(int, re.split('[^\d]', obj['_date'])[:-1]))
	else:
		return obj
		


class JSONSerializer(Serializer):
	
	mimetype = 'application/json'
	
	def serialize(self, obj):
		return json.dumps(obj, cls=CellarDoorJSONEncoder)
		
		
	def unserialize(self, stream):
		return json.load(stream, object_hook=as_date)
		
		
	def unserialize_string(self, data):
		return json.loads(data, object_hook=as_date)
@@ -44,16 +44,35 @@
 datetime
+%0Aimport collections
 %0A%0Afrom .
@@ -172,71 +172,48 @@
 :%0A%09%09
-try:%0A%09%09%09iterable = iter(obj)%0A%09%09except TypeError:%0A%09%09%09pass%0A%09%09else
+if isinstance(obj, collections.Iterable)
 :%0A%09%09
@@ -229,16 +229,11 @@
 ist(
-iterable
+obj
 )%0A%09%09

Use more reliable method of detecting iterables