Skip to main content

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, I wanted a different approach, which led me 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 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

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.7+, 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.1.0.tar.gz (53.5 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.1.0-py3-none-any.whl (53.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jsonbp-1.1.0.tar.gz
  • Upload date:
  • Size: 53.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for jsonbp-1.1.0.tar.gz
Algorithm Hash digest
SHA256 601a00e28da33b2c9912528787ddc962a9f584caabaed3842a483a85be1fffb8
MD5 613d8f0b34cfbd1e989b7322cb7a7e7d
BLAKE2b-256 4196468f31a699217aeb9b754735c0d6d9797f0cf734aecbfdbc77534d0d3552

See more details on using hashes here.

File details

Details for the file jsonbp-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: jsonbp-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 53.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for jsonbp-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ab5bc9a71311e9e419a02d18b7312832939f7cbf20e8886aad979b938365f74
MD5 481c6c4896687d46049e3594dd65d1c5
BLAKE2b-256 6a2364ae99bb2f0752396d9aa132e18e86b875f1e94016fde36c768fa974812c

See more details on using hashes here.

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