def serialize(obj, fields=None, invalid=ABORT):
    """
    Serialize a GitModel object to JSON.

    fields:  When None, serilizes all fields. Otherwise, only the given fields
             will be returned in the serialized output.

    invalid: If a field cannot be coerced into its respective data type, a
             ValidationError will be raised. When invalid is ABORT, this
             exception is re-raised. SET_EMPTY causes the value to be set to an
             empty value. IGNORE simply uses the current value. Note that
             serialization may still fail with IGNORE if a value is not
             serializable.
    """
    pyobj = OrderedDict({
        'model': obj._meta.model_name,
        'fields': {}
    })
    for field in obj._meta.fields:
        if fields is None or field.name in fields:
            if field.serializable:
                try:
                    value = field.serialize(obj)
                except __HOLE__:
                    if invalid == SET_EMPTY:
                        value = field.empty_value
                    elif invalid == IGNORE:
                        value = getattr(obj, field.name)
                    else:
                        raise
                pyobj['fields'][field.name] = value

    return pyobj
dataset/ETHPy150Open bendavis78/python-gitmodel/gitmodel/serializers/python.py/serialize