import json
import base64


def _serialize_state(state):
    """Serialize OAuth state to a base64 string after passing through JSON"""
    json_state = json.dumps(state)
    return base64.urlsafe_b64encode(
        json_state.encode('utf8')
    ).decode('ascii')