realtime: avoid unnecessary mallocs
[nit.git] / lib / json / serialization_read.nit
index e29f5bd..8dd73f7 100644 (file)
 # Services to read JSON: `from_json_string` and `JsonDeserializer`
 module serialization_read
 
-import ::serialization::caching
-private import ::serialization::engine_tools
+import serialization::caching
+private import serialization::engine_tools
+import serialization::safe
+
 private import static
-private import string_parser
+import poset
 
 # Deserializer from a Json string.
 class JsonDeserializer
        super CachingDeserializer
+       super SafeDeserializer
 
        # Json text to deserialize from.
        private var text: Text
@@ -102,18 +105,18 @@ class JsonDeserializer
                        # ref?
                        if kind == "ref" then
                                if not object.keys.has("__id") then
-                                       errors.add new Error("Serialization Error: JSON object reference does not declare a `__id`.")
+                                       errors.add new Error("Deserialization Error: JSON object reference does not declare a `__id`.")
                                        return object
                                end
 
                                var id = object["__id"]
                                if not id isa Int then
-                                       errors.add new Error("Serialization Error: JSON object reference declares a non-integer `__id`.")
+                                       errors.add new Error("Deserialization Error: JSON object reference declares a non-integer `__id`.")
                                        return object
                                end
 
                                if not cache.has_id(id) then
-                                       errors.add new Error("Serialization Error: JSON object reference has an unknown `__id`.")
+                                       errors.add new Error("Deserialization Error: JSON object reference has an unknown `__id`.")
                                        return object
                                end
 
@@ -127,12 +130,12 @@ class JsonDeserializer
                                        id = object["__id"]
 
                                        if not id isa Int then
-                                               errors.add new Error("Serialization Error: JSON object declaration declares a non-integer `__id`.")
+                                               errors.add new Error("Deserialization 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.")
+                                               errors.add new Error("Deserialization Error: JSON object with `__id` {id} is deserialized twice.")
                                                # Keep going
                                        end
                                end
@@ -144,23 +147,22 @@ class JsonDeserializer
 
                                        if class_name == null and static_type != null then
                                                # Fallack to the static type, strip the `nullable` prefix
-                                               var prefix = "nullable "
-                                               if static_type.has(prefix) then
-                                                       class_name = static_type.substring_from(prefix.length)
-                                               else class_name = static_type
+                                               class_name = static_type.strip_nullable
                                        end
                                end
 
                                if class_name == null then
-                                       errors.add new Error("Serialization Error: JSON object declaration does not declare a `__class`.")
+                                       errors.add new Error("Deserialization Error: JSON object declaration does not declare a `__class`.")
                                        return object
                                end
 
                                if not class_name isa String then
-                                       errors.add new Error("Serialization Error: JSON object declaration declares a non-string `__class`.")
+                                       errors.add new Error("Deserialization Error: JSON object declaration declares a non-string `__class`.")
                                        return object
                                end
 
+                               if not accept(class_name, static_type) then return null
+
                                # advance on path
                                path.push object
 
@@ -177,21 +179,32 @@ class JsonDeserializer
                        # char?
                        if kind == "char" then
                                if not object.keys.has("__val") then
-                                       errors.add new Error("Serialization Error: JSON `char` object does not declare a `__val`.")
+                                       errors.add new Error("Deserialization Error: JSON `char` object does not declare a `__val`.")
                                        return object
                                end
 
                                var val = object["__val"]
 
                                if not val isa String or val.is_empty then
-                                       errors.add new Error("Serialization Error: JSON `char` object does not declare a single char in `__val`.")
+                                       errors.add new Error("Deserialization Error: JSON `char` object does not declare a single char in `__val`.")
                                        return object
                                end
 
                                return val.chars.first
                        end
 
-                       errors.add new Error("Serialization Error: JSON object has an unknown `__kind`.")
+                       # byte?
+                       if kind == "byte" then
+                               var val = object.get_or_null("__val")
+                               if not val isa Int then
+                                       errors.add new Error("Serialization Error: JSON `byte` object does not declare an integer `__val`.")
+                                       return object
+                               end
+
+                               return val.to_b
+                       end
+
+                       errors.add new Error("Deserialization Error: JSON object has an unknown `__kind`.")
                        return object
                end
 
@@ -199,13 +212,8 @@ class JsonDeserializer
                if object isa Array[nullable Object] then
                        # Can we use the static type?
                        if static_type != null then
-                               var prefix = "nullable "
-                               var class_name = if static_type.has(prefix) then
-                                               static_type.substring_from(prefix.length)
-                                       else static_type
-
                                opened_array = object
-                               var value = deserialize_class(class_name)
+                               var value = deserialize_class(static_type.strip_nullable)
                                opened_array = null
                                return value
                        end
@@ -265,16 +273,26 @@ class JsonDeserializer
                        return array
                end
 
+               if object isa String and object.length == 1 and static_type == "Char" then
+                       # Char serialized as a JSON string
+                       return object.chars.first
+               end
+
+               if object isa Int and static_type == "Byte" then
+                       # Byte serialized as an integer
+                       return object.to_b
+               end
+
                return object
        end
 
        # Current array open for deserialization, used by `SimpleCollection::from_deserializer`
        private var opened_array: nullable Array[nullable Object] = null
 
-       redef fun deserialize
+       redef fun deserialize(static_type)
        do
                errors.clear
-               return convert_object(root)
+               return convert_object(root, static_type)
        end
 
        # User customizable heuristic to infer the name of the Nit class to deserialize `json_object`
@@ -382,25 +400,15 @@ redef class SimpleCollection[E]
                                open_array = arr
                        end
 
-                       # Try to get the name of the single parameter type assuming it is E.
-                       # This does not work in non-generic subclasses,
-                       # when the first parameter is not E, or
-                       # when there is more than one parameter. (The last one could be fixed)
-                       var class_name = class_name
-                       var items_type = null
-                       var bracket_index = class_name.index_of('[')
-                       if bracket_index != -1 then
-                               var start = bracket_index + 1
-                               var ending = class_name.last_index_of(']')
-                               items_type = class_name.substring(start, ending-start)
-                       end
+                       # Name of the dynamic name of E
+                       var items_type_name = (new GetName[E]).to_s
 
                        # Fill array
                        for o in open_array do
-                               var obj = v.convert_object(o, items_type)
+                               var obj = v.convert_object(o, items_type_name)
                                if obj isa E then
                                        add obj
-                               else v.errors.add new AttributeTypeError(self, "items", obj, "E")
+                               else v.errors.add new AttributeTypeError(self, "items", obj, items_type_name)
                        end
                end
        end
@@ -415,10 +423,34 @@ redef class Map[K, V]
                        v.notify_of_creation self
                        init
 
+                       var keys_type_name = (new GetName[K]).to_s
+                       var values_type_name = (new GetName[V]).to_s
+
                        var length = v.deserialize_attribute("__length")
                        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, values_type_name)
+
+                                       if not key isa K then
+                                               v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
+                                               continue
+                                       end
+
+                                       if not value isa V then
+                                               v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
+                                               continue
+                                       end
+
+                                       self[key] = value
+                               end
+                               return
+                       end
+
                        # Length is optional
                        if length == null and keys isa SequenceRead[nullable Object] then length = keys.length
 
@@ -435,17 +467,26 @@ redef class Map[K, V]
                                return
                        end
 
+                       # First, convert all keys to follow the order of the serialization
+                       var converted_keys = new Array[K]
                        for i in length.times do
-                               var key = v.convert_object(keys[i])
-                               var value = v.convert_object(values[i])
+                               var key = v.convert_object(keys[i], keys_type_name)
 
                                if not key isa K then
-                                       v.errors.add new AttributeTypeError(self, "keys", key, "K")
+                                       v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
                                        continue
                                end
 
+                               converted_keys.add key
+                       end
+
+                       # Then convert the values and build the map
+                       for i in length.times do
+                               var key = converted_keys[i]
+                               var value = v.convert_object(values[i], values_type_name)
+
                                if not value isa V then
-                                       v.errors.add new AttributeTypeError(self, "values", value, "V")
+                                       v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
                                        continue
                                end
 
@@ -458,3 +499,29 @@ redef class Map[K, V]
                end
        end
 end
+
+# ---
+# Metamodel
+
+# Class inheritance graph as a `POSet[String]` serialized to JSON
+private fun class_inheritance_metamodel_json: CString is intern
+
+redef class Sys
+       redef var class_inheritance_metamodel is lazy do
+               var engine = new JsonDeserializer(class_inheritance_metamodel_json.to_s)
+               engine.check_subtypes = false
+               engine.whitelist.add_all(
+                       ["String", "POSet[String]", "POSetElement[String]",
+                        "HashSet[String]", "HashMap[String, POSetElement[String]]"])
+
+               var poset = engine.deserialize
+               if engine.errors.not_empty then
+                       print_error "Deserialization errors in class_inheritance_metamodel:"
+                       print_error engine.errors.join("\n* ")
+                       return new POSet[String]
+               end
+
+               if poset isa POSet[String] then return poset
+               return new POSet[String]
+       end
+end