For serializing Python objects to JSON and back
Project description
jsons
A Python lib (Python3.5+) for serializing Python objects to dicts or strings and for deserializing dicts or strings to Python objects.
Installation
pip install jsons
Usage
import jsons
some_instance = jsons.load(some_dict, SomeClass) # Deserialization
some_dict = jsons.dump(some_instance) # Serialization
API overview
dump(obj: object) -> dict: serializes an object to a dict.load(json_obj: dict, cls: type = None) -> object: deserializes a dict to an object of typecls.dumps(obj: object, *args, **kwargs) -> str: serializes an object to a string.loads(s: str, cls: type = None, *args, **kwargs) -> objectdeserializes a string to an object of typecls.set_serializer(c: callable, cls: type) -> None: sets a custom serialization function for typecls.set_deserializer(c: callable, cls: type) -> None: sets a custom deserialization function for typecls.
Example with dataclasses
from dataclasses import dataclass
from typing import List
import jsons
# You can use dataclasses (since Python3.7). Regular Python classes (Python3.5+) will work as well as long as type hints
# are present for custom classes.
@dataclass
class Student:
name: str
@dataclass
class ClassRoom:
students: List[Student]
c = ClassRoom([Student('John'), Student('Mary'), Student('Greg'), Student('Susan')])
dumped_c = jsons.dump(c)
print(dumped_c)
# Prints:
# {'students': [{'name': 'John'}, {'name': 'Mary'}, {'name': 'Greg'}, {'name': 'Susan'}]}
loaded_c = jsons.load(dumped_c, ClassRoom)
print(loaded_c)
# Prints:
# ClassRoom(students=[Student(name='John'), Student(name='Mary'), Student(name='Greg'), Student(name='Susan')])
Example with regular classes
from typing import List
import jsons
class Student:
# Since ``name`` is expected to be a string, no type hint is required.
def __init__(self, name):
self.name = name
class ClassRoom:
# Since ``Student`` is a custom class, a type hint must be given.
def __init__(self, students: List[Student]):
self.students = students
c = ClassRoom([Student('John'), Student('Mary'), Student('Greg'), Student('Susan')])
dumped_c = jsons.dump(c)
print(dumped_c)
# Prints:
# {'students': [{'name': 'John'}, {'name': 'Mary'}, {'name': 'Greg'}, {'name': 'Susan'}]}
loaded_c = jsons.load(dumped_c, ClassRoom)
print(loaded_c)
# Prints:
# <__main__.ClassRoom object at 0x0337F9B0>
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
jsons-0.0.2.tar.gz
(4.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
jsons-0.0.2-py3-none-any.whl
(5.1 kB
view details)
File details
Details for the file jsons-0.0.2.tar.gz.
File metadata
- Download URL: jsons-0.0.2.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af84b00c7f2f92ef6e4cabf4e69f6e3b50b42e90102f221deee02cf5645f4ba8
|
|
| MD5 |
882d2ce2b75fb8f6a4f4f51a788b52ef
|
|
| BLAKE2b-256 |
2b7a98a448b12fe5ce50de2da0544fa562936e7e3a1a3d045be5fbe47cf4f186
|
File details
Details for the file jsons-0.0.2-py3-none-any.whl.
File metadata
- Download URL: jsons-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a54a638371bc1585a705e16a25296a6e3a62fd1abe7747dd828186b22e0cfb12
|
|
| MD5 |
23b859435382f1f4f1ee814ecb942986
|
|
| BLAKE2b-256 |
e482d8c244d7e837662a2e85875e89562a710428b81c1c2f0a9cbb0ddbef4279
|