Package: jsons
Turn Python objects into dicts or (json)strings and backNo changes required to your objectsEasily customizable and extendableWorks with dataclasses, attrs and POPOsðŸ’— this lib? Leave a â˜… and tell your colleagues!Example of a model to serialize:>>>@dataclass...classPerson:...name:str...birthday:datetime...>>>p=Person('Guido van Rossum',birthday_guido)Example of using jsons to serialize:>>>out=jsons.dump(p)>>>out{'birthday':'1956-01-31T12:00:00Z','name':'Guido van Rossum'}Example of using jsons to deserialize:>>>p2=jsons.load(out,Person)>>>p2Person(name='Guido van Rossum',birthday=datetime.datetime(1956,1,31,12,0,tzinfo=datetime.timezone.utc))Installationpip install jsonsUsageimportjsonssome_instance=jsons.load(some_dict,SomeClass)# Deserializationsome_dict=jsons.dump(some_instance)# SerializationIn some cases, you have instances that contain other instances that need (de)serialization, for instance with lists or dicts. You can use thetypingclasses for this as is demonstrated below.fromtypingimportList,Tupleimportjsons# For more complex deserialization with generic types, use the typing modulelist_of_tuples=jsons.load(some_dict,List[Tuple[AClass,AnotherClass]])(For more examples, see theFAQ)DocumentationMain documentationAPI docsFAQMetaRecent updates1.6.3Bugfix: a string was sometimes unintentionally parsed into a datetime.1.6.2Bugfix:fork_insts were not propagated indefault_list_deserializer(thanks to patrickguenther).1.6.1Bugfix: Loading dicts with hashed keys could cause an error due to being loaded twice (thanks to georgeharker).Bugfix: IntEnums were not serialized with their names whenuse_enum_name=True(thanks to georgeharker).Bugfix: Named tuples did not usetyping.get_type_hintsfor getting the types, causing trouble in future annotations (thanks to georgeharker).1.6.0Feature: Support for Python3.10.Feature: Support forattrs.1.5.1Bugfix:ZoneInfofailed to dump if attached to adatetime.1.5.0Feature: Support forZoneInfoon Python3.9+.Change: microseconds are no longer stripped by default (thanks to pietrodn).1.4.2Bugfix: get_origin did not work with python3.9+ parameterized collections (e.g.dict[str, str]).1.4.1Bugfix: Types of attributes that are not in the constructor were not properly looked for. See issue #128.1.4.0Feature: DefaultDicts can now be deserialized.Feature: Dicts with any (hashable) key can now be dumped and loaded.Feature: Suppress specific warnings.Bugfix: Loading a verbose-serialized object in a list could sometimes deserialize that object as a parent class.Bugfix: Unwanted stringification of NoneValues is now prevented in Optionals and Unions with NoneType.Bugfix: Fixed a bug with postponed annotations and dataclasses. See alsoIssue34776.Bugfix: Types of attributes that are not in the constructor are now looked for inannotations.1.3.1Bugfix: Fixed bug where classmethods were included in the serialized result.1.3.0Feature: Addedwarn_on_failparameter todefault_list_deserializerthat allows to continue deserialization upon errors.Feature: Addedtransformthat can transform an object to an object of another type.Feature: Added serializer and deserializer forpathlib.Path(thanks to alexmirrington).Change: When loading a list fails, the error message now points to the failing index.Bugfix: Fixed bug when dumping an object with an innerclass.1.2.0Bugfix: Fixed bug with postponed typehints (PEP-563).Bugfix: Loading an invalid value targeting an optional did not raise.Bugfix: Loading a dict did not properly pass key_transformers.Bugfix: Loading a namedtuple did not properly use key_transformers.Bugfix: Utilized__annotations__in favor_field_typesbecause of deprecation as of 3.8.1.1.2Feature: Added__version__which can be imported fromjsonsBugfix: Dumping a tuple with ellipsis failed in strict mode.1.1.1Feature: Added a serializer forUniontypes.Change: Exceptions are more clear upon deserialization failure (thanks to haluzpav).Change: You can no longer announce a class with a custom name.Bugfix: Fixed dumping optional attributes.Bugfix: Dataclasses inheriting fromJsonSerializablealways dumped their attributes as if in strict mode.1.1.0Feature: Addedstrictparameter todumpto indicate that dumping a certainclswill ignore any extra data.Feature: When usingdump(obj, cls=x),xcan now be any class (previously, only a class with__slots__).Feature: Support for dumpingDecimal(thanks to herdigiorgi).Feature: Primitives are now cast if possible when dumping (e.g.dump(5, str)).Feature: Dumping iterables with generic types (e.g.dump(obj, List[str])) will now dump with respect to that types (ifstrict)Feature: Thedefault_dictserializer now optionally accepts types:Optional[Dict[str, type]].Change: Improved performance when dumping usingstrict=True(up to 4 times faster!).Bugfix:set_validatorwith multiple types did not work.1.0.0Feature: Added a serializer/deserializer fortime.Feature: Added a serializer/deserializer fortimezone.Feature: Added a serializer/deserializer fortimedelta.Feature: Added a serializer/deserializer fordate.Bugfix: Dumping verbose did not store the types of dicts (Dict[K, V]).Bugfix: Loading withList(no generic type) failed.Bugfix: Loading withDict(no generic type) failed.Bugfix: Loading withTuple(no generic type) failed.ContributorsSpecial thanks to the following contributors of code, discussions or
suggestions:patrickguenther,davetapley,pietrodn,georgeharker,aecay,bibz,thijss,alexmirrington,tirkarthi,marksomething,herdigiorgi,jochembroekhoff,robinklaassen,ahmetkucuk,casparjespersen,cypreess,gastlich,jmolinski,haluzpav,finetuned89