import numbers
import json
import base64


def encode(d):
    """Encode an object in a way that can be transported via Mesos attributes: first to
    JSON, then to base64url. The JSON string is padded with spaces so that the base64
    string has no = pad characters, which are outside the legal set for Mesos.
    """
    if isinstance(d, numbers.Real) and not isinstance(d, bool):
        return repr(float(d))
    else:
        s = json.dumps(d, sort_keys=True)
        while len(s) % 3:
            s += ' '
        return base64.urlsafe_b64encode(s.encode('utf-8')).decode('ascii')