serialization :: E :: defaultinit
# The root class of the business objects.
# This factorizes most services and domain knowledge used by the `RestrictedSerializer`
#
# In real enterprise-level code, the various specific behaviors can be specified in more semantic classifiers.
abstract class E
serialize
# The semantic business identifier.
#
# With the `RestrictedSerializer`, references to `E` objects will be replaced with `id`-based information.
# This avoid to duplicate or enlarge the information cross-call wise.
#
# A future API/REST call can then request the _missing_ object from its identifier.
var id: String
# A phantom attribute to be serialized by the custom `RestrictedSerializer`.
#
# This can be used to inject constant or computed information that make little sense to have as a genuine attribute in
# the Nit model.
fun phantom: String do return "So Much Fun"
# An attribute not to be serialized by the custom `RestrictedSerializer`.
# e.g. we want it on the DB but not in API/REST JSON messages
#
# Note that the annotation `noserialize` hides the attribute for all serializers.
# To hide the attribute only in the `RestrictedSerializer`, it will have to actively ignore it.
var semi_private = "secret"
# Test method that serializes `self` and prints with the standard JsonSerializer
fun ser_json
do
var w = new StringWriter
var js = new JsonSerializer(w)
js.plain_json = true
js.serialize(self)
print w
end
# Test method that serializes `self` and prints with the custom RestrictedJsonSerializer.
fun ser_json2
do
var w = new StringWriter
var js = new RestrictedJsonSerializer(w)
js.plain_json = true
js.serialize(self)
print w
end
end
lib/serialization/examples/custom_serialization.nit:37,1--84,3