Used in BaseSerializer and its sub-classes to flatten referenceable
    values. Hide the reference handling from sub-classes.
    For example, to make strings referenceable in a sub-class only use
    this decorator with decorate flatten_str().

def referenceable(method):
    """Used in BaseSerializer and its sub-classes to flatten referenceable
    values. Hide the reference handling from sub-classes.
    For example, to make strings referenceable in a sub-class only use
    this decorator with decorate flatten_str()."""

    def wrapper(self, value, *args):
        deref = self._prepare(value)
        if deref is not None:
            return deref
        packer, data = method(self, value, *args)
        return self._preserve(value, packer, data)

    return wrapper