From 398b34439a48a2dff84199d9e190c3303fbb6151 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 29 Aug 2016 22:32:44 -0400 Subject: [PATCH] serialization: don't throw missing attribute error if there is a default value MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/json/serialization.nit | 2 +- src/frontend/serialization_phase.nit | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/json/serialization.nit b/lib/json/serialization.nit index 8a80240..ac29a62 100644 --- a/lib/json/serialization.nit +++ b/lib/json/serialization.nit @@ -302,7 +302,7 @@ class JsonDeserializer var current = path.last if not current.keys.has(name) then - errors.add new Error("Deserialization Error: JSON object has not attribute '{name}'.") + # Let the generated code / caller of `deserialize_attribute` raise the missing attribute error deserialize_attribute_missing = true return null end diff --git a/src/frontend/serialization_phase.nit b/src/frontend/serialization_phase.nit index 7fc1c31..c054558 100644 --- a/src/frontend/serialization_phase.nit +++ b/src/frontend/serialization_phase.nit @@ -312,9 +312,18 @@ do code.add """ self.{{{name}}} = v.deserialize_attribute("{{{attribute.serialize_name}}}", "{{{type_name}}}") """ - else code.add """ + else + code.add """ var {{{name}}} = v.deserialize_attribute("{{{attribute.serialize_name}}}", "{{{type_name}}}") if v.deserialize_attribute_missing then +""" + # What to do when an attribute is missing? + if attribute.has_value then + # Leave it to the default value + else code.add """ + v.errors.add new Error("Deserialization Error: attribute `{class_name}::{{{name}}}` missing from JSON object")""" + + code.add """ else if not {{{name}}} isa {{{type_name}}} then v.errors.add new AttributeTypeError(self, "{{{attribute.serialize_name}}}", {{{name}}}, "{{{type_name}}}") if v.keep_going == false then return @@ -322,6 +331,7 @@ do self.{{{name}}} = {{{name}}} end """ + end end code.add "end" -- 1.7.9.5