jsonbp


Namejsonbp JSON
Version 1.1.1 PyPI version JSON
download
home_pageNone
SummarySchema based JSON Parser/Serializer
upload_time2024-08-18 03:46:54
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT
keywords json serialization deserialization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# jsonbp

[![Documentation Status](https://readthedocs.org/projects/jsonbp/badge/?version=latest)](http://jsonbp.readthedocs.io/?badge=latest)
[![codecov](https://codecov.io/github/vottini/jsonbp/graph/badge.svg?token=N2H2WJ0SC5)](https://codecov.io/github/vottini/jsonbp)

**jsonbp** (JSON BluePrint) is a library for serializing and deserializing JSON
to and from Python based on schemas. While [json-schema][json_schema] and its
implementations offer a more mature and widely used technique, a different
approach was desired, which led to the development of this library.

jsonbp's design main goals were:
- schema reuse through import / type system
- custom user definable primitive types
- built in numeric fixed precision type which deserializes into Python's Decimal
- built in datetime type which deserializes into Python's datetime
- error reporting with support for localization
- support for enums
- easy to integrate and use

## Brief Introduction
 - [Schema definition](#schema-definition)
 - [Usage](#usage)
 - [Requirements and Dependencies](#requirements-and-dependencies)
 - [Installation](#installation)
 - [Documentation](#documentation)

## Schema definition

jsonbp uses its own (very simple) domain language to define a schema.
Here's a simple example:

```
root {
  x: float (min=0.0),
  y: float (min=0.0, max=1.0),

  optional color: {
    RED,
    GOLD,
    GREEN
  }
} [minLength=2]
```

This defines a schema that represents an array of minimum length 2 whose
elements contain the fields 'x' and 'y', where both 'x' and 'y' are not
allowed to be negative and further 'y' is not allowed to be greater than 1.0.
An optional field 'color' can also exist, and if present it needs to be either
"RED", "GOLD" or "GREEN". A JSON instance that obeys this schema is thus:

```js
[
  {
    "x": 2.0,
    "y", 0.004,
    "color": "RED"
  },

  {
    "x": 3.0,
    "y", 0.009
  },

  {
    "x": 4.0,
    "y", 0.016,
    "color": "GREEN"
  }
]
```

jsonbp offers the following directives to organize a schema:
- **"root"** -> defines which type will correspond to some JSON
- **"type"** -> defines specialized (restricted) simple types
- **"object"** -> specifies the contents of compound types (Objects)
- **"enum"** -> defines a list of allowed values for a given field
- **"import"** -> reuses directives from existing blueprints

These structures can be employed to simplify and make the schema more
modular. In the above example, one could split the definitions and get
something more reusable, like the following:

```
type non_negative : float (min=0.0)
type normalized : non_negative (max=1.0)

node coordinates {
  x: non_negative,
  y: normalized
}

include "color.jbp"
node colored_coordinates extends coordinates {
  optional color: color
}

root colored_coordinates[minLength=2]
```

where the contents of file "color.jbp" would be:

```
enum color {
  RED,
  GOLD,
  GREEN
}
```

> **_NOTE:_**  For full documentation, see [Documentation](#documentation)

## Usage overview

### Schema parsing

- jsonbp.load\_file(blueprint\_path: str) => JsonBlueprint
- jsonbp.load\_string(blueprint\_string: str) => JsonBlueprint

### JSON deserialization

- JsonBlueprint.deserialize(contents: str) => (success: bool, outcome: object)
- JsonBlueprint.serialize(payload: object) => str

### Example

```py
import jsonbp

blueprint = jsonbp.load_string('''
root {
  success: {
    YES,
    NO
  }
}
''')

jsonInstance = '{ "success": "YES" }'
success, outcome = blueprint.deserialize(jsonInstance)
print(f'Success: {success}')
print(f'Outcome: {outcome}')
```

Output:

```
Success: true
Outcome: {"success":"YES"}
```

> **_NOTE:_**  For full documentation, see [Documentation](#documentation)

## Requirements and Dependencies

jsonbp requires Python 3.7+, that's it.
Under the hood, jsonbp uses [PLY][ply] for its schema parsing. PLY comes
included with jsonbp already, there's no need to download it separately.

## Installation

jsonbp is available at PyPI:  [https://pypi.org/project/jsonbp/](https://pypi.org/project/jsonbp/)

To install through pip:
```bash
pip install jsonbp
```

## Documentation

- [`Full documentation`](https://jsonbp.readthedocs.io/en/latest/)
- [`Sample of using jsonbp in Flask`](https://github.com/vottini/sample-jsonbp-flask)

[//]: References
   [json_schema]: <https://json-schema.org/>
   [ply]: <https://www.dabeaz.com/ply/>


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "jsonbp",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "JSON, serialization, deserialization",
    "author": null,
    "author_email": "Gustavo Venturini <gustavo.c.venturini@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/34/eb/f8ce378b3eb0d6366bb079a32c93e652d3bec51c4294cd76f79548c3183f/jsonbp-1.1.1.tar.gz",
    "platform": null,
    "description": "\n# jsonbp\n\n[![Documentation Status](https://readthedocs.org/projects/jsonbp/badge/?version=latest)](http://jsonbp.readthedocs.io/?badge=latest)\n[![codecov](https://codecov.io/github/vottini/jsonbp/graph/badge.svg?token=N2H2WJ0SC5)](https://codecov.io/github/vottini/jsonbp)\n\n**jsonbp** (JSON BluePrint) is a library for serializing and deserializing JSON\nto and from Python based on schemas. While [json-schema][json_schema] and its\nimplementations offer a more mature and widely used technique, a different\napproach was desired, which led to the development of this library.\n\njsonbp's design main goals were:\n- schema reuse through import / type system\n- custom user definable primitive types\n- built in numeric fixed precision type which deserializes into Python's Decimal\n- built in datetime type which deserializes into Python's datetime\n- error reporting with support for localization\n- support for enums\n- easy to integrate and use\n\n## Brief Introduction\n - [Schema definition](#schema-definition)\n - [Usage](#usage)\n - [Requirements and Dependencies](#requirements-and-dependencies)\n - [Installation](#installation)\n - [Documentation](#documentation)\n\n## Schema definition\n\njsonbp uses its own (very simple) domain language to define a schema.\nHere's a simple example:\n\n```\nroot {\n  x: float (min=0.0),\n  y: float (min=0.0, max=1.0),\n\n  optional color: {\n    RED,\n    GOLD,\n    GREEN\n  }\n} [minLength=2]\n```\n\nThis defines a schema that represents an array of minimum length 2 whose\nelements contain the fields 'x' and 'y', where both 'x' and 'y' are not\nallowed to be negative and further 'y' is not allowed to be greater than 1.0.\nAn optional field 'color' can also exist, and if present it needs to be either\n\"RED\", \"GOLD\" or \"GREEN\". A JSON instance that obeys this schema is thus:\n\n```js\n[\n  {\n    \"x\": 2.0,\n    \"y\", 0.004,\n    \"color\": \"RED\"\n  },\n\n  {\n    \"x\": 3.0,\n    \"y\", 0.009\n  },\n\n  {\n    \"x\": 4.0,\n    \"y\", 0.016,\n    \"color\": \"GREEN\"\n  }\n]\n```\n\njsonbp offers the following directives to organize a schema:\n- **\"root\"** -> defines which type will correspond to some JSON\n- **\"type\"** -> defines specialized (restricted) simple types\n- **\"object\"** -> specifies the contents of compound types (Objects)\n- **\"enum\"** -> defines a list of allowed values for a given field\n- **\"import\"** -> reuses directives from existing blueprints\n\nThese structures can be employed to simplify and make the schema more\nmodular. In the above example, one could split the definitions and get\nsomething more reusable, like the following:\n\n```\ntype non_negative : float (min=0.0)\ntype normalized : non_negative (max=1.0)\n\nnode coordinates {\n  x: non_negative,\n  y: normalized\n}\n\ninclude \"color.jbp\"\nnode colored_coordinates extends coordinates {\n  optional color: color\n}\n\nroot colored_coordinates[minLength=2]\n```\n\nwhere the contents of file \"color.jbp\" would be:\n\n```\nenum color {\n  RED,\n  GOLD,\n  GREEN\n}\n```\n\n> **_NOTE:_**  For full documentation, see [Documentation](#documentation)\n\n## Usage overview\n\n### Schema parsing\n\n- jsonbp.load\\_file(blueprint\\_path: str) => JsonBlueprint\n- jsonbp.load\\_string(blueprint\\_string: str) => JsonBlueprint\n\n### JSON deserialization\n\n- JsonBlueprint.deserialize(contents: str) => (success: bool, outcome: object)\n- JsonBlueprint.serialize(payload: object) => str\n\n### Example\n\n```py\nimport jsonbp\n\nblueprint = jsonbp.load_string('''\nroot {\n  success: {\n    YES,\n    NO\n  }\n}\n''')\n\njsonInstance = '{ \"success\": \"YES\" }'\nsuccess, outcome = blueprint.deserialize(jsonInstance)\nprint(f'Success: {success}')\nprint(f'Outcome: {outcome}')\n```\n\nOutput:\n\n```\nSuccess: true\nOutcome: {\"success\":\"YES\"}\n```\n\n> **_NOTE:_**  For full documentation, see [Documentation](#documentation)\n\n## Requirements and Dependencies\n\njsonbp requires Python 3.7+, that's it.\nUnder the hood, jsonbp uses [PLY][ply] for its schema parsing. PLY comes\nincluded with jsonbp already, there's no need to download it separately.\n\n## Installation\n\njsonbp is available at PyPI:  [https://pypi.org/project/jsonbp/](https://pypi.org/project/jsonbp/)\n\nTo install through pip:\n```bash\npip install jsonbp\n```\n\n## Documentation\n\n- [`Full documentation`](https://jsonbp.readthedocs.io/en/latest/)\n- [`Sample of using jsonbp in Flask`](https://github.com/vottini/sample-jsonbp-flask)\n\n[//]: References\n   [json_schema]: <https://json-schema.org/>\n   [ply]: <https://www.dabeaz.com/ply/>\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Schema based JSON Parser/Serializer",
    "version": "1.1.1",
    "project_urls": {
        "Documentation": "https://jsonbp.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/vottini/jsonbp"
    },
    "split_keywords": [
        "json",
        " serialization",
        " deserialization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c86aebeb0e7015f53770c31ec47fa4813f94f653956c5f880b9a06ee9e091504",
                "md5": "46d344b2171f58b472ed65490d22b41d",
                "sha256": "681a0bc46684f8a4decf52eb35af64e530a5a33d07b38ce43525ba12cc07dd67"
            },
            "downloads": -1,
            "filename": "jsonbp-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "46d344b2171f58b472ed65490d22b41d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 53932,
            "upload_time": "2024-08-18T03:46:52",
            "upload_time_iso_8601": "2024-08-18T03:46:52.344012Z",
            "url": "https://files.pythonhosted.org/packages/c8/6a/ebeb0e7015f53770c31ec47fa4813f94f653956c5f880b9a06ee9e091504/jsonbp-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "34ebf8ce378b3eb0d6366bb079a32c93e652d3bec51c4294cd76f79548c3183f",
                "md5": "cd1236b9436fdd5146cd01d15a2de975",
                "sha256": "21a62bd2db3a7b0219c1dc6651297856447c8d0bb18dd21a62d941f6a9e854e0"
            },
            "downloads": -1,
            "filename": "jsonbp-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "cd1236b9436fdd5146cd01d15a2de975",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 53690,
            "upload_time": "2024-08-18T03:46:54",
            "upload_time_iso_8601": "2024-08-18T03:46:54.203793Z",
            "url": "https://files.pythonhosted.org/packages/34/eb/f8ce378b3eb0d6366bb079a32c93e652d3bec51c4294cd76f79548c3183f/jsonbp-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-18 03:46:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vottini",
    "github_project": "jsonbp",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jsonbp"
}
        
Elapsed time: 7.63439s