object
to a Nit object
# Convert the simple JSON `object` to a Nit object
private fun convert_object(object: nullable Object, static_type: nullable String): nullable Object
do
#print "convert_object {if object != null then object.class_name else "null"}"
if object isa Array[nullable Object] and object.length >= 1 then
# Serialized object?
var first = object.first
if first isa MsgPackExt then
if first.typ == ext_typ_obj then
# An array starts with a *ext*, it must be a serialized object
# New object declaration
var id = first.data.to_i
if cache.has_id(id) then
# FIXME use Warning
errors.add new Error("Deserialization Error: object with id {id} is deserialized twice.")
# Keep going
end
var type_name = null
var i = 1
# Read dynamic type
if object.length >= 2 then
# Try to get the type name as a string
var o = object[i]
if o isa String and static_type == "String" and object.length == 2 then
cache[id] = o
return o
else
var typ = convert_object(object[i], "String")
if typ isa String then
type_name = typ
i += 1
end
end
end
if type_name == null then
# There was no dynamic type
# We could use a `class_name_heuristic` here...
# Fallback to the static type
if static_type != null then
type_name = static_type.strip_nullable
end
if type_name == null then
errors.add new Error("Deserialization Error: could not determine dynamic type of `{object}`.")
return null
end
end
if not accept(type_name, static_type) then return null
var attributes = null
if object.length > i then attributes = object[i]
if not attributes isa Map[nullable Serializable, nullable Serializable] then
# Some other type (could be an error), or there's no attributes
attributes = new Map[nullable Serializable, nullable Serializable]
end
# advance on path
path.push attributes
path_arrays.push object
just_opened_id = id
var value = deserialize_class(type_name)
just_opened_id = null
# revert on path
path.pop
path_arrays.pop
return value
else
errors.add new Error("Deserialization Error: unknown MessagePack ext '{first.typ}'.")
end
end
# Plain array? Try to convert it to the desired static_type
if static_type != null then
return deserialize_class(static_type.strip_nullable)
end
return object
end
if object isa Map[nullable Serializable, nullable Serializable] then
# Plain map
# TODO parse it as an instance of `static_type`
if static_type != null then
path.push object
path_arrays.push null
just_opened_id = null
var value = deserialize_class(static_type.strip_nullable)
path.pop
path_arrays.pop
return value
end
return object
end
if object isa MsgPackExt then
# First try the custom extensions
var custom = deserialize_ext(object, static_type)
if custom == null then
# No custom, go for deser standard references
if object.typ == ext_typ_ref then
# Reference to an object
var id = object.data.to_i
if not cache.has_id(id) then
errors.add new Error("Deserialization Error: object reference id unknown.")
return object
end
return cache.object_for(id)
else if object.typ == ext_typ_char then
# Char
return object.data.to_s.first
else if object.typ == ext_typ_byte then
# Byte
return object.data.first
end
end
end
if object isa String and object.length == 1 and static_type == "Char" then
# Char serialized as a 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
lib/msgpack/serialization_read.nit:163,2--311,4