frontend serialization: don't create `core_serialize_to` if it exists
[nit.git] / src / frontend / serialization_phase.nit
index c054558..4549144 100644 (file)
@@ -181,6 +181,16 @@ private class SerializationPhasePreModel
        do
                var npropdefs = nclassdef.n_propdefs
 
+               # Do not insert a `core_serialize_to` if it already exists
+               for npropdef in npropdefs do
+                       if npropdef isa AMethPropdef then
+                               var methid = npropdef.n_methid
+                               if methid != null and methid.collect_text == "core_serialize_to" then
+                                       return
+                               end
+                       end
+               end
+
                var code = new Array[String]
                code.add "redef fun core_serialize_to(v)"
                code.add "do"
@@ -320,6 +330,9 @@ do
                                # What to do when an attribute is missing?
                                if attribute.has_value then
                                        # Leave it to the default value
+                               else if mtype isa MNullableType then
+                                       code.add """
+               self.{{{name}}} = null"""
                                else code.add """
                v.errors.add new Error("Deserialization Error: attribute `{class_name}::{{{name}}}` missing from JSON object")"""