from cgi import escape as html_escape
from datetime import datetime, timedelta
from decimal import Decimal
+from collections import namedtuple
# Map of special chars to ascii from Darkplace's console.c.
_qfont_table = [
def datetime_seconds(td):
return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
-def namedtuple_to_dict(tup):
- result = {}
- for key,value in tup._asdict().items():
- result[key] = value
- return result
-
-def fix_json_types(data):
+def to_json(data):
+ if not type(data) == dict:
+ # assume it's a named tuple
+ data = data._asdict()
result = {}
for key,value in data.items():
- if type(value) == Decimal:
+ if value == None:
+ result[key] = None
+ elif type(value) in [bool,int,long,float,complex,str]:
+ result[key] = value
+ elif type(value) == unicode:
+ result[key] = value.encode('utf-8')
+ elif type(value) == Decimal:
result[key] = float(value)
elif type(value) == datetime:
result[key] = value.strftime('%Y-%m-%dT%H:%M:%SZ')
elif type(value) == timedelta:
result[key] = datetime_seconds(value)
else:
- result[key] = value
+ print key,value
+ result[key] = to_json(value.to_dict())
return result