1.15. serializers

This module provides a standardized interface for serializing objects using different formats. The Serializers provided by this module are organized by their format into different classes. The necessary methods for utilizing them are all classmethod’s making it unnecessary to create an instance of any of them.

1.15.1. Functions

from_elementtree_element(element, require_type=True)[source]

Load a value from an xml.etree.ElementTree.SubElement instance. If require_type is True, then the element must specify an acceptable value via the “type” attribute. If require_type is False and no type attribute is specified, the value is returned as a string.

Parameters:
Returns:

The deserialized value from the element.

to_elementtree_subelement(parent, tag, value, attrib=None)[source]

Serialize value to an xml.etree.ElementTree.SubElement with appropriate information describing it’s type. If value is not of a supported type, a TypeError will be raised.

Parameters:
  • parent (xml.etree.ElementTree.Element) – The parent element to associate this subelement with.
  • tag (str) – The name of the XML tag.
  • value – The value to serialize to an XML element.
  • attrib (dict) – Optional attributes to include in the element.
Returns:

The newly created XML element, representing value.

Return type:

xml.etree.ElementTree.Element

1.15.2. Classes

class JSON[source]

Bases: king_phisher.serializers.Serializer

classmethod dumps(data, pretty=True)[source]

Convert a Python object to a JSON encoded string.

Parameters:
  • data – The object to encode.
  • pretty (bool) – Set options to make the resulting JSON data more readable.
Returns:

The encoded data.

Return type:

str

classmethod loads(data, strict=True)[source]

Load JSON encoded data.

Parameters:
  • data (str) – The encoded data to load.
  • strict (bool) – Do not try remove trailing commas from the JSON data.
Returns:

The Python object represented by the encoded data.

class MsgPack[source]

Bases: king_phisher.serializers.Serializer

classmethod dumps(data)[source]

Convert a Python object to a MsgPack encoded bytes instance.

Parameters:
  • data – The object to encode.
  • pretty (bool) – Set options to make the resulting JSON data more readable.
Returns:

The encoded data.

Return type:

str

classmethod loads(data)[source]

Load MsgPack encoded data.

Parameters:data (bytes) – The encoded data to load.
Returns:The Python object represented by the encoded data.
class Serializer[source]

Bases: king_phisher.serializers._SerializerMeta

The base class for serializer objects of different formats and protocols. These serializers are extended using a King Phisher-specific protocol for serializing additional types, most notably Python’s datetime.datetime type.

Note

None of the serializers handle Python 3’s bytes type. These objects will be treated as strings and silently converted.

classmethod dump(data, file_h, *args, **kwargs)[source]

Write a Python object to a file by encoding it with this serializer.

Parameters:
  • data – The object to encode.
  • file_h (file) – The file to write the encoded string to.
encoding = 'utf-8'[source]

The encoding which this serializer uses for handling strings.

classmethod load(file_h, *args, **kwargs)[source]

Load encoded data from the specified file.

Parameters:
  • file_h (file) – The file to read and load encoded data from.
  • strict (bool) – Do not try remove trailing commas from the JSON data.
Returns:

The Python object represented by the encoded data.