GithubHelp home page GithubHelp logo

rsabet / hyperjson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mre/hyperjson

0.0 2.0 0.0 2.76 MB

A hyper-fast Python module for reading/writing JSON data using Rust's serde-json.

License: Apache License 2.0

Makefile 0.90% Python 74.56% Shell 2.17% Rust 22.37%

hyperjson's Introduction

hyperjson

Build Status

A hyper-fast, safe Python module to read and write JSON data. Works as a drop-in replacement for Python's built-in json module. It's a thin wrapper around Rust's serde-json built with pyo3. Compatible with Python 3. Should also work on Python 2, but it's not officially supported. This is alpha software and there will be bugs, so maybe don't deploy to production just yet. ๐Ÿ˜‰

Usage

hyperjson is meant as a drop-in replacement for Python's json module:

>>> import hyperjson 
>>> hyperjson.dumps([{"key": "value"}, 81, True])
'[{"key":"value"},81,true]'
>>> hyperjson.loads("""[{"key": "value"}, 81, true]""")
[{u'key': u'value'}, 81, True

Motivation

Parsing JSON is a solved problem. So, no need to reinvent the wheel, right?
Unless you care about performance and safety.

Turns out, parsing JSON correctly is hard, but due to Rust, the risk of running into stack overflows or segmentation faults is lower (basically zero, especially in comparison to C implementations).

For a more in-detail discussion, watch the talk about this project recorded at the Rust Cologne Meetup in August 2018.

Goals

  • Compatibility: Support the full feature-set of Python's json module.
  • Safety: No segfaults, panics, or overflows.
  • Performance: Significantly faster than json and as fast as ujson (both written in C).

Non-goals

  • Support ujson and simplejson extensions:
    Custom extensions like encode(), __json__(), or toDict() are not supported. The reason is, that they go against PEP8 (e.g. dunder functions are restricted to the standard library, camelCase is not pythonic) and are not available in Python's json module.
  • Whitespace preservation: Whitespace in JSON strings is not preserved. Mainly because JSON is a whitespace-agnostic format and serde-json stips them out by design. In practice this should not be a problem, since your application shouldn't depend on whitespace padding, but it's something to be aware of.

Benchmark

We are not fast yet. That said, we haven't made any optimizations or even done any cleanup yet.
Another reason why hyperjson can be fast in the long-term is by exploiting features of newer CPUs like multi-core and SIMD. That's one area other (C-based) JSON extensions haven't touched yet because it might make code harder to debug and prone to race-conditions. In Rust, this is feasible due to crates like faster and rayon.

So there's a chance that these values might improve soon.
If you want to help, check the instructions in the Development Environment section below.

Test machine:
MacBook Pro 15 inch, Mid 2015 (2,2 GHz Intel Core i7, 16 GB RAM) Darwin 17.6.18

Serialization benchmarks Deserialization benchmarks

Installation

To compile the code and create an importable Python module from it, call

make install

From there, you can simply use it from Python as seen in the usage example above.

Contributions welcome!

If you like to hack on hyperjson, here is what needs to be done:

Just pick one of the open tickets. We will provide mentorship for all of them. ๐Ÿ˜ƒ

Developer guide

This project uses pipenv for managing the development environment. If you don't have it installed, run

pip install pipenv

The project requires the nightly version of Rust.

Install it via rustup:

rustup override set nightly

If you have already installed the nightly version, make sure it is updated:

rustup update nightly

After that, you can compile the current version of hyperjson, execute all tests and benchmarks with the following commands:

make install
make test
make bench

Now just modify the source code and run the above commands again to test your changes. Happy hacking!

๐Ÿคซ Pssst!...check out the Makefile for more commands.

Troubleshooting

In case you want to test your changes within a virtual environment, run

pipenv shell

When you are done testing, exit the virtual environment:

exit

Creating pretty diagrams

In order to create the benchmark histogram, you first need a few additional prerequisites:

On macOS, please also add the following to your ~/.matplotlib/matplotlibrc (reference):

backend: TkAgg

After that, run the following

make plot

License

hyperjson is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in hyperjson by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

hyperjson's People

Contributors

cjgu avatar dtolnay avatar konstin avatar mre avatar rsabet avatar wdv4758h avatar zapanton avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.