Package: lindh-jsonobject
lindh-jsonobjectJSON serializable python3 objects.IntroductionThe purpose withlindh.jsonobjectis 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.DependenciesThere are no dependencies besides core python3.7+InstallationThis repository can be installed withpip.pipinstalllindh-jsonobjectExample>>>fromjsonimportdumps>>>fromlindh.jsonobjectimportProperty,PropertySet,EnumProperty>>>classWheel(PropertySet):...diameter=Property(float,default=1.)>>>classRating(EnumProperty):...ok='ok'...bad='bad'...good='good'>>>classCar(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 HintingYou can also specify types for properties with Type Hinting, if available:>>>fromjsonimportdumps>>>fromtypingimportList>>>fromlindh.jsonobjectimportProperty,PropertySet,EnumProperty>>>classWheel(PropertySet):...diameter:float=Property(default=1.)>>>classRating(EnumProperty):...ok='ok'...bad='bad'...good='good'>>>classCar(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:strintfloatbooldicttyping.List[T]whereTis a subclass ofPropertySetTwhereTis a subclass of EnumPropertySchema-LessThere is also included a “schema-less” mode, found underlindh.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:>>>fromlindh.jsonobjectimportDictionary>>>d=Dictionary.load('tests/test.json')>>>palle=(d.drivers....where(lambdax:x.name=="Palle Kuling")....join(d.cars,lambdadriver,car:driver.car_brand==car.brandanddriver.car_model==car.model)....single())>>>palle.rating'good'You can also use chained methods likeselect(expr),first()andextend(**items).Authorlindh.jsonobjectis written and maintained by Johan Egneblad <johan@egneblad.se>.