calamus: JSON-LD Serialization Library for Python

Documentation Status,%20Integration%20Tests%20and%20Deploy/badge.svg

calamus is a library built on top of marshmallow to allow (de-)Serialization of Python classes to JSON-LD


calamus releases and development versions are available from PyPI. You can install it using any tool that knows how to handle PyPI packages.

With pip:

$ pip install calamus


Assuming you have a class like

class Book:
    def __init__(self, _id, name):
        self._id = _id = name

Declare schemes

You can declare a schema for serialization like

schema = fields.Namespace("")

class BookSchema(JsonLDSchema):
    _id = fields.Id()
    name = fields.String(

    class Meta:
        rdf_type = schema.Book
        model = Book

The fields.Namespace class represents an ontology namespace.

Make sure to set rdf_type to the RDF triple type you want get and model to the python class this schema applies to.

Serializing objects (“Dumping”)

You can now easily serialize python classes to JSON-LD

book = Book(_id="", name="Ilias")
jsonld_dict = BookSchema().dump(book)
#    "@id": "",
#    "@type": "",
#    "": "Ilias",

jsonld_string = BookSchema().dumps(book)
#'{"@id": "", "": "Ilias", "@type": ""}')

Deserializing objects (“Loading”)

You can also easily deserialize JSON-LD to python objects

data = {
    "@id": "",
    "@type": "",
    "": "Ilias",
book = BookSchema().load(data)
#<Book(_id="", name="Ilias")>

Indices and tables