Skip to content

json -> ujson. - #336

Closed
ttezel wants to merge 1 commit into
aio-libs:masterfrom
ttezel:use_json
Closed

ttezel wants to merge 1 commit into
aio-libs:masterfrom
ttezel:use_json

Conversation

@ttezel

@ttezel ttezel commented Apr 20, 2015

Copy link
Copy Markdown

There are various benchmarks that show ujson beats json in performance. I wrote a very simple one here and posted the results there, it also showed ujson outperforming json. The tradeoff here is that ujson does not support indent and sort_keys functionality (for pretty-printing) but I think aiohttp should not worry about that since users can pretty-print if they want.

@ludovic-gasc

Copy link
Copy Markdown
Contributor

Hi,

In API-Hour plugin for aiohttp, I use ujson if available: https://github.com/Eyepea/API-Hour/blob/master/api_hour/plugins/aiohttp/__init__.py#L4

Nevertheless, the usage of ujson should be optional, because you can have issues to install ujson, json is included in Python.
Personally, with the override mechanism parameter, I've enough to customize the behaviour.
But, it should be a good idea to add a page in aiohttp documentation with some tips&tricks to improve performances with aiohttp.

@kxepal

kxepal commented Apr 20, 2015

Copy link
Copy Markdown
Member

-1
First, you'd forgot to add ujson into project dependencies.

Second, Windows users have to install it additionally.

Third, it brings completely different from stdlib json behaviour for happy debugging hours:

>>> import ujson
>>> ujson.encode(object())
{}
>>> ujson.encode(...)
{}

I'm +1 to make JSON library configurable at project-wide level, however. For instance, couchdb-python has a solution to manage custom json lib on fly. Then users will pick the implementation their like without passing additional parameters and patching the aiohttp.

@ludovic-gasc

Copy link
Copy Markdown
Contributor

@kxepal: Personally, I'm not a big fan to use global environment variable, this type of mechanism shot my foot several types on production because if you've forgotten this variable for X reasons, you have different behaviour between your VMs of prod, preprod, staging and dev: It's a nightmare to track this type of bugs. The developer has tested with another library than on production.

I'm more partisan to declare that explicitly by the developer, because you'll have a stacktrace directly at the startup of the daemon if the library used by the developer is missed.

Moreover, if in the same application you need to use another lib, because ujson can't transform data, you'll be happy to change that on runtime. I'd already have this issue with ujson, it isn't the silver bullet to replace json: you don't have always the same behaviour between both libraries.

With @asvetlov, during PyCON sprint code, we made something like that for aiopg to support psycopg2 and psycopg2cffi: https://github.com/Eyepea/aiopg/blob/psycopg2cffi_2nd_attempt/aiopg/connection.py#L42

Nevertheless, we may not need that, we should only add a parameter to Application to define default JSON loader globally, instead of by request, like we have now.

@kxepal

kxepal commented Apr 20, 2015

Copy link
Copy Markdown
Member

@GMLudo If you references to couchdb-python implementation, then environ variable is only one of the way to switch json module. It's also provides functional like json.use("simplejson").

However, I don't propose to take it as is, but to borrow the underlying idea. That idea is about to some proxy json module that is able to manage exact implementation behind it. How this would be implemented for aiohttp - that's a second sort question.

@asvetlov

Copy link
Copy Markdown
Member

I'm with @kxepal
ujson should be optional if any.
Exact api is a question. Please keep in mind we use json library not only for server but for client side also.

@ludovic-gasc

Copy link
Copy Markdown
Contributor

@kxepal: Thanks for the tip, I thought it was possible to choose Python library only via global environment.

@fafhrd91

Copy link
Copy Markdown
Member

-1 for dependency, but I like @kxepal approach

@asvetlov asvetlov closed this Jan 29, 2016
@asvetlov asvetlov reopened this Jan 29, 2016
@asvetlov asvetlov closed this Jan 29, 2016
@lock

lock Bot commented Oct 29, 2019

Copy link
Copy Markdown

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock Bot added the outdated label Oct 29, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants