Schema based JSON Parser/Serializer
Project description
jsonbp
jsonbp (JSON BluePrint) is a library for serializing and deserializing JSON to and from Python based on schemas. While 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
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:
[
{
"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 NonNegative : Float (min=0.0)
type Normalized : NonNegative (max=1.0)
object Coordinates {
x: NonNegative,
y: Normalized
}
include "color.jbp"
object ColoredCoordinates extends Coordinates {
optional color: Color
}
root ColoredCoordinates[minLength=2]
where the contents of file "color.jbp" would be:
enum Color {
RED,
GOLD,
GREEN
}
NOTE: For full documentation, see 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
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
Requirements and Dependencies
jsonbp requires Python 3.11+, that's it. Under the hood, jsonbp uses 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/
To install through pip:
pip install jsonbp
Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonbp-1.2.3.tar.gz.
File metadata
- Download URL: jsonbp-1.2.3.tar.gz
- Upload date:
- Size: 54.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e19b1bca6e1b0eadc6233b0ccc35d701457416702e089ef45ab058c965adba73
|
|
| MD5 |
820ca0d4958b1c891a9eaf5721795921
|
|
| BLAKE2b-256 |
93648ae7e1f361468ef5585420645de5ce8a2a3f411f7afc5d4ebd82828521a4
|
Provenance
The following attestation bundles were made for jsonbp-1.2.3.tar.gz:
Publisher:
pypi-publish.yml on vottini/jsonbp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonbp-1.2.3.tar.gz -
Subject digest:
e19b1bca6e1b0eadc6233b0ccc35d701457416702e089ef45ab058c965adba73 - Sigstore transparency entry: 1065505084
- Sigstore integration time:
-
Permalink:
vottini/jsonbp@62dcd2f2f3bddfd6ffdb5e97b0e8f6d6a0675dc7 -
Branch / Tag:
refs/tags/1.2.3 - Owner: https://github.com/vottini
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@62dcd2f2f3bddfd6ffdb5e97b0e8f6d6a0675dc7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file jsonbp-1.2.3-py3-none-any.whl.
File metadata
- Download URL: jsonbp-1.2.3-py3-none-any.whl
- Upload date:
- Size: 55.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5fe118f010bd8db6be26ea6d00a5cd445decfa601c0992234a1117233bf4861
|
|
| MD5 |
67d2ede02ac227377a59ba7775146433
|
|
| BLAKE2b-256 |
50d6f1822b7ba90346d937636aed8f83ae69d037772ab834cceb6face475a1a3
|
Provenance
The following attestation bundles were made for jsonbp-1.2.3-py3-none-any.whl:
Publisher:
pypi-publish.yml on vottini/jsonbp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonbp-1.2.3-py3-none-any.whl -
Subject digest:
a5fe118f010bd8db6be26ea6d00a5cd445decfa601c0992234a1117233bf4861 - Sigstore transparency entry: 1065505100
- Sigstore integration time:
-
Permalink:
vottini/jsonbp@62dcd2f2f3bddfd6ffdb5e97b0e8f6d6a0675dc7 -
Branch / Tag:
refs/tags/1.2.3 - Owner: https://github.com/vottini
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@62dcd2f2f3bddfd6ffdb5e97b0e8f6d6a0675dc7 -
Trigger Event:
push
-
Statement type: