json: support reading plain JSON map when the static type is known
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 15 Oct 2016 02:38:55 +0000 (22:38 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 2 Feb 2017 21:33:36 +0000 (16:33 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/json/serialization_read.nit

index 10d5436..a9ebcfd 100644 (file)
@@ -488,6 +488,26 @@ redef class Map[K, V]
                        var keys = v.path.last.get_or_null("__keys")
                        var values = v.path.last.get_or_null("__values")
 
+                       if keys == null and values == null then
+                               # Fallback to a plain object
+                               for key, value_src in v.path.last do
+                                       var value = v.convert_object(value_src)
+
+                                       if not key isa K then
+                                               v.errors.add new AttributeTypeError(self, "keys", key, "K")
+                                               continue
+                                       end
+
+                                       if not value isa V then
+                                               v.errors.add new AttributeTypeError(self, "values", value, "V")
+                                               continue
+                                       end
+
+                                       self[key] = value
+                               end
+                               return
+                       end
+
                        # Length is optional
                        if length == null and keys isa SequenceRead[nullable Object] then length = keys.length