Table of Contents

Class SchemaModel

The base class for models generated as classes by Bonsai.Sgen.

A model is generated as the class of the same name in the namespace declared by the SGEN_NAMESPACE attribute of its module. Pass sgen_typename in the class definition to describe a type defined elsewhere, so that a schema refers to the existing type instead of defining a new one.

A model listing ABC among its direct bases is generated as an abstract class. Its subclasses are generated as concrete classes unless they also list ABC.

A discriminated union is declared as a type alias or as a SchemaUnion, so that it is generated under its own name. A generic model has no type name, so it is exported only as a local type. A concrete subclass of a generic model has a type name of its own.

The alias of a field is its key in the schema and in the generated code, so it is used for serialization, and the title of the field is the name of the generated property. Aliases are the camelCase form of field names, and data with either form is read.

Inheritance
SchemaModel

Methods

View Source

model_dump

Serializes the model to a dict, under aliases by default, including nested types.

Declaration
def model_dump(*, by_alias: bool | None = True, **kwargs: Any) -> dict[str, Any]
Parameters
Type Name Description
bool | None by_alias
Any **kwargs
Returns
Type Description
dict[str, Any]
View Source

model_dump_json

Serializes the model to JSON, under aliases by default, including nested types.

Declaration
def model_dump_json(*, by_alias: bool | None = True, **kwargs: Any) -> str
Parameters
Type Name Description
bool | None by_alias
Any **kwargs
Returns
Type Description
str