# Buggy code
def get_as_dict(self):
    """
    This returns the token data as a dictionary.
    It is used to display the token list at /token/list.

    The certificate token can add the PKCS12 file if it exists

    :return: The token data as dict
    :rtype: dict
    """
    # first get the database values as dict
    token_dict = self.token.get()

    if "privatekey" in token_dict.get("info"):
        token_dict["info"]["pkcs12"] = base64.b64encode(self._create_pkcs12_bin())
        # del(token_dict["privatekey"])

    return token_dict


# Traceback (TypeError)
[2019-08-19 16:06:52,849] ERROR in app: Exception on /token/init [POST]
Traceback (most recent call last):
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/lib/prepolicy.py", line 129, in policy_wrapper
return wrapped_function(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/lib/prepolicy.py", line 129, in policy_wrapper
return wrapped_function(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/lib/prepolicy.py", line 129, in policy_wrapper
return wrapped_function(*args, **kwds)
[Previous line repeated 15 more times]
File "/home/fred/privacyidea/privacyidea/privacyidea/api/lib/postpolicy.py", line 102, in policy_wrapper
response = wrapped_function(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/lib/subscriptions.py", line 333, in check_subscription_wrapper
f_result = func(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/lib/event.py", line 99, in event_wrapper
f_result = func(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/lib/log.py", line 154, in log_wrapper
return func(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/token.py", line 291, in init
return send_result(True, details=response_details)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/lib/utils.py", line 104, in send_result
return jsonify(prepare_result(obj, rid, details))
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/json/__init__.py", line 321, in jsonify
dumps(data, indent=indent, separators=separators) + '\n',
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/json/__init__.py", line 179, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/lib64/python3.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib64/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/fred/privacyidea/privacyidea/venv3/lib/python3.7/site-packages/flask/json/__init__.py", line 81, in default
return _json.JSONEncoder.default(self, o)
File "/usr/lib64/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

# Fixed code
def get_as_dict(self):
    """
    This returns the token data as a dictionary.
    It is used to display the token list at /token/list.

    The certificate token can add the PKCS12 file if it exists

    :return: The token data as dict
    :rtype: dict
    """
    # first get the database values as dict
    token_dict = self.token.get()

    if "privatekey" in token_dict.get("info"):
        token_dict["info"]["pkcs12"] = b64encode_and_unicode(self._create_pkcs12_bin())

    return token_dict