Skip to main content

Schema based JSON Parser/Serializer

Project description

jsonbp

Documentation Status codecov

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonbp-1.2.3.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonbp-1.2.3-py3-none-any.whl (55.1 kB view details)

Uploaded Python 3

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

Hashes for jsonbp-1.2.3.tar.gz
Algorithm Hash digest
SHA256 e19b1bca6e1b0eadc6233b0ccc35d701457416702e089ef45ab058c965adba73
MD5 820ca0d4958b1c891a9eaf5721795921
BLAKE2b-256 93648ae7e1f361468ef5585420645de5ce8a2a3f411f7afc5d4ebd82828521a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonbp-1.2.3.tar.gz:

Publisher: pypi-publish.yml on vottini/jsonbp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for jsonbp-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a5fe118f010bd8db6be26ea6d00a5cd445decfa601c0992234a1117233bf4861
MD5 67d2ede02ac227377a59ba7775146433
BLAKE2b-256 50d6f1822b7ba90346d937636aed8f83ae69d037772ab834cceb6face475a1a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonbp-1.2.3-py3-none-any.whl:

Publisher: pypi-publish.yml on vottini/jsonbp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page