import json


def dumps(obj):
    """
    Serialise a DisCoPy object as JSON.

    >>> from pprint import PrettyPrinter
    >>> pprint = PrettyPrinter(indent=4, width=60).pprint
    >>> from discopy.cat import Box, Ob
    >>> f = Box('f', Ob('x'), Ob('y'), data=[42, {'Alice': 1}])
    >>> d = f >> f[::-1]
    >>> assert loads(dumps(d)) == d
    >>> pprint(json.loads(dumps(d)))
    {   'boxes': [   {   'cod': {   'factory': 'discopy.cat.Ob',
                                    'name': 'y'},
                         'data': [42, {'Alice': 1}],
                         'dom': {   'factory': 'discopy.cat.Ob',
                                    'name': 'x'},
                         'factory': 'discopy.cat.Box',
                         'name': 'f'},
                     {   'cod': {   'factory': 'discopy.cat.Ob',
                                    'name': 'x'},
                         'data': [42, {'Alice': 1}],
                         'dom': {   'factory': 'discopy.cat.Ob',
                                    'name': 'y'},
                         'factory': 'discopy.cat.Box',
                         'is_dagger': True,
                         'name': 'f'}],
        'cod': {'factory': 'discopy.cat.Ob', 'name': 'x'},
        'dom': {'factory': 'discopy.cat.Ob', 'name': 'x'},
        'factory': 'discopy.cat.Arrow'}
    """
    return json.dumps(obj.to_tree())