import json

from .compat import OrderedDict
from .openpgp_message import OpenPGPMessage


def dump_stream(f, output_stream, indent=4):
    message = OpenPGPMessage.from_stream(f)
    return json.dump(message, output_stream, indent=indent,
                     cls=OpenPGPJsonEncoder)


class OpenPGPJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytearray):
            return self.serialize_bytes(obj)

        if getattr(obj, 'serialize', None):
            return obj.serialize()

        return super(OpenPGPJsonEncoder, self).default(obj)

    def encode(self, obj):
        # If a builtin type provides a `serialize` method, use that instead of
        # the default serialisation, eg. namedtuple

        if getattr(obj, 'serialize', None):
            obj = obj.serialize()

        return super(OpenPGPJsonEncoder, self).encode(obj)

    @staticmethod
    def serialize_bytes(some_bytes):
        return OrderedDict([
            ('octets', ':'.join(['{0:02x}'.format(byte)
                                 for byte in some_bytes])),
            ('length', len(some_bytes)),
        ])

import json

from .compat import OrderedDict
from .openpgp_message import OpenPGPMessage


def dump_stream(f, output_stream, indent=4):
    message = OpenPGPMessage.from_stream(f)
    return json.dump(message, output_stream, indent=indent,
                     cls=OpenPGPJsonEncoder)


class OpenPGPJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytearray):
            return self.serialize_bytes(obj)

        if getattr(obj, 'serialize', None):
            return obj.serialize()

        return super(OpenPGPJsonEncoder, self).default(obj)

    @staticmethod
    def serialize_bytes(some_bytes):
        return OrderedDict([
            ('octets', ':'.join(['{0:02x}'.format(byte)
                                 for byte in some_bytes])),
            ('length', len(some_bytes)),
        ])

Revert "Fix JSON encoding of `PacketLocation`"
Revert "Fix JSON encoding of `PacketLocation`"

This reverts commit 9e91912c6c1764c88890ec47df9372e6ac41612c.

  import json
  
  from .compat import OrderedDict
  from .openpgp_message import OpenPGPMessage
  
  
  def dump_stream(f, output_stream, indent=4):
      message = OpenPGPMessage.from_stream(f)
      return json.dump(message, output_stream, indent=indent,
                       cls=OpenPGPJsonEncoder)
  
  
  class OpenPGPJsonEncoder(json.JSONEncoder):
      def default(self, obj):
          if isinstance(obj, bytearray):
              return self.serialize_bytes(obj)
  
          if getattr(obj, 'serialize', None):
              return obj.serialize()
  
          return super(OpenPGPJsonEncoder, self).default(obj)
  
-     def encode(self, obj):
-         # If a builtin type provides a `serialize` method, use that instead of
-         # the default serialisation, eg. namedtuple
- 
-         if getattr(obj, 'serialize', None):
-             obj = obj.serialize()
- 
-         return super(OpenPGPJsonEncoder, self).encode(obj)
- 
      @staticmethod
      def serialize_bytes(some_bytes):
          return OrderedDict([
              ('octets', ':'.join(['{0:02x}'.format(byte)
                                   for byte in some_bytes])),
              ('length', len(some_bytes)),
          ])
  
Revert "Fix JSON encoding of `PacketLocation`"
## Code Before:
import json

from .compat import OrderedDict
from .openpgp_message import OpenPGPMessage


def dump_stream(f, output_stream, indent=4):
    message = OpenPGPMessage.from_stream(f)
    return json.dump(message, output_stream, indent=indent,
                     cls=OpenPGPJsonEncoder)


class OpenPGPJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytearray):
            return self.serialize_bytes(obj)

        if getattr(obj, 'serialize', None):
            return obj.serialize()

        return super(OpenPGPJsonEncoder, self).default(obj)

    def encode(self, obj):
        # If a builtin type provides a `serialize` method, use that instead of
        # the default serialisation, eg. namedtuple

        if getattr(obj, 'serialize', None):
            obj = obj.serialize()

        return super(OpenPGPJsonEncoder, self).encode(obj)

    @staticmethod
    def serialize_bytes(some_bytes):
        return OrderedDict([
            ('octets', ':'.join(['{0:02x}'.format(byte)
                                 for byte in some_bytes])),
            ('length', len(some_bytes)),
        ])

## Instruction:
Revert "Fix JSON encoding of `PacketLocation`"
## Code After:
import json

from .compat import OrderedDict
from .openpgp_message import OpenPGPMessage


def dump_stream(f, output_stream, indent=4):
    message = OpenPGPMessage.from_stream(f)
    return json.dump(message, output_stream, indent=indent,
                     cls=OpenPGPJsonEncoder)


class OpenPGPJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytearray):
            return self.serialize_bytes(obj)

        if getattr(obj, 'serialize', None):
            return obj.serialize()

        return super(OpenPGPJsonEncoder, self).default(obj)

    @staticmethod
    def serialize_bytes(some_bytes):
        return OrderedDict([
            ('octets', ':'.join(['{0:02x}'.format(byte)
                                 for byte in some_bytes])),
            ('length', len(some_bytes)),
        ])
