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>
Advanced features
Overriding the default (de)serialization behavior
You may alter the behavior of the serialization and deserialization processes yourself by defining your own custom serialization/deserialization functions.
jsons.set_serializer(custom_serializer, datetime) # A custom datetime serializer.
jsons.set_deserializer(custom_deserializer, str) # A custom string deserializer.
Transforming the JSON keys
You can have the keys transformed by the serialization or deserialization process by providing a transformer function that takes a string and returns a string.
result = jsons.dump(some_obj, jsons.KEY_TRANSFORMER_CAMELCASE)
# result could be something like: {'thisIsTransformed': 123}
result = jsons.load(some_dict, SomeClass, jsons.KEY_TRANSFORMER_SNAKECASE)
# result could be something like: {'this_is_transformed': 123}
The following casing styles are supported:
KEY_TRANSFORMER_SNAKECASE # snake_case
KEY_TRANSFORMER_CAMELCASE # camelCase
KEY_TRANSFORMER_PASCALCASE # PascalCase
KEY_TRANSFORMER_LISPCASE # lisp-case
Metadata
Release files for jsons 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| jsons-0.2.3.tar.gz | 7.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jsons-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.6 kB
Release files / jsons-0.2.3.tar.gz
| Download URL | jsons-0.2.3.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
82b792f6d14fa64233460b6dd2c585cf15acffbb90fd7038c2f9a610f6932e58
|
|
BLAKE2b-256 checksum How to use checksums |
6660a38dbd592f88b5943c4e53b892803d5d000b9b71583ccb1578faf38daeb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / jsons-0.2.3-py3-none-any.whl
| Download URL | jsons-0.2.3-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8d9427a4f6cde6b788b7e612ec3fa4d6dbfb4027d004dbb717e7b7d82604994c
|
|
BLAKE2b-256 checksum How to use checksums |
8b070d6f46e140822e3adba542516409ac31897f8a8e8a64b063104e3b4b012c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |