API¶
This part of the documentation lists the full API reference of all public classes and functions.
- class serde.Model(*args, **kwargs)[source]¶
- The base model. - to_dict()[source]¶
- Convert this model to a dictionary. - Returns:
- the model serialized as a dictionary. 
- Return type:
 
 - to_json(**kwargs)[source]¶
- Dump the model as a JSON string. - Parameters:
- **kwargs – extra keyword arguments to pass directly to - json.dumps.
- Returns:
- a JSON representation of this model. 
- Return type:
 
 - classmethod from_json(s, **kwargs)[source]¶
- Load the model from a JSON string. - Parameters:
- s (str) – the JSON string. 
- **kwargs – extra keyword arguments to pass directly to - json.loads.
 
- Returns:
- an instance of this model. 
- Return type:
 
 
Fields¶
This module contains field classes for use with Models.
- class serde.fields.Field(rename=None, default=None, serializers=None, deserializers=None, normalizers=None, validators=None)[source]¶
- A field on a - Model.- Fields handle serializing, deserializing, normalization, and validation of input values for - Modelobjects.- Parameters:
- rename (str) – override the name for the field when serializing and expect this name when deserializing. 
- default – a value to use if there is no input field value or the input value is - None. This can also be a callable that generates the default. The callable must take no positional arguments. This default only applies to instantiated values. Field values are still required on deserialization.
- serializers (list) – a list of serializer functions taking the value to serialize as an argument. The functions need to raise an - Exceptionif they fail. These serializer functions will be applied before the primary serializer on this Field.
- deserializers (list) – a list of deserializer functions taking the value to deserialize as an argument. The functions need to raise an - Exceptionif they fail. These deserializer functions will be applied after the primary deserializer on this Field.
- normalizers (list) – a list of normalizer functions taking the value to normalize as an argument. The functions need to raise an - Exceptionif they fail. These normalizer functions will be applied after the primary normalizer on this Field.
- validators (list) – a list of validator functions taking the value to validate as an argument. The functions need to raise an - Exceptionif they fail.
 
 
Primitives¶
- class serde.fields.Bool(**kwargs)¶
- This field represents the built-in - <class 'bool'>type.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Bytes(**kwargs)¶
- This field represents the built-in - <class 'bytes'>type.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Complex(**kwargs)¶
- This field represents the built-in - <class 'complex'>type.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Float(**kwargs)¶
- This field represents the built-in - <class 'float'>type.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Int(**kwargs)¶
- This field represents the built-in - <class 'int'>type.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Str(**kwargs)¶
- This field represents the built-in - <class 'str'>type.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Text(encoding=None, errors='strict', **kwargs)[source]¶
- A text field. - A - Textis a string field in Python 3 and a unicode field in Python 2. It will normalize byte strings into unicode strings using the given encoding.- Parameters:
- encoding (str) – the encoding with which to decode bytes. Passed directly to - bytes.decode. If not given then- chardet.detectwill be used to detect the encoding.
- errors (str) – The error handling scheme to use for the handling of decoding errors. Passed directly to - bytes.decode.
- **kwargs – keyword arguments for the - Fieldconstructor.
 
 
Containers¶
In container fields each element is serialized, deserialized, normalized and
validated with the specified element type. The element type can be specified
using Field classes, Field instances, Model classes, or built-in
types that have a corresponding field type in this library.
- class serde.fields.Nested(ty, **kwargs)[source]¶
- A field for - Modelfields.- A - Nestedis a wrapper field for models to support sub-models. The serialize and deserialize methods call the- to_dict()and- from_dict()methods on the model class. This allows complex nested models.- Parameters:
- model_cls (serde.Model) – the nested model class. 
- **kwargs – keyword arguments for the - Fieldconstructor.
 
 
- class serde.fields.Optional(inner=None, **kwargs)[source]¶
- An optional field. - An - Optionalis a field that is allowed to be- None. Serialization, normalization, deserialization, and validation using the wrapped field will only be called if the value is not- None.
- class serde.fields.Dict(key=None, value=None, **kwargs)[source]¶
- This field represents the built-in - dicttype.
- class serde.fields.OrderedDict(key=None, value=None, **kwargs)[source]¶
- An - OrderedDictfield.- Parameters:
- key – the - Fieldclass or instance for keys in this- OrderedDict.
- value – the - Fieldclass or instance for values in this- OrderedDict.
- **kwargs – keyword arguments for the - Fieldconstructor.
 
 
- class serde.fields.List(element=None, **kwargs)[source]¶
- This field represents the built-in - listtype.
- class serde.fields.Set(element=None, **kwargs)[source]¶
- This field represents the built-in - settype.
Standard library¶
- class serde.fields.DateTime(format='iso8601', **kwargs)[source]¶
- A - datetimefield.- This field serializes - datetimeobjects as strings and deserializes string representations of datetimes as- datetimeobjects.- The date format can be specified. It will default to ISO 8601. 
- class serde.fields.Date(format='iso8601', **kwargs)[source]¶
- A - datefield.- This field behaves in a similar fashion to the - DateTimefield.
- class serde.fields.Time(format='iso8601', **kwargs)[source]¶
- A - timefield.- This field behaves in a similar fashion to the - DateTimefield.
- class serde.fields.Uuid(output_form='str', **kwargs)[source]¶
- A - UUIDfield.- A - Uuidfield validates that the input data is a UUID. It serializes the UUID into the specified output form and deserializes hex strings, bytes, fields, or integers as UUIDs.
- class serde.fields.Regex(pattern, flags=0, **kwargs)[source]¶
- A regex field. - A - Regexis a string field that validates that data matches a specified regex expression.- Parameters:
- pattern (str) – the regex pattern that the value must match. 
- flags (int) – the regex flags passed directly to - re.compile.
- **kwargs – keyword arguments for the - Fieldconstructor.
 
 
- class serde.fields.Decimal(resolution=None, **kwargs)[source]¶
- A - Decimalfield.- This field serializes - Decimalobjects as strings and deserializes string representations of Decimals as- Decimalobjects.- The resolution of the decimal can be specified. When not specified, the number is not rounded. When it is specified, the decimal is rounded to this number of decimal places upon serialization and deserialization. - Note: When float type numbers are not rounded before serialization, they will be serialized in exact form, which as they are floats, is almost never the exact intended value, e.g. 0.2 = 0.20000000000000000000023 
Miscellanous¶
- class serde.fields.Choice(choices, **kwargs)[source]¶
- One of a given selection of values. - A - Choicefield checks if the input data is one of the allowed values. These values do not need to be the same type.- Parameters:
- choices – a list or range or tuple of allowed values. 
- **kwargs – keyword arguments for the - Fieldconstructor.
 
 
Extended¶
The following fields are available with the ext feature.
- class serde.fields.Domain(**kwargs)¶
- A text field that asserts the text is a valid domain. - The validation is delegated to - validators.domain.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Email(**kwargs)¶
- A text field that asserts the text is a valid email. - The validation is delegated to - validators.email.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Ipv4Address(**kwargs)¶
- A text field that asserts the text is a valid IPv4 address. - The validation is delegated to - validators.ip_address.ipv4.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Ipv6Address(**kwargs)¶
- A text field that asserts the text is a valid IPv6 address. - The validation is delegated to - validators.ip_address.ipv6.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.MacAddress(**kwargs)¶
- A text field that asserts the text is a valid MAC address. - The validation is delegated to - validators.mac_address.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Slug(**kwargs)¶
- A text field that asserts the text is a valid slug. - The validation is delegated to - validators.slug.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
- class serde.fields.Url(**kwargs)¶
- A text field that asserts the text is a valid URL. - The validation is delegated to - validators.url.- Parameters:
- **kwargs – keyword arguments for the - Fieldconstructor.
 
Validators¶
This module contains validators for use with Fields.
- class serde.validators.Validator[source]¶
- An abstract validator class that all validators should subclass. 
- class serde.validators.Min(endpoint, inclusive=True)[source]¶
- A validator that asserts the value is greater than a minimum. 
- class serde.validators.Max(endpoint, inclusive=True)[source]¶
- A validator that asserts the value is less than a maximum. 
- class serde.validators.Between(min_endpoint, max_endpoint, inclusive=True)[source]¶
- A validator that asserts the value is between two endpoints. 
- class serde.validators.Length(length)[source]¶
- A validator that asserts the value’s length is a specific value. 
- class serde.validators.LengthMin(endpoint, inclusive=True)[source]¶
- A validator that asserts the value’s length is greater than a minimum. 
Exceptions¶
This module contains Exception classes that are used in Serde.