Use of ultrajson considered harmful

Someone at work got the idea that we should use this Python JSON library called ultrajson because apparently the standard Python json library was slow, or something. Well, we did end up using it, and it turned out to be a Bad Idea.

The standard JSON library in Python, which ujson is intended to be a drop-in replacement for, handles errors in a sane and Pythonic way. For example, if you attempt to serialize something that obviously doesn’t have a reasonable JSON representation, such as a Python function:

>>> def b():
...  pass
... 
>>> json.dumps({'test': b})
Traceback (most recent call last):
  File "<stdin>", line 1, in 
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function b at 0x1056b3398> is not JSON serializable

TypeError, of course. The programmer immediately realizes what the problem is and how to fix it.

Let’s see what ultrajson does with, say, a Python object.

>>> class Test(object):
...  def t(self):
...   print "hello"
... 
>>> t = Test()
>>> ujson.dumps({'a': t})
'{"a":{}}'

Yep, it apparently serializes a lot of things it doesn’t want to deal with as empty JSON objects. Silent failure, the king of error handling. We had a funny bug caused by this; thankfully it never made it to production.

What if I try to serialize something else? Like, say, a function?

>>> def b():
...  pass
... 
>>> ujson.dumps({'a': b})
Traceback (most recent call last):
  File "<stdin>", line 1, in 
OverflowError: Maximum recursion level reached

Wonderful. I hope I don’t have to explain why this stinks and how much. Please spread the word and save the world from the potential bugs caused by this terrible library.

Comments (5)

  1. Anon wrote:

    Holy crap

    You’re alive

    Saturday, August 3, 2013 at 13:52 #
  2. Noob wrote:

    You should blog more frequently :)

    Monday, August 5, 2013 at 17:36 #
  3. taargus taargus wrote:

    you should talk for about encoding so i don’t have to talk to daiz. no one should have to talk to daiz.

    Saturday, October 12, 2013 at 04:07 #
  4. Good news for us! we honestly support you in this idea,welcome to come to our web also,you will find good news too.Our web provide absolutely cheap price goods for you,welcome you always.
    canada goose cheap promotion http://www.snowjacketsoutlet.com/

    Saturday, October 19, 2013 at 15:44 #
  5. Paul Wise wrote:

    I filed a bug about this, please comment there:

    https://github.com/esnme/ultrajson/issues/134

    Wednesday, May 28, 2014 at 05:03 #