Functions

pyswark.lib.pydantic.ser_des.toJson(model: Type[BaseModel], **kw) str

Serialize a Pydantic model to JSON with embedded type information.

The serialized output includes the model’s fully-qualified class name, enabling automatic deserialization via fromJson().

Parameters:
  • model (BaseModel) – A Pydantic model instance to serialize.

  • **kw – Additional keyword arguments passed to json.dumps() (e.g., indent=2 for pretty-printing).

Returns:

JSON string with structure: {"model": "module.Class", "contents": {...}}

Return type:

str

See also

fromJson

Deserialize JSON back to the original model type.

toDict

Convert to dict without JSON encoding.

Example

>>> json_str = toJson(my_model, indent=2)
pyswark.lib.pydantic.ser_des.fromJson(loadable: str) Type[BaseModel]

Deserialize JSON to its original Pydantic model type.

The JSON must have been created with toJson() (or have the same structure with model and contents keys).

Parameters:

loadable (str) – JSON string containing embedded type information.

Returns:

An instance of the original model class.

Return type:

BaseModel

Raises:

TypeError – If the model path cannot be resolved or is not a BaseModel subclass.

See also

toJson

Serialize a model with type information.

fromDict

Deserialize from a dictionary.

Example

>>> restored = fromJson(json_string)
>>> print(type(restored).__name__)
'Character'