import json


def snapshot_serializer(val, **kwargs):
    """Serializer function that returns a JSON string if snapshot=True"""
    snapshot = kwargs.get('snapshot', False)
    val = val.serialize(**kwargs)
    if snapshot:
        val = json.dumps(val)
    return val