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(prefix) then
- class_name = static_type.substring_from(prefix.length)
- else class_name = static_type
+ class_name = static_type.strip_nullable
end
end
end
if static_type != null and check_subtypes then
- var static_class = static_type.strip_nullable_and_params
- var dynamic_class = class_name.strip_nullable_and_params
+ var static_class = static_type.strip_nullable_and_params.to_s
+ var dynamic_class = class_name.strip_nullable_and_params.to_s
if not class_inheritance_metamodel.has_edge(dynamic_class, static_class) then
errors.add new Error("Deserialization Error: `{class_name}` is not a subtype of the static type `{static_type}`")
return null
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
end
return res
end
-
- # Strip the `nullable` prefix and the params from the class name `self`
- #
- # ~~~nitish
- # assert "String".strip_nullable_and_params == "String"
- # assert "Array[Int]".strip_nullable_and_params == "Array"
- # assert "Map[Set[String], Set[Int]]".strip_nullable_and_params == "Map"
- # ~~~
- private fun strip_nullable_and_params: String
- do
- var class_name = to_s
-
- var prefix = "nullable "
- if class_name.has_prefix(prefix) then class_name = class_name.substring_from(prefix.length)
-
- var bracket_index = class_name.index_of('[')
- if bracket_index == -1 then return class_name
- return class_name.substring(0, bracket_index)
- end
end
redef class SimpleCollection[E]
return c
end
end
+
+redef class Text
+
+ # Strip the `nullable` prefix from the type name `self`
+ #
+ # ~~~
+ # assert "String".strip_nullable == "String"
+ # assert "nullable Array[Int]".strip_nullable == "Array[Int]"
+ # assert "Map[Set[String], Set[Int]]".strip_nullable == "Map[Set[String], Set[Int]]"
+ # ~~~
+ fun strip_nullable: Text
+ do
+ var prefix = "nullable "
+ return if has_prefix(prefix) then substring_from(prefix.length) else self
+ end
+
+ # Strip the `nullable` prefix and the params from the type name `self`
+ #
+ # ~~~
+ # assert "String".strip_nullable_and_params == "String"
+ # assert "nullable Array[Int]".strip_nullable_and_params == "Array"
+ # assert "Map[Set[String], Set[Int]]".strip_nullable_and_params == "Map"
+ # ~~~
+ fun strip_nullable_and_params: Text
+ do
+ var class_name = strip_nullable
+
+ var bracket_index = class_name.index_of('[')
+ if bracket_index == -1 then return class_name
+ return class_name.substring(0, bracket_index)
+ end
+end
# This method should be redefined for each custom subclass of `Serializable`.
# All refinement should look for a precise `class_name` and call super
# on unsupported classes.
- protected fun deserialize_class(class_name: String): nullable Object do
+ protected fun deserialize_class(class_name: Text): nullable Object do
if class_name == "Error" then return new Error.from_deserializer(self)
return deserialize_class_intern(class_name)
end
# Refinements to this method will be generated by the serialization phase.
# To avoid conflicts, there should not be any other refinements to this method.
# You can instead use `deserialize_class`.
- protected fun deserialize_class_intern(class_name: String): nullable Object do
+ protected fun deserialize_class_intern(class_name: Text): nullable Object do
errors.add new Error("Deserialization Error: Doesn't know how to deserialize class \"{class_name}\"")
return null
end