Native Python types


Turns ASN.1 object into a Python built-in type object(s).

Takes any ASN.1 object (e.g. PyAsn1Item derivative) walks all its components recursively and produces a Python built-in type or a tree of those.

One exception is that instead of dict, the OrderedDict can be produced (whenever available) to preserve ordering of the components in ASN.1 SEQUENCE.

Parameters:pyasn1 object to encode (or a tree of them)
Returns:object – Python built-in type instance (or a tree of them)
Raises:PyAsn1Error – On encoding errors


Encode ASN.1 value object into native Python types

>>> seq = SequenceOf(componentType=Integer())
>>> seq.extend([1, 2, 3])
>>> encode(seq)
[1, 2, 3]
pyasn1.codec.native.decoder.decode(pyObject, asn1Spec)

Turns Python objects of built-in types into ASN.1 objects.

Takes Python objects of built-in types and turns them into a tree of ASN.1 objects (e.g. PyAsn1Item derivative) which may be a scalar or an arbitrary nested structure.

Parameters:pyObject (object) – A scalar or nested Python objects
Keyword Arguments:
 asn1Spec (any pyasn1 type object e.g. PyAsn1Item derivative) – A pyasn1 type object to act as a template guiding the decoder. It is required for successful interpretation of Python objects mapping into their ASN.1 representations.
Returns:PyAsn1Item derivative – A scalar or constructed pyasn1 object
Raises:PyAsn1Error – On decoding errors


Decode native Python object into ASN.1 objects with ASN.1 schema

>>> seq = SequenceOf(componentType=Integer())
>>> s, _ = decode([1, 2, 3], asn1Spec=seq)
>>> str(s)
 1 2 3