Module osyspyrt.asn1json

Support ASN.1 JER (JSON Encoding Rules).

Classes

class Asn1JsonDecodeBuffer (textIO)

Wraps a text stream object with JSON functionality.

Parameters

textIO : TextIOBase
The underlying text stream to read from. It must be seekable.

Static methods

def from_text(string)

Create an Asn1JsonDecodeBuffer on the given JSON text.

Methods

def decode_bitstring(self, decode_cls=)

Decode ASN.1 BIT STRING from JSON.

This function decodes from a JSON object, as required by X.697 for a BIT STRING that is not constrained to a fixed length.

Parameters

decode_cls : Class
The class to instantiate. It must be a subclass of Asn1BitString, supporting the init signature of that class.

Returns

instance of decode_cls, by default an Asn1BitString.

def decode_bitstring_fixed(self, numbits, decode_cls=)

Decode ASN.1 BIT STRING from JSON.

This function decodes from a JSON string, as required by X.697 for a BIT STRING that is constrained to a fixed length.

This function ensures that the given number of bits are decoded and that unused bits are zero.

Parameters

numbits : int
The fixed size of the BIT STRING to be decoded.
decode_cls : Class
The class to instantiate. It must be a subclass of Asn1BitString, supporting the init signature of that class.

Returns

instance of decode_cls, by default an Asn1BitString.

def decode_extension(self, name=None)

Decode a JSON value. If name is None, this returns the decoded value. If name is not None, this returns a string of the form '"name":"value"'

def decode_octetstring(self, base=16)

Decode an ASN.1 OCTET STRING, encoded in the given base (16 or 64). Whitespace ahead of the string is skipped.

Returns

bytes
 
def decode_oid(self)

Decode an ASN.1 OBJECT IDENTIFIER and return it as a sequence of integers.

def decode_real(self)

Decode an ASN.1 REAL value that could be either a base 2 or base 10 abstract value.

If the value is encoded as base 2, it will be decoded to a float.
If the value is encoded as base 10, it will decoded to
decimal.Decimal.
If the value is encoded as a special value, it will be decoded to
a float.

Returns

float or decimal.Decimal as described above.

def decode_real10(self)

Decode an ASN.1 REAL value this is constrained to exclude base 2 abstract values.

Returns

decimal.Decimal
 
def decode_roid(self)

Decode an ASN.1 RELATIVE-OID and return it as a sequence of integers.

def decode_tbcd_string(self, base=16)

Decode an ASN.1 OCTET STRING value, encoded as a JSON string using the given base (16 or 64), and then interpret the OCTET STRING as a TBCD string, according to 3GPP 29.002

Whitespace ahead of the string is skipped. If a JSON string is not next on input, an exception is raised.

Returns

str
The JSON string, without the delimiting quote marks and with character escapes applied.

Raises

Asn1Error if a JSON string is not on input.

def mark(self)

Mark the current position so that we can read ahead, without limit, and return to the current position by calling resetPos.

def next_character_is(self, c)

Returns True if the next character is the given character.

This uses seek_character to see what the next character is and returns True if and only if there is a next character and it is the given character.

def next_character_is_not(self, c)

Returns true if the next character is not the given character.

This uses seek_character to see what the next character is and returns True if and only if there is a next character and it is not the given character.

def read_boolean(self)

Reads "true" or "false" from input and returns the corresponding bool value. If the input does not match "true" or "false", an error is raised.

Whitespace ahead of the "true" or "false" value is skipped.

def read_character(self, matchChar)

Read the given character from input. Leading whitespace is skipped.

This will read the next character of input. If it matches the given token, this method simply returns. If there is no next character, or if the next character is not the given character, an exception is raised.

Raises

Asn1Exception if the expected character token is not present
 
def read_int(self)

Convenience method equivalent to int(self.read_number)

def read_json_value(self)

Read the next JSON value from the input. Whitespace ahead of the next JSON value is skipped. If a JSON value cannot be read from the input, an exception is raised.

This method will generally be useful in reading an entire JSON object, array, or number. It returns every character of input making up the JSON value (i.e., it does not discard whitespace within the JSON value, or translate escaped characters inside JSON strings).

