calamus is a library built on top of marshmallow to allow (de-)Serialization of Python classes to JSON-LD.
Project description
calamus: JSON-LD Serialization Library for Python
calamus is a library built on top of marshmallow to allow (de-)Serialization of Python classes to JSON-LD
Installation
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
Usage
Assuming you have a class like
class Book: def __init__(self, _id, name): self._id = _id self.name = name
Declare schemes
You can declare a schema for serialization like
schema = fields.Namespace("http://schema.org/") class BookSchema(JsonLDSchema): _id = fields.Id() name = fields.String(schema.name) 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="http://example.com/books/1", name="Ilias") jsonld_dict = BookSchema().dump(book) #{ # "@id": "http://example.com/books/1", # "@type": "http://schema.org/Book", # "http://schema.org/name": "Ilias", #} jsonld_string = BookSchema().dumps(book) #'{"@id": "http://example.com/books/1", "http://schema.org/name": "Ilias", "@type": "http://schema.org/Book"}')
Deserializing objects (“Loading”)
You can also easily deserialize JSON-LD to python objects
data = { "@id": "http://example.com/books/1", "@type": "http://schema.org/Book", "http://schema.org/name": "Ilias", } book = BookSchema().load(data) #<Book(_id="http://example.com/books/1", name="Ilias")>
Support
You can reach us on our Gitter Channel.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file calamus-0.3.1.tar.gz
.
File metadata
- Download URL: calamus-0.3.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.7.10-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 597660302a2f37fa0f917030aa6a95a40efd8134d448eb10dcd4b080a67515c2 |
|
MD5 | 359e8ec67c65600c136c59cfe09023af |
|
BLAKE2b-256 | f2f9c6122d531f40e5c682325a50e29dadf89aa2f31eb3ccf7f53688fca80872 |
File details
Details for the file calamus-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: calamus-0.3.1-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.7.10-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13c1bfccf815a5c3113c2f9aac70d32daddc8304717806e07436c8bd17397084 |
|
MD5 | d2fa81306e94f27301c5f80bbc3406bf |
|
BLAKE2b-256 | c2e2036a443e2d7f166fdb31696d368ac4eb7e1eae4b782ca2c6ae1cd9ca352b |