serialization: add hooks for a custom serializer
[nit.git] / lib / serialization / serialization.nit
index f94445e..b0d5d6c 100644 (file)
@@ -57,6 +57,11 @@ interface Serializer
        # use double dispatch to customize the bahavior per serializable objects.
        fun serialize(object: nullable Serializable) is abstract
 
+       # The object currently serialized by `serialized`
+       #
+       # Can be used by a custom serializer to add domain-specific serialization behavior.
+       protected fun current_object: nullable Object is abstract
+
        # Serialize an object, with full serialization or a simple reference
        protected fun serialize_reference(object: Serializable) is abstract
 
@@ -81,6 +86,15 @@ interface Serializer
                return true
        end
 
+       # The method is called when a standard `value` is serialized
+       #
+       # The default behavior is to call `value.core_serialize_to(self)` but it
+       # can be redefined by a custom serializer to add domain-specific serialization behavior.
+       fun serialize_core(value: Serializable)
+       do
+               value.core_serialize_to(self)
+       end
+
        # Warn of problems and potential errors (such as if an attribute
        # is not serializable)
        fun warn(msg: String) do print "Serialization warning: {msg}"