Three types of Python encode functions/methods are available for encoding different types of data. These are:
Standard base run-time functions. These exist in the
asn1json.py run-time module, in the
Asn1JsonEncodeBuffer
run-time class. Examples are encode_bitstring and
encode_real, but for many types,
write or write_string are all
that is needed.
Static methods in Python classes, such as for primitive types and
SEQUENCE/SET OF, as described above.
The method name in this case is json_encode.
Instance methods in Python classes for constructed types. Classes
are generated for SEQUENCE, SET,
and CHOICE constructs.
The encode method in this case encodes the instance attributes which
correspond to the ASN.1 elements defind in the ASN.1 types.
The method name is also json_encode, as it was in the
static method case.
The encode methods do not have a return value and will raise an exception if the encoding fails.
The encode methods on Asn1JsonEncodeBuffer
have a value parameter for the value to be encoded, and in some cases
additional parameters (e.g.
encode_octetstring has a parameter for the base for
the encoding, base). Of course, these methods are
invoked on an instance of Asn1JsonEncodeBuffer.
The generated encode methods accept an
Asn1JsonEncodeBuffer, the buffer to encode to.
Static methods additionally accept the value to be encoded. Some
examples:
@staticmethod def json_encode(encbuf, value): def json_encode(self, encbuf):