Stable JSON serialization and hashing for Python and JavaScript.
Project description
crosshash
Stable, cross-platform JSON serialization and hashing for Python and JavaScript.
Why?
To make it possible to compare and hash JSON objects in a stable way across platforms.
How?
- Sort keys alphabetically
- Ensure no unsafe numbers are present
- Use the same format as
JSON.stringify()
(lowest common denominator) - Hash the resulting string with MD5
Installation
Python
pip install crosshash
JavaScript
npm install crosshash
Usage
Python
from crosshash import crossjson, crosshash, CrossHashError, MAX_SAFE_INTEGER
data = {
'B': 2,
'C': [1, 2, 3],
'A': 1,
}
assert crossjson(data) == '{"A":1,"B":2,"C":[1,2,3]}' # stable JSON
assert crosshash(data) == '12982c60a9a8829ea4eeb2e1e7e1e04e' # stable hash
crosshash({'A': MAX_SAFE_INTEGER + 1}) # throws CrossHashError
JavaScript
const {crossjson, crosshash, CrossHashError} = require('crosshash')
const obj = {
B: 2,
C: [1, 2, 3],
A: 1,
}
assert(crossjson(obj) === '{"A":1,"B":2,"C":[1,2,3]}') // stable JSON
assert(crosshash(obj) === '12982c60a9a8829ea4eeb2e1e7e1e04e') // stable hash
crosshash({a: Number.MAX_SAFE_INTEGER + 1}) // throws CrossHashError
Test suite
To ensure consistency, the test suite invokes the Python and JavaScript implementations of crossjson()
and crosshash()
on the same data and compares the results.
Development
git clone git@github.com:httpie/crosshash.git
cd crosshash
make install
make test
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
crosshash-0.1.0.tar.gz
(3.1 kB
view hashes)
Built Distribution
Close
Hashes for crosshash-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a5179fc305cf8b9df9d4bf084b6789dac809e37d8e649301dc3537fb99e42de |
|
MD5 | f81fbc8857a4406cd521d0c08632db2e |
|
BLAKE2b-256 | 25886b72015d5efd971eba8ad19e2e572df6a8f74c6f711fe5799e8bad8aa32a |