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.
serialization $ RestrictedSerializer :: SELF
Type of this instance, automatically specialized in every classserialization $ RestrictedSerializer :: serialize_attribute
This method is called when trying to serialize a specific attributeserialization $ RestrictedSerializer :: serialize_core
This method is called to generate the attributes of a serialized representationcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializer :: current_object
The object currently serialized byserialized
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializer :: serialize
Entry point method of this service, serialize theobject
serialization :: Serializer :: serialize_attribute
Serialize an attribute to compose a serializable objectserialization :: Serializer :: serialize_core
The method is called when a standardvalue
is serialized
serialization :: Serializer :: serialize_reference
Serialize an object, with full serialization or a simple referenceserialization :: Serializer :: try_to_serialize
Serializevalue
is possible, i.e. it is Serializable
or null
serialization :: Serializer :: warn
Warn of problems and potential errors (such as if an attributeserialization :: RestrictedJsonSerializer
Extends JsonSerializer and adds specific business behaviors when dealing with business objects.
# 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