Merge: examples/fibonacci: fix off-by-one fibonacci
[nit.git] / lib / serialization / serialization.nit
index b0dc728..8ae8d2f 100644 (file)
@@ -46,6 +46,7 @@ module serialization is
        new_annotation auto_serializable
        new_annotation serialize
        new_annotation noserialize
+       new_annotation serialize_as
 end
 
 # Abstract serialization service to be sub-classed by specialized services.
@@ -106,7 +107,8 @@ abstract class Deserializer
        # This method should be redefined for each custom subclass of `Serializable`.
        # All refinement should look for a precise `class_name` and call super
        # on unsupported classes.
-       protected fun deserialize_class(class_name: String): Object do
+       protected fun deserialize_class(class_name: String): nullable Object do
+               if class_name == "Error" then return new Error.from_deserializer(self)
                return deserialize_class_intern(class_name)
        end
 
@@ -115,9 +117,9 @@ abstract class Deserializer
        # Refinements to this method will be generated by the serialization phase.
        # To avoid conflicts, there should not be any other refinements to this method.
        # You can instead use `deserialize_class`.
-       protected fun deserialize_class_intern(class_name: String): Object do
-               print "Error: doesn't know how to deserialize class \"{class_name}\""
-               abort
+       protected fun deserialize_class_intern(class_name: String): nullable Object do
+               errors.add new Error("Deserialization Error: Doesn't know how to deserialize class \"{class_name}\"")
+               return null
        end
 
        # Should `self` keep trying to deserialize an object after an error?
@@ -131,7 +133,7 @@ abstract class Deserializer
        # When at `true`, this may cause the accumulation of a lot of entries in `errors`.
        #
        # Default at `true`.
-       var keep_going: nullable Bool is writable
+       var keep_going: nullable Bool = null is writable
 
        # Errors encountered in the last call to `deserialize`
        var errors = new Array[Error]
@@ -191,7 +193,7 @@ interface Serializable
        # Create an instance of this class from the `deserializer`
        #
        # This constructor is refined by subclasses to correctly build their instances.
-       init from_deserializer(deserializer: Deserializer) do end
+       init from_deserializer(deserializer: Deserializer) is nosuper do end
 end
 
 redef interface Object
@@ -220,7 +222,7 @@ redef class Char super DirectSerializable end
 redef class Int super DirectSerializable end
 redef class Float super DirectSerializable end
 redef class NativeString super DirectSerializable end
-redef class String super DirectSerializable end
+redef class Text super DirectSerializable end
 redef class SimpleCollection[E] super Serializable end
 redef class Map[K, V] super Serializable end
 
@@ -242,7 +244,7 @@ redef class Couple[F, S]
        end
 end
 
-redef class Container[E]
+redef class Ref[E]
        super Serializable
 
        redef init from_deserializer(v)
@@ -257,3 +259,25 @@ redef class Container[E]
                v.serialize_attribute("item", first)
        end
 end
+
+redef class Error
+       super Serializable
+
+       redef init from_deserializer(v)
+       do
+               v.notify_of_creation self
+
+               var message = v.deserialize_attribute("message")
+               if not message isa String then message = ""
+               init message
+
+               var cause = v.deserialize_attribute("cause")
+               if cause isa nullable Error then self.cause = cause
+       end
+
+       redef fun core_serialize_to(v)
+       do
+               v.serialize_attribute("message", message)
+               v.serialize_attribute("cause", cause)
+       end
+end