Menu

#3 Faster writer

open
None
5
2005-05-18
2005-05-18
No

I'd appreciate a faster writer. Here's one that runs in
75% of current in simple timeit tests:

def write(obj):
if isinstance(obj, dict):
items = [":".join((write(key), write(val)))
for key, val in obj.iteritems()]
chunk = ["{", ",".join(items), "}"]
elif isinstance(obj, list):
items = [write(item) for item in obj]
chunk = ["[", ",".join(items), "]"]
elif isinstance(obj, basestring):
chunk = ['"', obj, '"']
elif isinstance(obj, float):
return "%f" % obj
elif isinstance(obj, int):
if obj is True:
return "true"
elif obj is False:
return "false"
return str(obj)
elif obj is None:
return "null"
else:
raise ValueError("Not representable in JSON:
%s" % obj)
return "".join(chunk)

Look ma, no "import types"! :)

Discussion

  • Robert Brewer

    Robert Brewer - 2005-05-18

    Faster JSON writer

     
  • Robert Brewer

    Robert Brewer - 2005-05-18
    • assigned_to: nobody --> patrickdlogan
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.