# Buggy code
def ParseRecord(self, parser_mediator, key, structure):
    """Parses a structure of tokens derived from a line of a text file.

    Args:
      parser_mediator (ParserMediator): parser mediator.
      key (str): identifier of the structure of tokens.
      structure (pyparsing.ParseResults): structure of tokens derived from
          a line of a text file.

    Raises:
      ParseError: when the structure type is unknown.
    """
    if key != "line":
        raise errors.ParseError(
            "Unable to parse record, unknown structure: {0:s}".format(key)
        )

    msg_value = structure.get("msg")
    if not msg_value:
        parser_mediator.ProduceExtractionError(
            "missing msg value: {0!s}".format(structure)
        )
        return

    try:
        seconds = int(msg_value[0], 10)
    except ValueError:
        parser_mediator.ProduceExtractionError(
            "unsupported number of seconds in msg value: {0!s}".format(structure)
        )
        return

    try:
        milliseconds = int(msg_value[1], 10)
    except ValueError:
        parser_mediator.ProduceExtractionError(
            "unsupported number of milliseconds in msg value: {0!s}".format(structure)
        )
        return

    timestamp = ((seconds * 1000) + milliseconds) * 1000
    body_text = structure[2][0]

    try:
        # Try to parse the body text as key value pairs. Note that not
        # all log lines will be properly formatted key value pairs.
        key_value_dict = self._SELINUX_KEY_VALUE_DICT.parseString(body_text)
    except pyparsing.ParseException:
        key_value_dict = {}

    attributes = {
        "audit_type": structure.get("type"),
        "body": body_text,
        "pid": key_value_dict.get("pid"),
    }

    event_object = SELinuxLineEvent(timestamp, 0, attributes)
    parser_mediator.ProduceEvent(event_object)


# Traceback (TypeError)
2016-07-21 12:52:47,488 [WARNING] (Worker_01 ) PID:8090 <worker_process> Unhandled exception while processing path spec: OS:/home/onager/code/plaso_onager/test_data/selinux.log.
2016-07-21 12:52:47,489 [ERROR] (Worker_01 ) PID:8090 <worker_process> ([u'UNKNOWN[', u'1323', u']'], {u'key': [(u'type', 0)], u'value': [(u'UNKNOWN[', 0)]}) is not JSON serializable
Traceback (most recent call last):
File "/home/onager/code/plaso_onager/plaso/multi_processing/worker_process.py", line 245, in _ProcessPathSpec
extraction_worker.ProcessPathSpec(parser_mediator, path_spec)
File "/home/onager/code/plaso_onager/plaso/engine/worker.py", line 679, in ProcessPathSpec
self._ProcessFileEntry(parser_mediator, file_entry)
File "/home/onager/code/plaso_onager/plaso/engine/worker.py", line 540, in _ProcessFileEntry
parser_mediator, file_entry, data_stream.name)
File "/home/onager/code/plaso_onager/plaso/engine/worker.py", line 629, in _ProcessFileEntryDataStream
parser_mediator, file_entry, data_stream_name)
File "/home/onager/code/plaso_onager/plaso/engine/extractors.py", line 248, in ParseDataStream
parser_mediator, parser, file_entry, file_object=file_object)
File "/home/onager/code/plaso_onager/plaso/engine/extractors.py", line 185, in _ParseFileEntryWithParser
parser.Parse(parser_mediator, file_object)
File "/home/onager/code/plaso_onager/plaso/parsers/interface.py", line 269, in Parse
self.ParseFileObject(parser_mediator, file_object, **kwargs)
File "/home/onager/code/plaso_onager/plaso/parsers/text_parser.py", line 477, in ParseFileObject
parser_mediator, use_key, parsed_structure)
File "/home/onager/code/plaso_onager/plaso/parsers/selinux.py", line 151, in ParseRecord
parser_mediator.ProduceEvent(event_object)
File "/home/onager/code/plaso_onager/plaso/parsers/mediator.py", line 488, in ProduceEvent
self._storage_writer.AddEvent(event_object)
File "/home/onager/code/plaso_onager/plaso/storage/zip_file.py", line 2903, in AddEvent
self._storage_file.AddEvent(event)
File "/home/onager/code/plaso_onager/plaso/storage/gzip_file.py", line 110, in AddEvent
self._WriteAttributeContainer(event)
File "/home/onager/code/plaso_onager/plaso/storage/gzip_file.py", line 84, in _WriteAttributeContainer
attribute_container)
File "/home/onager/code/plaso_onager/plaso/storage/interface.py", line 237, in _SerializeAttributeContainer
attribute_container)
File "/home/onager/code/plaso_onager/plaso/serializer/json_serializer.py", line 421, in WriteSerialized
return json.dumps(json_dict)
File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ([u'UNKNOWN[', u'1323', u']'], {u'key': [(u'type', 0)], u'value': [(u'UNKNOWN[', 0)]}) is not JSON serializable

# Fixed code
def ParseRecord(self, parser_mediator, key, structure):
    """Parses a structure of tokens derived from a line of a text file.

    Args:
      parser_mediator (ParserMediator): parser mediator.
      key (str): identifier of the structure of tokens.
      structure (pyparsing.ParseResults): structure of tokens derived from
          a line of a text file.

    Raises:
      ParseError: when the structure type is unknown.
    """
    if key != "line":
        raise errors.ParseError(
            "Unable to parse record, unknown structure: {0:s}".format(key)
        )

    msg_value = structure.get("msg")
    if not msg_value:
        parser_mediator.ProduceExtractionError(
            "missing msg value: {0!s}".format(structure)
        )
        return

    try:
        seconds = int(msg_value[0], 10)
    except ValueError:
        parser_mediator.ProduceExtractionError(
            "unsupported number of seconds in msg value: {0!s}".format(structure)
        )
        return

    try:
        milliseconds = int(msg_value[1], 10)
    except ValueError:
        parser_mediator.ProduceExtractionError(
            "unsupported number of milliseconds in msg value: {0!s}".format(structure)
        )
        return

    timestamp = ((seconds * 1000) + milliseconds) * 1000
    body_text = structure[2][0]

    try:
        # Try to parse the body text as key value pairs. Note that not
        # all log lines will be properly formatted key value pairs.
        key_value_dict = self._SELINUX_KEY_VALUE_DICT.parseString(body_text)
    except pyparsing.ParseException:
        key_value_dict = {}

    audit_type = structure.get("type")
    pid = key_value_dict.get("pid")

    event_object = SELinuxLineEvent(timestamp, 0, audit_type, pid, body_text)
    parser_mediator.ProduceEvent(event_object)