Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Update default json library #1479
Comments
|
I must admit, I am not familiar with However, it is still a young project (not that I think a better approach is just allowing the developer to pass in their own import my_favorite_json
from sanic.response import json
async def handler(request):
return json(..., dumps=my_favorite_json.dumps)The developer is free to use an alternative. And, indeed, perhaps we can add some documentation on this. At this time I would be more in favor of a PR that fixes the documentation that would explain how to do this rather than universally making the change for the entire project (especially since there would be a breaking change as pointed out). |
|
Yeah, i agree with @ahopkins . Also looks like |
|
Can I take this one? :) |
|
@szepnapot Please do. (So I know we are on the same page, you are talking about the documentation part, right?) |
|
Ok. I think it would be more appropriate to change the default to rapidjson or worse to change it to json and make the user choose another library for performance reasons. ujson has been unmaintained for years, that'll only get worse, and neither rapidjson nor json have its issues. rapidjson is close in performance for strings and dicts type web app payloads. I think a documentation change on using a different JSON library isn't likely to have much effect though in that people also don't really change defaults and it doesn't help the library's goal of being fast out of the box. orjson can distribute macOS, Windows, and source distributions, but it hasn't been done. |
|
@ahopkins Yes :) |
|
@szepnapot Since you are working on it, maybe also mention how to pass a import my_favorite_json
from sanic.response import json
async def handler(request):
json_data = request.load_json(loads=my_favorite_json.loads)
...
return json(..., dumps=my_favorite_json.dumps) |
|
@yunstanford orjson now publishes Linux, macOS, and Windows wheels. Can we either go forward with this or split the documentation to another ticket so I can close? |
|
@ijl i've tried once, but looks like some unit tests are failing. but didn't dig deep. |
|
Any progress on this? |
|
hmm for small JSONs ujson faster. |
New release of the |
|
I'll reopen for anyone that wants to add discussion. Now that it is being supported again, I am less inclined to switch but only en to convincing arguments. Thanks for sharing @VMAtm , I'll take a read thru the release notes. |
|
Looks like new ultrajson now degradate feature set to orjson. |
|
What do you mean? |
for me, the most important feature was datetime conversion. Now ultrajson have the same feature set as orjson but slower. |
|
Personally on a couple of performance-critical applications in my organisation I have replaced ujson with orjson, specifically for the built-in iso-format on datetime objects (and orjson is super fast). |
|
@ashleysommer I see, for me, was the most important deserialization what not supported by orjson. |
|
Yeah. That was one of the biggest reasons I was hesitant to make the switch because of the changes that would cause for Sanic users. If it is a problem we will have to endure either way, I am back to the beginning on this one. Anyone willing to put together another round of performance testing? |
|
I been in the json lib hellhole for a while now, and im a vivid Sanic user and what I would love to get access to instead of choosing one in particular is to do something like this: https://github.com/mattgiles/mujson Have a "proxy" so you can load the compatible ones and it deside which to use based on dev prefs and so on. This would make Sanic a bit more "configurable" to the specific issue you are having :) I need to speed up my json handling since I am running our entire backend microservice installation on Sanic with quite a bit of throughput, being able to use whats strong for the usecase is a strong feat and gives a very flexible solution to the json serialization and deserialization for Sanic. (In my eyes there are no unicorn in the json lib family) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

I propose replacing the dependency on ujson with orjson.
sanic using orjson does 2.9x the requests per second on an example benchmark compared to when using ujson.
It also does not have the correctness issues ujson has. Its README has details: https://github.com/ijl/orjson.
I think the implementation details of
json()leak by handling types differently (e.g., ujson serializing datetimes to epoch timestamps, supporting subclasses) and exposingkwargs, so switching would be a breaking change.orjson:
ujson:
The output is a 56KiB JSON document of a GitHub activity feed. This was measured using:
The minimal patch to benchmark:
With ujson fully replaced, the test suite is fine. I'll open a pull request if it's ok to go ahead.