pyjson_translator
A simple JSON to Python object translator.
Installation
pip install -U pyjson_translator
Alternatively, you can use poetry for installation:
poetry add pyjson_translator@latest
For development purposes, use:
poetry install --with dev
Otherwise, use:
poetry install
How to Use
Serialization and Deserialization
The pyjson_translator package provides functions to serialize and deserialize various types of Python objects,
including primitive types, complex types, Pydantic models, SQLAlchemy models, and some simple classes.
When pyjson_translator encounters an unhandled type, it fails quickly and throws an exception. This helps to quickly diagnose the problem and make adjustments as needed.
Basic Types
from pyjson_translator.serialize import serialize_value, deserialize_value
int_value = 123
str_value = "hello"
bool_value = True
# Serialize basic types
serialized_int = serialize_value(123)
serialized_str = serialize_value("hello")
serialized_bool = serialize_value(True)
# Deserialize basic types
deserialize_value(serialized_int, int)
deserialize_value(serialized_str, str)
deserialize_value(serialized_bool, bool)
Complex Types
from pyjson_translator.serialize import serialize_value, deserialize_value
complex_value = 3 + 4j
# Serialize complex types
# {'real': 3.0, 'imaginary': 4.0}
serialized_complex = serialize_value(complex_value)
# Deserialize basic types
# (3+4j)
deserialize_value(serialized_complex, complex)
Pydantic Models
from pydantic import BaseModel
from pyjson_translator.serialize import serialize_value, deserialize_value
class ExampleModel(BaseModel):
id: int
name: str
active: bool = True
example_model = ExampleModel(id=1, name="Example", active=True)
# Serialize Pydantic model
# {'id': 1, 'name': 'Example', 'active': True}
serialized_model = serialize_value(example_model)
# Deserialize Pydantic model
# id=1 name='Example' active=True
deserialized_model = deserialize_value(serialized_model, ExampleModel)
SQLAlchemy Models
from pyjson_translator.serialize import serialize_value, deserialize_value
from pyjson_translator.db_sqlalchemy_instance import default_sqlalchemy_instance as db
class AddressClass(db.Model):
__tablename__ = 'addresses_table'
id = db.Column(db.Integer, primary_key=True)
street = db.Column(db.String(100))
city = db.Column(db.String(50))
state = db.Column(db.String(20))
zip = db.Column(db.String(10))
user_id = db.Column(db.Integer, db.ForeignKey('users_table.id'), nullable=False)
class UserClass(db.Model):
__tablename__ = 'users_table'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
email = db.Column(db.String(120), unique=True)
address = db.relationship("AddressClass", backref="user", lazy='select', passive_deletes="all")
address_instance = AddressClass(id=1, street="123 Main St", city="New York", state="NY", zip="10001", user_id=1)
user_instance = UserClass(id=1, username="john_doe", email="john@example.com", address=[address_instance])
# Serialize SQLAlchemy model
# {'address': [{'city': 'New York',
# 'id': 1,
# 'state': 'NY',
# 'street': '123 Main St',
# 'zip': '10001'}],
# 'email': 'john@example.com',
# 'id': 1,
# 'username': 'john_doe'}
serialized_user = serialize_value(user_instance)
# Deserialize SQLAlchemy model
# {'address': [{'city': 'New York',
# 'id': 1,
# 'state': 'NY',
# 'street': '123 Main St',
# 'zip': '10001'}],
# 'email': 'john@example.com',
# 'id': 1,
# 'username': 'john_doe'}
deserialized_user = deserialize_value(serialized_user, UserClass)
Simple Classes
from pyjson_translator.serialize import serialize_value, deserialize_value
class SimpleModel:
def __init__(self, simple_id, name, active):
self.simple_id = simple_id
self.name = name
self.active = active
def __repr__(self):
return f"<SimpleModel simple_id={self.simple_id}, name={self.name}, active={self.active}>"
example_model = SimpleModel(simple_id=1, name="Example", active=True)
# Serialize simple class
# {'simple_id': 1, 'name': 'Example', 'active': True}
serialized_simple_model = serialize_value(example_model)
# Deserialize simple class
# <SimpleModel simple_id=1, name=Example, active=True>
deserialized_simple_model = deserialize_value(serialized_simple_model, SimpleModel)
List with Simple Class
from typing import List
from pyjson_translator.serialize import serialize_value, deserialize_value
class SimpleModel:
def __init__(self, simple_id, name, active):
self.simple_id = simple_id
self.name = name
self.active = active
def __repr__(self):
return f"<SimpleModel simple_id={self.simple_id}, name={self.name}, active={self.active}>"
example_model = SimpleModel(simple_id=1, name="Example", active=True)
example_model2 = SimpleModel(simple_id=2, name="Example", active=True)
simple_model_list = [example_model, example_model2]
# Serialize a list with simple class
# [{'simple_id': 1, 'name': 'Example', 'active': True}, {'simple_id': 2, 'name': 'Example', 'active': True}]
serialized_simple_model_list = serialize_value(simple_model_list)
# Deserialize simple class
# [<SimpleModel simple_id=1, name=Example, active=True>, <SimpleModel simple_id=2, name=Example, active=True>]
deserialized_simple_model_list = deserialize_value(serialized_simple_model_list, List[SimpleModel])
More Examples
For more examples and detailed usage, please refer to the tests directory in the repository.
Metadata
Release files for pyjson_translator 0.1.27
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 |
|---|---|---|---|---|
| pyjson_translator-0.1.27.tar.gz | 12.4 kB | Without provenance | Details for pyjson_translator-0.1.27.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyjson_translator-0.1.27-py3-none-any.whl | Python 3 | none | any | Details for pyjson_translator-0.1.27-py3-none-any.whl |
Total release size: 25.5 kB
Release files / pyjson_translator-0.1.27.tar.gz
| Download URL | pyjson_translator-0.1.27.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7b75555d22a6220533f1fccd8e8d59f6962a5b0b0a41762cb94a20f33547ded7
|
|
BLAKE2b-256 checksum How to use checksums |
a8ebd5a382a2023f55466344dff3dcb5c87920f17e4e5503462692e545a8d880
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.13 Darwin/24.0.0
|
Release files / pyjson_translator-0.1.27-py3-none-any.whl
| Download URL | pyjson_translator-0.1.27-py3-none-any.whl |
|---|---|
| Size | 13.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a488e4a18f9742d7327f47bb95491d0f645aa6a4b3f1d4f2a438730e9c64439d
|
|
BLAKE2b-256 checksum How to use checksums |
522650699221363da02f9d111e4c08b181f6f380fe20135b427fb5e4efdeda4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.13 Darwin/24.0.0
|
Security for pyjson_translator 0.1.27
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.