crosshash


Namecrosshash JSON
Version 0.3.4 PyPI version JSON
download
home_pagehttps://github.com/httpie/crosshash
SummaryStable, cross-platform JSON serialization and hashing for Python and JavaScript.
upload_time2023-09-03 08:24:58
maintainer
docs_urlNone
authorJakub Roztocil
requires_python>=3.11,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `crosshash`

[![Build status](https://github.com/httpie/crosshash/workflows/test/badge.svg)](https://github.com/httpie/crosshash/actions)

Stable, cross-platform JSON serialization and hashing for Python and JavaScript.

## Motivation

To make it possible to compare and hash JSON objects in a stable way across platforms.

## Installation

### Python

[![PyPi](https://badge.fury.io/py/crosshash.svg)](https://pypi.python.org/pypi/crosshash)

```bash
pip install crosshash
```

### JavaScript

[![NPM](https://badge.fury.io/js/crosshash.svg)](https://www.npmjs.com/package/crosshash)

```bash
npm install crosshash
```

## Features


### API
The following functions are implemented in both Python and JavaScript and the output is guaranteed to be the same:

#### `crossjson(obj) → str`

- Sort keys alphabetically
- Ensure no unsafe numbers are present 
- Serialize using the same format as `JSON.stringify()` (lowest common denominator)

#### `crosshash(obj) → str`

- Serialize the object with `crossjson()`
- Hash the resulting string with MD5


### CLI

Both Python and JavaScript implementations come with a CLI that can be used to generate stable JSON and hashes.

```bash
JSON='{"B":2,"C":[1,2,3],"A":1}'
[ $(crosshash-js --hash "$JSON") == $(crosshash-py --hash "$JSON") ] && echo 'It’s a match!'
[ $(crosshash-js --json "$JSON") == $(crosshash-py --json "$JSON") ] && echo 'It’s a match!'
```


## Usage

### Python

#### API

```python
from crosshash import crossjson, crosshash, CrossHashError, MAX_SAFE_INTEGER

obj = {'B': 2, 'C': [1, 2, 3], 'A': 1}

# Generate stable JSON:
assert crossjson(obj) == '{"A":1,"B":2,"C":[1,2,3]}'  

# Generate stable hash:
assert crosshash(obj) == '12982c60a9a8829ea4eeb2e1e7e1e04e'

# Throws `CrossHashError`:
crosshash({'A': MAX_SAFE_INTEGER + 1})  
```

#### CLI

You can invoke `crosshash.py` directly or use `python -m crosshash`. The package also installs an executable called `crosshash-py`.

```bash
$ crosshash-py --json '{"B": 2, "C": [1, 2, 3], "A": 1}'
{"A":1,"B":2,"C":[1,2,3]}
```

```bash
$ crosshash-py --hash '{"B": 2, "C": [1, 2, 3], "A": 1}'
12982c60a9a8829ea4eeb2e1e7e1e04e
```


### JavaScript

#### API

The library runs in the browser and Node.js and comes with [TypeScript definitions](./crosshash.d.ts).

```javascript
const {crossjson, crosshash, CrossHashError} = require('crosshash')

const obj = {B: 2, C: [1, 2, 3], A: 1}

// Generate stable JSON:
assert(crossjson(obj) === '{"A":1,"B":2,"C":[1,2,3]}')

// Generate stable hash:
assert(crosshash(obj) === '12982c60a9a8829ea4eeb2e1e7e1e04e')

// Throws `CrossHashError`:
crosshash({A: Number.MAX_SAFE_INTEGER + 1}) 
```

#### CLI

You can invoke `crosshash.js` directly or using `npx`. The package also installs an executable called `crosshash-js`.

```bash
$ crosshash-js --json '{"B": 2, "C": [1, 2, 3], "A": 1}'
{"A":1,"B":2,"C":[1,2,3]}
```

```bash
$ crosshash-js --hash '{"B": 2, "C": [1, 2, 3], "A": 1}'
12982c60a9a8829ea4eeb2e1e7e1e04e
```

## Test suite

To ensure consistency, the [test suite](./tests) invokes the Python and JavaScript implementations of `crossjson()` and `crosshash()` on the [same data](./tests/cases.py) and compares the results.


## Development

It should be fairly straightforward to add support for other languages.

```bash
git clone git@github.com:httpie/crosshash.git
cd ./crosshash
make install
make test
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/httpie/crosshash",
    "name": "crosshash",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.11,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jakub Roztocil",
    "author_email": "jakub@httpie.io",
    "download_url": "https://files.pythonhosted.org/packages/97/d0/992ced227cb8ca892905b6be7101dcaf7cff82e9f1c6084c69dec2415fad/crosshash-0.3.4.tar.gz",
    "platform": null,
    "description": "# `crosshash`\n\n[![Build status](https://github.com/httpie/crosshash/workflows/test/badge.svg)](https://github.com/httpie/crosshash/actions)\n\nStable, cross-platform JSON serialization and hashing for Python and JavaScript.\n\n## Motivation\n\nTo make it possible to compare and hash JSON objects in a stable way across platforms.\n\n## Installation\n\n### Python\n\n[![PyPi](https://badge.fury.io/py/crosshash.svg)](https://pypi.python.org/pypi/crosshash)\n\n```bash\npip install crosshash\n```\n\n### JavaScript\n\n[![NPM](https://badge.fury.io/js/crosshash.svg)](https://www.npmjs.com/package/crosshash)\n\n```bash\nnpm install crosshash\n```\n\n## Features\n\n\n### API\nThe following functions are implemented in both Python and JavaScript and the output is guaranteed to be the same:\n\n#### `crossjson(obj) \u2192 str`\n\n- Sort keys alphabetically\n- Ensure no unsafe numbers are present \n- Serialize using the same format as `JSON.stringify()` (lowest common denominator)\n\n#### `crosshash(obj) \u2192 str`\n\n- Serialize the object with `crossjson()`\n- Hash the resulting string with MD5\n\n\n### CLI\n\nBoth Python and JavaScript implementations come with a CLI that can be used to generate stable JSON and hashes.\n\n```bash\nJSON='{\"B\":2,\"C\":[1,2,3],\"A\":1}'\n[ $(crosshash-js --hash \"$JSON\") == $(crosshash-py --hash \"$JSON\") ] && echo 'It\u2019s a match!'\n[ $(crosshash-js --json \"$JSON\") == $(crosshash-py --json \"$JSON\") ] && echo 'It\u2019s a match!'\n```\n\n\n## Usage\n\n### Python\n\n#### API\n\n```python\nfrom crosshash import crossjson, crosshash, CrossHashError, MAX_SAFE_INTEGER\n\nobj = {'B': 2, 'C': [1, 2, 3], 'A': 1}\n\n# Generate stable JSON:\nassert crossjson(obj) == '{\"A\":1,\"B\":2,\"C\":[1,2,3]}'  \n\n# Generate stable hash:\nassert crosshash(obj) == '12982c60a9a8829ea4eeb2e1e7e1e04e'\n\n# Throws `CrossHashError`:\ncrosshash({'A': MAX_SAFE_INTEGER + 1})  \n```\n\n#### CLI\n\nYou can invoke `crosshash.py` directly or use `python -m crosshash`. The package also installs an executable called `crosshash-py`.\n\n```bash\n$ crosshash-py --json '{\"B\": 2, \"C\": [1, 2, 3], \"A\": 1}'\n{\"A\":1,\"B\":2,\"C\":[1,2,3]}\n```\n\n```bash\n$ crosshash-py --hash '{\"B\": 2, \"C\": [1, 2, 3], \"A\": 1}'\n12982c60a9a8829ea4eeb2e1e7e1e04e\n```\n\n\n### JavaScript\n\n#### API\n\nThe library runs in the browser and Node.js and comes with [TypeScript definitions](./crosshash.d.ts).\n\n```javascript\nconst {crossjson, crosshash, CrossHashError} = require('crosshash')\n\nconst obj = {B: 2, C: [1, 2, 3], A: 1}\n\n// Generate stable JSON:\nassert(crossjson(obj) === '{\"A\":1,\"B\":2,\"C\":[1,2,3]}')\n\n// Generate stable hash:\nassert(crosshash(obj) === '12982c60a9a8829ea4eeb2e1e7e1e04e')\n\n// Throws `CrossHashError`:\ncrosshash({A: Number.MAX_SAFE_INTEGER + 1}) \n```\n\n#### CLI\n\nYou can invoke `crosshash.js` directly or using `npx`. The package also installs an executable called `crosshash-js`.\n\n```bash\n$ crosshash-js --json '{\"B\": 2, \"C\": [1, 2, 3], \"A\": 1}'\n{\"A\":1,\"B\":2,\"C\":[1,2,3]}\n```\n\n```bash\n$ crosshash-js --hash '{\"B\": 2, \"C\": [1, 2, 3], \"A\": 1}'\n12982c60a9a8829ea4eeb2e1e7e1e04e\n```\n\n## Test suite\n\nTo ensure consistency, the [test suite](./tests) invokes the Python and JavaScript implementations of `crossjson()` and `crosshash()` on the [same data](./tests/cases.py) and compares the results.\n\n\n## Development\n\nIt should be fairly straightforward to add support for other languages.\n\n```bash\ngit clone git@github.com:httpie/crosshash.git\ncd ./crosshash\nmake install\nmake test\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Stable, cross-platform JSON serialization and hashing for Python and JavaScript.",
    "version": "0.3.4",
    "project_urls": {
        "Homepage": "https://github.com/httpie/crosshash",
        "Repository": "https://github.com/httpie/crosshash"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5634243eb4a661dd098ddd6724ec4752a87768d04f708b81118a15d13492ec37",
                "md5": "af9b3726e9a99df7caa2253ae11e10c7",
                "sha256": "1ad5249f9d2be01c658f66479319791310789376e77ddcf72a4d7d8ee14aa6dc"
            },
            "downloads": -1,
            "filename": "crosshash-0.3.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "af9b3726e9a99df7caa2253ae11e10c7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11,<4.0",
            "size": 6726,
            "upload_time": "2023-09-03T08:24:56",
            "upload_time_iso_8601": "2023-09-03T08:24:56.146283Z",
            "url": "https://files.pythonhosted.org/packages/56/34/243eb4a661dd098ddd6724ec4752a87768d04f708b81118a15d13492ec37/crosshash-0.3.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "97d0992ced227cb8ca892905b6be7101dcaf7cff82e9f1c6084c69dec2415fad",
                "md5": "6e20b2741be4f4fcd363f688a654b92a",
                "sha256": "cc2c76ee45af51d77852c9612182afa9c9b8739f656afc9a9f5e6da3978665a4"
            },
            "downloads": -1,
            "filename": "crosshash-0.3.4.tar.gz",
            "has_sig": false,
            "md5_digest": "6e20b2741be4f4fcd363f688a654b92a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11,<4.0",
            "size": 3804,
            "upload_time": "2023-09-03T08:24:58",
            "upload_time_iso_8601": "2023-09-03T08:24:58.817161Z",
            "url": "https://files.pythonhosted.org/packages/97/d0/992ced227cb8ca892905b6be7101dcaf7cff82e9f1c6084c69dec2415fad/crosshash-0.3.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-03 08:24:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "httpie",
    "github_project": "crosshash",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "crosshash"
}
        
Elapsed time: 0.42994s