# Path: utils.py
# class ProtocolViolation(Exception):
#     """Indicates that the communication protocol between this and a remote
#     client has been violated. In this case we abort from the communication."""
#     def __init__(self, reason="unknown"):
#         self.reason = reason
#     def __repr__(self):
#         return "ProtocolViolation(%s)"%self.reason
# 
# def constructor(func):
#     @classmethod
#     def f(cls, *args, **kwargs):
#         self = cls.__new__(cls)
#         func(self, *args, **kwargs)
#         return self
#     return f


import binascii
from utils import ProtocolViolation, constructor


class Bytes():
    @staticmethod
    def tojson(obj):
        return binascii.hexlify(obj).decode("ascii")
    @staticmethod
    def fromjson(json):
        return binascii.unhexlify(json.encode("ascii"))

class Hash():
    @staticmethod
    def tojson(obj):
        return obj[::-1].encode("hex")
    @staticmethod
    def fromjson(json):
        return json.decode("hex")[::-1]

class Entity(object):
    def tojson(self):
        retval = {}
        for (field, type) in self.fields.items():
            retval[field] = type.tojson(self.__getattribute__(field))
        return retval

# Path: utils.py
# class ProtocolViolation(Exception):
#     """Indicates that the communication protocol between this and a remote
#     client has been violated. In this case we abort from the communication."""
#     def __init__(self, reason="unknown"):
#         self.reason = reason
#     def __repr__(self):
#         return "ProtocolViolation(%s)"%self.reason
# 
# def constructor(func):
#     @classmethod
#     def f(cls, *args, **kwargs):
#         self = cls.__new__(cls)
#         func(self, *args, **kwargs)
#         return self
#     return f


# Path: jserialize.py
import binascii
from utils import ProtocolViolation, constructor



class Bytes():
    @staticmethod
    def tojson(obj):
        return binascii.hexlify(obj).decode("ascii")
    @staticmethod
    def fromjson(json):
        return binascii.unhexlify(json.encode("ascii"))

class Hash():
    @staticmethod
    def tojson(obj):
        return obj[::-1].encode("hex")
    @staticmethod
    def fromjson(json):
        return json.decode("hex")[::-1]

class Entity(object):
    def tojson(self):
        retval = {}
        for (field, type) in self.fields.items():
            retval[field] = type.tojson(self.__getattribute__(field))
        return retval

    @constructor