JSONIFY
Description
jsonify is a Rapidy encoder used for converting complex Python objects
into simpler Python objects or strings.
jsonify greatly simplifies the process of saving and sending complex data.
from datetime import datetime
from decimal import Decimal
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class InnerData(BaseModel):
text: str = 'text'
class ComplexData(BaseModel):
decimal: Decimal = Decimal('1.22223311')
date: datetime = datetime.now()
inner: InnerData = Field(default_factory=InnerData)
jsonify('text') # 'text'
jsonify('text', dumps=True) # '"text"'
jsonify(Decimal("1.22223311")) # '1.22223311'
jsonify(ComplexData()) # {'decimal': '1.22223311', 'date': '2024-10-30T10:51:07.884276', 'inner': {'text': 'text'}}
jsonify(ComplexData(), dumps=True) # '{"decimal": "1.22223311", "date": "2024-10-30T10:51:07.884276", "inner": {"text": "text"}}'
jsonify allows you to convert any python object into a string or a simpler object that can be
converted to a json format.
You can use jsonify to prepare data for saving to a database (for example, for MongoDB/Redis or as a JSON/JSONB field in Postgres).
Rapidy also uses jsonify internally when preparing server responses.
Decimal is always converted to a string.
jsonify Attributes
obj
obj: Any — the object to be converted (can be practically anything).
include
include: set[str] | dict[str, Any] | None = None — Pydantic's include parameter, passed to Pydantic models
to specify the fields to include.
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class Result(BaseModel):
value: str = Field('data', alias='someValue')
another_value: str = Field('another_data', alias='someAnotherValue')
jsonify(
Result(),
include={'value'},
)
# {'someValue': 'data'}
exclude
exclude: set[str] | dict[str, Any] | None = None — Pydantic's exclude parameter, passed to Pydantic models
to specify the fields to exclude.
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class Result(BaseModel):
value: str = Field('data', alias='someValue')
another_value: str = Field('another_data', alias='someAnotherValue')
jsonify(
Result(),
exclude={'value'},
)
# {'someAnotherValue': 'another_data'}
by_alias
by_alias: bool = True — Pydantic's by_alias parameter, passed to Pydantic models to determine whether to use
the alias names (if provided) or the Python attribute names in the output.
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class Result(BaseModel):
value: str = Field('data', alias='someValue')
jsonify(
Result(),
by_alias=True, # <-- default
)
# {"someValue": "data"}
...
jsonify(
Result(),
by_alias=False,
)
# {"value": "data"}
exclude_unset
exclude_unset: bool = False — Pydantic's exclude_unset parameter, passed to Pydantic models to determine
whether to exclude fields that were not explicitly set (and only have default values) from the output.
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class Result(BaseModel):
value: str = Field('data', alias='someValue')
another_value: str = Field('another_data', alias='someAnotherValue')
jsonify(
Result(someAnotherValue='new_data'),
exclude_unset=False, # <-- default
)
# {"someValue": "data", "someAnotherValue": "new_data"}
...
jsonify(
Result(someAnotherValue='new_data'),
exclude_unset=True,
)
# {"someAnotherValue": "new_data"}
exclude_defaults
exclude_defaults: bool = False — Pydantic's exclude_defaults parameter, passed to Pydantic models to
determine whether to exclude fields with default values, even if they were explicitly set.
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class Result(BaseModel):
value: str = Field('data', alias='someValue')
jsonify(
Result(),
response_exclude_defaults=True,
)
# {}
exclude_none
exclude_none: bool = False — Pydantic's exclude_none parameter, passed to Pydantic models to determine
whether to exclude fields with a None value from the output.
from pydantic import BaseModel, Field
from rapidy.encoders import jsonify
class Result(BaseModel):
value: str = Field('data', alias='someValue')
none_value: None = None
jsonify(
Result(),
exclude_none=True,
)
# {"someValue": "data"}
...
jsonify(
Result(),
exclude_none=False, # default
)
# {"someValue": "data", "none_value": null}
charset
charset: str = 'utf-8' — the character set to be used for encoding and decoding the obj data.
from rapidy.enums import Charset
from rapidy.encoders import jsonify
jsonify(
'data',
charset=Charset.utf32,
# or
charset='utf32',
)
dumps
dumps: bool = True — a flag determining whether to convert the created object into a string. Can be set to False if
the object is already a json string.
dumps_encoder
dumps_encoder: Callable = json.dumps — any callable object that takes an object and returns a JSON string.
from typing import Any
from rapidy.encoders import jsonify
def custom_encoder(obj: Any) -> str:
...
jsonify(
'data',
dumps_encoder=custom_encoder,
)
custom_encoder
custom_encoder: Callable | None = None — Pydantic's custom_encoder parameter, passed to Pydantic models to
define a custom encoder.