Custom FieldsΒΆ

The most common way to create a custom field with serpy is to override serpy.Field.to_value(). This method is called on the value retrieved from the object being serialized. For example, to create a field that adds 5 to every value it serializes, do:

class Add5Field(serpy.Field):
   def to_value(self, value):
      return value + 5

Then to use it:

class Obj(object):
   pass

class ObjSerializer(serpy.Serializer):
   foo = Add5Field()

f = Obj()
f.foo = 9
ObjSerializer(f).data
# {'foo': 14}

Another use for custom fields is data validation. For example, to validate that every serialized value has a '.' in it:

class ValidateDotField(serpy.Field):
   def to_value(self, value):
      if '.' not in value:
         raise ValidationError('no dot!')
      return value

For more control over the behavior of the field, override serpy.Field.as_getter(). When the serpy.Serializer class is created, each field will be compiled to a getter, that will be called to get its associated attribute from the object. For an example of this, see the serpy.MethodField() implementation.