openc2lib.encoders.json

JSON Encoding

This module provides the code for encoding OpenC2 messages with JSON.

 1""" JSON Encoding
 2
 3	This module provides the code for encoding OpenC2 messages with JSON.
 4"""
 5import json
 6
 7from openc2lib import Encoder, register_encoder
 8
 9
10@register_encoder
11class JSONEncoder(Encoder):
12	""" JSON Encoder
13	
14		This class implements the `Encoder` interface for the JSON format. It leverages
15		the intermediary dictionary representation.
16
17		The `JSONEncoder` can be used to create an OpenC2 stack in `Consumer` and `Producer`.
18	"""
19	encoder_type = 'json'
20	""" The label that is used to identify this `Encoder` in OpenC2 messages. """
21
22	@staticmethod
23	def encode(obj):
24		""" Encode an OpenC2 object
25
26			This method is used to encode an openc2lib object, which usually is a `Command` or `Message`. 
27			The implementation leverages the intermediary dictionary representation and it is
28			therefore agnostic of openc2lib clases.
29
30			:param obj: A valid openc2lib object.
31			:return: A string with the json representation of the `obj`.
32
33		"""
34		return json.dumps(Encoder.todict(obj))
35
36	@staticmethod
37	def decode(msg, msgtype=None):
38		""" Decode an OpenC2 message
39
40			This method is used to create an openc2lib object of type `msgtype` from a json record. 
41			The openc2lib class `msgtype` corresponding to the json record `msg` must be explicitly provided,
42			since parsing and automatically inferring the `msgtype` is not currently implemented.
43
44			The implementation leverages the intermediary dictionary representation and it is
45			therefore agnostic of openc2lib classes.
46
47			:param msg: The json record to decode.
48			:param msgtype: The openc2lib class to convert the json to.
49			:return: An `msgtype` class initialized according to the json content.
50		"""
51		if msgtype == None:
52			return json.loads(msg)
53
54		if isinstance(msg, str):
55			return Encoder.decode(msgtype, json.loads(msg))
56		else:
57			return Encoder.decode(msgtype, msg)
@register_encoder
class JSONEncoder(openc2lib.core.encoder.Encoder):
11@register_encoder
12class JSONEncoder(Encoder):
13	""" JSON Encoder
14	
15		This class implements the `Encoder` interface for the JSON format. It leverages
16		the intermediary dictionary representation.
17
18		The `JSONEncoder` can be used to create an OpenC2 stack in `Consumer` and `Producer`.
19	"""
20	encoder_type = 'json'
21	""" The label that is used to identify this `Encoder` in OpenC2 messages. """
22
23	@staticmethod
24	def encode(obj):
25		""" Encode an OpenC2 object
26
27			This method is used to encode an openc2lib object, which usually is a `Command` or `Message`. 
28			The implementation leverages the intermediary dictionary representation and it is
29			therefore agnostic of openc2lib clases.
30
31			:param obj: A valid openc2lib object.
32			:return: A string with the json representation of the `obj`.
33
34		"""
35		return json.dumps(Encoder.todict(obj))
36
37	@staticmethod
38	def decode(msg, msgtype=None):
39		""" Decode an OpenC2 message
40
41			This method is used to create an openc2lib object of type `msgtype` from a json record. 
42			The openc2lib class `msgtype` corresponding to the json record `msg` must be explicitly provided,
43			since parsing and automatically inferring the `msgtype` is not currently implemented.
44
45			The implementation leverages the intermediary dictionary representation and it is
46			therefore agnostic of openc2lib classes.
47
48			:param msg: The json record to decode.
49			:param msgtype: The openc2lib class to convert the json to.
50			:return: An `msgtype` class initialized according to the json content.
51		"""
52		if msgtype == None:
53			return json.loads(msg)
54
55		if isinstance(msg, str):
56			return Encoder.decode(msgtype, json.loads(msg))
57		else:
58			return Encoder.decode(msgtype, msg)

JSON Encoder

This class implements the Encoder interface for the JSON format. It leverages the intermediary dictionary representation.

The JSONEncoder can be used to create an OpenC2 stack in Consumer and Producer.

encoder_type = 'json'

The label that is used to identify this Encoder in OpenC2 messages.

@staticmethod
def encode(obj):
23	@staticmethod
24	def encode(obj):
25		""" Encode an OpenC2 object
26
27			This method is used to encode an openc2lib object, which usually is a `Command` or `Message`. 
28			The implementation leverages the intermediary dictionary representation and it is
29			therefore agnostic of openc2lib clases.
30
31			:param obj: A valid openc2lib object.
32			:return: A string with the json representation of the `obj`.
33
34		"""
35		return json.dumps(Encoder.todict(obj))

Encode an OpenC2 object

This method is used to encode an openc2lib object, which usually is a Command or Message. The implementation leverages the intermediary dictionary representation and it is therefore agnostic of openc2lib clases.

Parameters
  • obj: A valid openc2lib object.
Returns

A string with the json representation of the obj.

@staticmethod
def decode(msg, msgtype=None):
37	@staticmethod
38	def decode(msg, msgtype=None):
39		""" Decode an OpenC2 message
40
41			This method is used to create an openc2lib object of type `msgtype` from a json record. 
42			The openc2lib class `msgtype` corresponding to the json record `msg` must be explicitly provided,
43			since parsing and automatically inferring the `msgtype` is not currently implemented.
44
45			The implementation leverages the intermediary dictionary representation and it is
46			therefore agnostic of openc2lib classes.
47
48			:param msg: The json record to decode.
49			:param msgtype: The openc2lib class to convert the json to.
50			:return: An `msgtype` class initialized according to the json content.
51		"""
52		if msgtype == None:
53			return json.loads(msg)
54
55		if isinstance(msg, str):
56			return Encoder.decode(msgtype, json.loads(msg))
57		else:
58			return Encoder.decode(msgtype, msg)

Decode an OpenC2 message

This method is used to create an openc2lib object of type msgtype from a json record. The openc2lib class msgtype corresponding to the json record msg must be explicitly provided, since parsing and automatically inferring the msgtype is not currently implemented.

The implementation leverages the intermediary dictionary representation and it is therefore agnostic of openc2lib classes.

Parameters
  • msg: The json record to decode.
  • msgtype: The openc2lib class to convert the json to.
Returns

An msgtype class initialized according to the json content.