# Extends Serializer and adds specific business behaviors when dealing with business objects.
#
# As with standard Nit, additional level of customization can be achieved by adding more double-dispatching :)
# We can thus choose to locate the specific behavior in the serializer, or the serializees.
class RestrictedSerializer
super Serializer
# This method is called to generate the attributes of a serialized representation
redef fun serialize_core(value)
do
super
if value isa E then
# Inject additional special domain-specific information
serialize_attribute("more-data", value.phantom)
end
end
# This method is called when trying to serialize a specific attribute
redef fun serialize_attribute(name, value)
do
var recv = current_object
if recv isa E then
# do not serialize `E::semi_private`
if name == "semi_private" then return
end
if value isa E then
# Do not serialize references to `E`.
# Just use a domain-specific value that make sense in the business logic.
serialize_attribute(name, "ID:" + value.id)
return
end
super
end
end
lib/serialization/examples/custom_serialization.nit:86,1--122,3