JSON Schema Toolkit¶
Programmatic building of JSON schemas (recursive field mappings) with validation, a Django JSON Field, and native PostgreSQL JSON type constraints.
Overview¶
Built on top of json_document and json_schema_validator, with powerful support for building and validating JSON documents. Can be used to programmatically build JSON schemas by mapping fields to a document or recursively to other fields, as well as used for validating a Django JSON field during save operations. If PostgreSQL >= 9.2 is used, supports the native JSON data type. If PostgreSQL >= 9.3 is used, supports custom JSON SQL constraint generation for the Django Model.
Extends json_document to provide nullable fields (in additional to optional fields), deletion of members (through del), a Pythonic API for dot notation member access, as well as convenience input transformations for data such as dates and times, time deltas, and others.
- JSON Schema: < http://json-schema.org/ >
- Django: < http://www.djangoproject.com/ >
- PostgreSQL JSON Functions and Operators < http://www.postgresql.org/docs/9.3/static/functions-json.html >
Prerequisites¶
Core:
- Python >= 2.6
- json_document >= 0.8
- json_schema_validator >= 2.3
Optionally, for the Django field:
- Django >= 1.4
Optionally, for PostgreSQL native JSON data type:
- PostgreSQL >= 9.2
- psycopg2 >= 2.4
Optionally, for JSON SQL constraints:
- PostgreSQL >= 9.3
Optionally, for testing:
- unittest2 >= 0.5.1
Obtaining¶
- Author’s website for the project: http://www.petrounias.org/software/json-schema-toolkit/
- Git repository on GitHub: https://github.com/petrounias/json-schema-toolkit/
- Mercurial repository on BitBucket: http://www.bitbucket.org/petrounias/json-schema-toolkit/
Installation¶
Ensure the required packages json_document and json_schema_validator are installed, and then install json_schema_toolkit:
Via setup tools:
python setup.py install
Via pip and pypi:
pip install json-schema-toolkit
Table of Contents¶
Basic Usage¶
All imports are from:
from json_schema_toolkit.document import ...
JSON Document¶
Extends json_document Document (and hence DocumentFragment) in order to build the schema automatically through introspection. The schema is generated by each field present in the Document. Fields of type ‘object’ and ‘array’ recursively generate sub-schemas.
The schema parameters (such as ‘title’) for the Document are provided in a ‘Meta’ class; the base Document always describes a JSON type ‘object’. If no fields are specified, the resulting Document’s type is ‘any’.
Example empty JSONDocument:
class EmptyDocument(JSONDocument):
class Meta(object):
title = u'a title'
description = u'a description'
d1 = EmptyDocument({})
JSON Document Field¶
Extensible fields of various kinds (including non-native JSON types which can be converted to a standard string-based representation such as date and time). Fields can be added to a JSONDocument as well as the content of other fields which expect them (JSONObjectField and JSONListField). The constructor arguments for fields are used to parametrize the schema they each generate.
Example of a simple ‘integer’ field:
class SimpleDocument(JSONDocument):
answer = JSONIntegerField(title = u'the answer',
description = u'answer mutually exclusive with question in each given universe')
class Meta(object):
title = u'oracle'
description = u'a repository of truth'
d1 = SimpleDocument({ 'answer' : 42 })
Example of an ‘array’ type wich contains ‘string’ elements:
class ListDocument(JSONDocument):
events = JSONListField(title = u'events',
description = u'important historical events', content = [
JSONStringField(title = u'event',
description = u'important historical event'),
])
class Meta(object):
title = u'history'
description = u'a collection of historical events'
d1 = ListDocument({ 'events' : [
u'Sinking of Atlantis',
u'Discovery of Atlantis',
u'Colonization of Atlantis',
] })
Example of an ‘object’ type which contains a combination of other fields:
class ObjectDocument(JSONDocument):
events = JSONListField(title = u'events',
description = u'important historical events', content = [
JSONObjectField(title = u'event',
description = u'important historical event', content = {
'title' : JSONStringField(title = u'event title'),
'importance' : JSONIntegerField(title = u'event importance'),
}),
])
class Meta(object):
title = u'history'
description = u'a collection of historical events'
d1 = ObjectDocument({ 'events' : [
{ 'title' : u'Sinking of Atlantis', 'importance' : 3 },
{ 'title' : u'Discovery of Atlantis', 'importance' : 7 },
{ 'title' : u'Colonization of Atlantis', 'importance' : 12 },
] })
Fragment Proxy¶
Provides a Pythonic API to composite fields, so members can be accessed through the dot notation. The relevant fragment can be obtained through the ‘_fragment’ property.
Django JSON Field (planned)¶
Planned
Django JSON field with validation and optional support for native PostgreSQL 9.2 JSON data type, and PostgreSQL 9.3 constraints.
PostgreSQL native JSON type (planned)¶
Planned
Support for PostgreSQL 9.3 JSON function for generating SQL constraints within the Django ORM.
http://www.postgresql.org/docs/9.3/static/functions-json.html
Examples¶
All imports are from:
from json_schema_toolkit.document import ...
Empty Document¶
A document which is a simple object, with the ‘any’ JSON type:
class EmptyDocument(JSONDocument):
class Meta(object):
title = u'a title'
description = u'a description'
d1 = EmptyDocument({})
Simple Document¶
A document which features a single field ‘answer’ of type ‘integer’:
class SimpleDocument(JSONDocument):
answer = JSONIntegerField(title = u'the answer',
description = u'answer mutually exclusive with question in each given universe')
class Meta(object):
title = u'oracle'
description = u'a repository of truth'
d1 = SimpleDocument({ 'answer' : 42 })
List Document¶
A document which contains a list ‘events’ of type ‘string’:
class ListDocument(JSONDocument):
events = JSONListField(title = u'events',
description = u'important historical events', content = [
JSONStringField(title = u'event',
description = u'important historical event'),
])
class Meta(object):
title = u'history'
description = u'a collection of historical events'
d1 = ListDocument({ 'events' : [
u'Sinking of Atlantis',
u'Discovery of Atlantis',
u'Colonization of Atlantis',
] })
Composite Document¶
A document which contains a list of objects, comprising a ‘string’ and an ‘integer’:
class ObjectDocument(JSONDocument):
events = JSONListField(title = u'events',
description = u'important historical events', content = [
JSONObjectField(title = u'event',
description = u'important historical event', content = {
'title' : JSONStringField(title = u'event title'),
'importance' : JSONIntegerField(title = u'event importance'),
}),
])
class Meta(object):
title = u'history'
description = u'a collection of historical events'
d1 = ObjectDocument({ 'events' : [
{ 'title' : u'Sinking of Atlantis', 'importance' : 3 },
{ 'title' : u'Discovery of Atlantis', 'importance' : 7 },
{ 'title' : u'Colonization of Atlantis', 'importance' : 12 },
] })
Module json_schema_toolkit.document¶
FragmentProxy¶
- class json_schema_toolkit.document.FragmentProxy(fragment)¶
JSONDocument¶
- class json_schema_toolkit.document.JSONDocument(value)¶
JSONDocumentFragment¶
- class json_schema_toolkit.document.JSONDocumentFragment(document, parent, value, item=None, schema=None)¶
JSONDocumentField¶
- class json_schema_toolkit.document.JSONDocumentField(title=None, description=None, default=None, optional=False, null=False, pattern=None, content=None, implementation=None)¶
Technical and Implementation Notes¶
Deleting Members¶
Deleting a value is implemented through setting the value of a DocumentFragment. This is done so the Document can keep track of fragments in its cache, as well as bump its revision.
Schema dictionary¶
The schema generated by a JSONDocumentField records all arguments passed to the constructor of the field, as well as a reference to itself through ‘__field’, in a similar way that the fragment implementation class is recorded in ‘__fragment_cls’.
Release Notes¶
- v1.0.0 alpha @ 16 June 2013 Initial public release.
Development Status¶
Actively developed and maintained. Currently used in production in proprietary projects by the author and his team.
Future Work¶
- Django Field and custom PostgreSQL JSON data type support.
Contributors¶
Written and maintained by Alexis Petrounias < http://www.petrounias.org/ >
Based on work and feedback by Zygmunt Krynicki < http://www.suxx.pl/ >
License¶
Released under the OSI-approved BSD license. Please note that json_document and json_schema_validator are LGPLv3 and not copyleft-free, so this may affect your ability to include this software’s requirements in proprietary software. This software only links against the aforementioned libraries in accordance with their license.
Copyright (c) 2013 Alexis Petrounias < www.petrounias.org >, all rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.