lindh-jsonobject
JSON serializable python3 objects.
Introduction
The purpose with lindh.jsonobject is to provide a way to serialize and deserialize python3 objects into and from JSON so that they can be communicated with other application and stored into document databases such as CouchDB.
Some code and inspiration comes from the Django project, and the objects behave much like such. However, while Django objects are meant for relational databases, these are meant to be used with complex objects in document databases.
Dependencies
There are no dependencies besides core python3.7+
Installation
This repository can be installed with pip.
pip install lindh-jsonobject
Example
>>> from json import dumps
>>> from lindh.jsonobject import Property, PropertySet, EnumProperty
>>> class Wheel(PropertySet):
... diameter = Property(float, default=1.)
>>> class Rating(EnumProperty):
... ok = 'ok'
... bad = 'bad'
... good = 'good'
>>> class Car(PropertySet):
... wheels = Property(type=Wheel, is_list=True)
... brand = Property()
... model = Property()
... rating = Property(enum=Rating, default=Rating.ok)
>>> volvo = Car(brand='Volvo', model='V70', rating=Rating.good)
>>> print(volvo.to_json())
{
"*schema": "Car",
"brand": "Volvo",
"model": "V70",
"rating": "good",
"wheels": []
}
>>> volvo.wheels.append(Wheel(diameter=2.))
>>> print(volvo.to_json())
{
"*schema": "Car",
"brand": "Volvo",
"model": "V70",
"rating": "good",
"wheels": [
{
"*schema": "Wheel",
"diameter": 2.0
}
]
}
>>> volvo.wheels.append(Wheel(diameter=2.))
>>> print(volvo.to_json())
{
"*schema": "Car",
"brand": "Volvo",
"model": "V70",
"rating": "good",
"wheels": [
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 2.0
}
]
}
>>> volvo.wheels.append(Wheel(diameter=2.))
>>> volvo.wheels.append(Wheel()) # using default value here
>>> print(volvo.to_json())
{
"*schema": "Car",
"brand": "Volvo",
"model": "V70",
"rating": "good",
"wheels": [
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 1.0
}
]
}
>>> volvo2 = Car.FromJSON(volvo.to_json())
>>> print(volvo2.to_json())
{
"*schema": "Car",
"brand": "Volvo",
"model": "V70",
"rating": "good",
"wheels": [
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 2.0
},
{
"*schema": "Wheel",
"diameter": 1.0
}
]
}
Type Hinting
You can also specify types for properties with Type Hinting, if available:
>>> from json import dumps
>>> from typing import List
>>> from lindh.jsonobject import Property, PropertySet, EnumProperty
>>> class Wheel(PropertySet):
... diameter: float = Property(default=1.)
>>> class Rating(EnumProperty):
... ok = 'ok'
... bad = 'bad'
... good = 'good'
>>> class Car(PropertySet):
... wheels: List[Wheel] = Property()
... brand = Property()
... model = Property()
... rating: Rating = Property(default=Rating.ok)
>>> volvo = Car(brand='Volvo', model='V90', rating=Rating.good, wheels=[])
>>> volvo.wheels.append(Wheel(diameter=3.))
>>> print(volvo.to_json())
{
"*schema": "Car",
"brand": "Volvo",
"model": "V90",
"rating": "good",
"wheels": [
{
"*schema": "Wheel",
"diameter": 3.0
}
]
}
Supported types:
str
int
float
bool
dict
typing.List[T] where T is a subclass of PropertySet
T where T is a subclass of EnumProperty
Schema-Less
There is also included a “schema-less” mode, found under lindh.jsonobject.noschema. The idea is to provide an easy-to-use read-only LINQ-like way of exploring JSON-like files. Here is a small example:
>>> from lindh.jsonobject import Dictionary
>>> d = Dictionary.load('tests/test.json')
>>> palle = (d.drivers
... .where(lambda x: x.name == "Palle Kuling")
... .join(d.cars, lambda driver, car: driver.car_brand == car.brand and driver.car_model == car.model)
... .single())
>>> palle.rating
'good'
You can also use chained methods like select(expr), first() and extend(**items).
Metadata
Release files for lindh-jsonobject 2023.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lindh-jsonobject-2023.2.tar.gz | 15.2 kB | Without provenance | Details for lindh-jsonobject-2023.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lindh_jsonobject-2023.2-py3-none-any.whl | Python 3 | none | any | Details for lindh_jsonobject-2023.2-py3-none-any.whl |
Total release size: 23.4 kB
Release files / lindh-jsonobject-2023.2.tar.gz
| Download URL | lindh-jsonobject-2023.2.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b8cca172e7704adf4a86974801145f58261cda437ac4c359f517b0582ac593b2
|
|
BLAKE2b-256 checksum How to use checksums |
a3138f0409c223616d71763fb944e65f40c622a66f391a091d5a4fdf32807a81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / lindh_jsonobject-2023.2-py3-none-any.whl
| Download URL | lindh_jsonobject-2023.2-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
15d3f67ff99cf9ee4495449295f44ad45cfbd11f0582b8e0305ad6e1a800a534
|
|
BLAKE2b-256 checksum How to use checksums |
03a5f9e5394b9980da482da20c93a8c99e29911b9311c4356bfbcc625e8f78d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for lindh-jsonobject 2023.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.