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.2.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.2-py3-none-any.whl (55.0 kB view details)

Uploaded Python 3

File details

Details for the file jsonbp-1.2.2.tar.gz.

File metadata

  • Download URL: jsonbp-1.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 cb68fd4583e8c61b267bbf0e5beee27bb33721ad688ce5215487256cb4dcf205
MD5 95f142c5641cd213ee195f6d8f10a9d8
BLAKE2b-256 d3c5d48c78b5125c55e83e51c365e342e9d2af5a03840f418f206181a2e8517d

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonbp-1.2.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: jsonbp-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 55.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b79b5c4abdfab2f78f615953d817f9378ca277114a4d2d1923467eb13501975a
MD5 d93bd629f1f51001dcfeca8e094b030b
BLAKE2b-256 d5d8865bb17d30bd9615dde87c8d571c16e713a0b241fc751b70ffe89d64f675

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonbp-1.2.2-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