To read a JSON string, or the literals true, false, or null, see readString, readBoolean, and readNull.

Raises

Asn1Error if a JSON value cannot be read.

def read_null(self)

Read a JSON null value.

Returns

Asn1Type.null_value
 
def read_number(self)

Read a JSON number from input. This will skip any whitespace ahead of the number. If a JSON number is not available in the input, an error is raised.

Returns

str
A JSON number as a string. It is guaranteed to be a valid JSON number.
def read_string(self)

Read a JSON string from the input.

Whitespace ahead of the string is skipped. If a JSON string is not next on input, an exception is raised.

Returns

str
The JSON string, without the delimiting quote marks and with character escapes applied.

Raises

Asn1Error if a JSON string is not on input.

def reset(self)

Return the input position to the position at which mark() was previously called.

def seek_character(self)

Seeks past whitespace in the input to find the next input character.

This will skip any whitespace in the input until it finds the next input character, call it X. 'X' is not read, so all of the read* methods will still see this character, and repeated calls to seekCharacter will return the same character.

Returns

str
The next input character or "" if there is no next character.
def skip_json_value(self)

Read past any leading whitespace and the next JSON value from the input.

If a JSON value cannot be read from the input, an error is raised.

def skip_whitespace(self)

Skip (read past) all whitespace on input until there is no more input or a nonwhitespace character is the next character of input.

class Asn1JsonEncodeBuffer (**kwargs)

Extends osyspyrt.io.TextWriter for JSON

Ancestors

Methods

def encode_bitstring(self, value)

Encode ASN.1 BIT STRING to JSON.

This encodes to a JSON object, as required by X.697 for a BIT STRING that is not constrained to a fixed length.

Parameters

value : Asn1BitString, bytes, or bytearray
 
def encode_bitstring_fixed(self, value)

Encode ASN.1 BIT STRING to JSON.

This function encodes to a JSON string, as required by X.697 for a BIT STRING that is constrained to a fixed length.

Parameters

value : Asn1BitString, bytes, or bytearray
 
def encode_extensions(self, extensions, asArray)

Encode the given list of extensions. The list may include None values to represent missing extension elements, applicable when asArray is True.

Parameters

asArray : bool
True if the extensions are being encoded into an [ARRAY] SEQUENCE.
def encode_octetstring(self, value, base=16)

Encode the given OCTET STRING value as a JSON string using the given base (16 or 64) as the representation.

Parameters

value : str, bytes, or bytearray
If str, the characters are treated as ASCII characters to derive the bytes that should be encoded.
base : 16 or 64
 
def encode_oid(self, value)

Encode the given sequence of ints as ASN.1 OBJECT IDENTIFIER or RELATIVE-OID (the encoding is the same).

def encode_real(self, value)

Encode ASN.1 REAL value that is not restricted to exclude base 2 abstract values.

float and int values are treated as base 2 abstract values and encoded as JSON number. decimal.Decimal values are treated as base 10 values and encoded as JSON object.

For the special values -0, +/-inf, and nan, either type may be used and the value will be encoded as a JSON string.

Parameters

value: int, float or decimal.Decimal, as described above.

def encode_real10(self, value)

Encode an ASN.1 REAL value that is restricted to exclude base 2 abstract values.

The special values -0, +/-inf, and nan are encoded to JSON string. All other values are encoded to a JSON number.

Parameters

value: int, float or decimal.Decimal.

def encode_roid(self, value)

Encode the given sequence of ints as ASN.1 OBJECT IDENTIFIER or RELATIVE-OID (the encoding is the same).

def encode_tbcd_string(self, value, base=16)

Encode the given ASN.1 OCTET STRING, provided either as bytes or as a TBCD character string, to a JSON string, using the given base.

Parameters

value : str, bytes, bytearray
If str, translate it to bytes by interpreting it as a TBCD character string, and then encode those bytes in the given base.
base : 16 or 64
 
def write_string(self, value)

Write the given string as a JSON string.

This will encode the enclosing double quotes, and will escape the string's characters as needed.

Inherited members