lib/json: object __id is optional
authorAlexis Laferrière <alexis.laf@xymus.net>
Mon, 29 Jun 2015 00:47:17 +0000 (20:47 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 29 Jun 2015 02:47:04 +0000 (22:47 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/json/serialization.nit

index 359acf5..b6d1773 100644 (file)
@@ -219,15 +219,25 @@ class JsonDeserializer
 
                        # obj?
                        if kind == "obj" then
-                               assert object.keys.has("__id")
-                               var id = object["__id"]
-                               assert id isa Int
+                               var id = null
+                               if object.keys.has("__id") then
+                                       id = object["__id"]
+
+                                       if not id isa Int then
+                                               errors.add new Error("Serialization Error: JSON object declaration declares a non-integer `__id`.")
+                                               return object
+                                       end
+
+                                       if cache.has_id(id) then
+                                               errors.add new Error("Serialization Error: JSON object with `__id` {id} is deserialized twice.")
+                                               # Keep going
+                                       end
+                               end
 
                                assert object.keys.has("__class")
                                var class_name = object["__class"]
                                assert class_name isa String
 
-                               assert not cache.has_id(id) else print "Error: Object with id '{id}' of {class_name} is deserialized twice."
 
                                # advance on path
                                path.push object