Deserialize MessagePack format to full Nit objects

Introduced properties

fun attributes_path: Array[String]

msgpack :: MsgPackDeserializer :: attributes_path

Names of the attributes from the root to the object currently being deserialized
protected fun attributes_path=(attributes_path: Array[String])

msgpack :: MsgPackDeserializer :: attributes_path=

Names of the attributes from the root to the object currently being deserialized
protected fun deserialize_ext(ext: MsgPackExt, static_type: nullable String): nullable Object

msgpack :: MsgPackDeserializer :: deserialize_ext

Hook to customize the deserialization of MessagePack extensions
fun path_arrays: Array[nullable Array[nullable Object]]

msgpack :: MsgPackDeserializer :: path_arrays

Metadata arrays with from the root deserialized object to the current object
protected fun path_arrays=(path_arrays: Array[nullable Array[nullable Object]])

msgpack :: MsgPackDeserializer :: path_arrays=

Metadata arrays with from the root deserialized object to the current object
fun stream: Reader

msgpack :: MsgPackDeserializer :: stream

Source stream
protected fun stream=(stream: Reader)

msgpack :: MsgPackDeserializer :: stream=

Source stream

Redefined properties

redef type SELF: MsgPackDeserializer

msgpack $ MsgPackDeserializer :: SELF

Type of this instance, automatically specialized in every class
redef fun deserialize(static_type: nullable String): nullable Object

msgpack $ MsgPackDeserializer :: deserialize

Deserialize and return an object, storing errors in the attribute errors
redef fun deserialize_attribute(name: String, static_type: nullable String): nullable Object

msgpack $ MsgPackDeserializer :: deserialize_attribute

Deserialize the attribute with name from the object open for deserialization
redef fun notify_of_creation(new_object: Object)

msgpack $ MsgPackDeserializer :: notify_of_creation

This may be called multiple times by the same object from defs of a same constructor

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun accept(dynamic_type: Text, static_type: nullable Text): Bool

serialization :: SafeDeserializer :: accept

Should self accept to deserialize an instance of dynamic_type for an attribute wuth static_type?
protected fun accept_json_serializer(v: JsonSerializer)

serialization :: Serializable :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
protected fun accept_msgpack_attribute_counter(v: AttributeCounter)

serialization :: Serializable :: accept_msgpack_attribute_counter

Hook to customize the behavior of the AttributeCounter
protected fun accept_msgpack_serializer(v: MsgPackSerializer)

serialization :: Serializable :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)

serialization :: Serializable :: add_to_bundle

Called by []= to dynamically choose the appropriate method according
fun attributes_path: Array[String]

msgpack :: MsgPackDeserializer :: attributes_path

Names of the attributes from the root to the object currently being deserialized
protected fun attributes_path=(attributes_path: Array[String])

msgpack :: MsgPackDeserializer :: attributes_path=

Names of the attributes from the root to the object currently being deserialized
fun cache: DeserializerCache

serialization :: CachingDeserializer :: cache

Cache of known objects
fun cache=(cache: DeserializerCache)

serialization :: CachingDeserializer :: cache=

Cache of known objects
fun check_subtypes: Bool

serialization :: SafeDeserializer :: check_subtypes

Should objects be checked if they a subtype of the static type before deserialization?
fun check_subtypes=(check_subtypes: Bool)

serialization :: SafeDeserializer :: check_subtypes=

Should objects be checked if they a subtype of the static type before deserialization?
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun core_serialize_to(serializer: Serializer)

serialization :: Serializable :: core_serialize_to

Actual serialization of self to serializer
abstract fun deserialize(static_type: nullable String): nullable Object

serialization :: Deserializer :: deserialize

Deserialize and return an object, storing errors in the attribute errors
abstract fun deserialize_attribute(name: String, static_type: nullable String): nullable Object

serialization :: Deserializer :: deserialize_attribute

Deserialize the attribute with name from the object open for deserialization
fun deserialize_attribute_missing: Bool

serialization :: Deserializer :: deserialize_attribute_missing

Was the attribute queried by the last call to deserialize_attribute missing?
protected fun deserialize_attribute_missing=(deserialize_attribute_missing: Bool)

serialization :: Deserializer :: deserialize_attribute_missing=

Was the attribute queried by the last call to deserialize_attribute missing?
protected fun deserialize_class(class_name: Text): nullable Object

serialization :: Deserializer :: deserialize_class

Deserialize the next available object as an instance of class_name
protected fun deserialize_class_intern(class_name: Text): nullable Object

serialization :: Deserializer :: deserialize_class_intern

Generated service to deserialize the next available object as an instance of class_name
protected fun deserialize_ext(ext: MsgPackExt, static_type: nullable String): nullable Object

msgpack :: MsgPackDeserializer :: deserialize_ext

Hook to customize the deserialization of MessagePack extensions
fun errors: Array[Error]

serialization :: Deserializer :: errors

Errors encountered in the last call to deserialize
protected fun errors=(errors: Array[Error])

serialization :: Deserializer :: errors=

Errors encountered in the last call to deserialize
fun ext_typ_byte: Int

msgpack :: MsgPackEngine :: ext_typ_byte

ext type byte to identify a byte, defaults to 0x7Eu8 or '|'
fun ext_typ_byte=(ext_typ_byte: Int)

msgpack :: MsgPackEngine :: ext_typ_byte=

ext type byte to identify a byte, defaults to 0x7Eu8 or '|'
fun ext_typ_char: Int

msgpack :: MsgPackEngine :: ext_typ_char

ext type byte to identify a char, defaults to 0x7Cu8 or '~'
fun ext_typ_char=(ext_typ_char: Int)

msgpack :: MsgPackEngine :: ext_typ_char=

ext type byte to identify a char, defaults to 0x7Cu8 or '~'
fun ext_typ_obj: Int

msgpack :: MsgPackEngine :: ext_typ_obj

ext type byte for object definitions, defaults to 0x7Bu8 or '{'
fun ext_typ_obj=(ext_typ_obj: Int)

msgpack :: MsgPackEngine :: ext_typ_obj=

ext type byte for object definitions, defaults to 0x7Bu8 or '{'
fun ext_typ_ref: Int

msgpack :: MsgPackEngine :: ext_typ_ref

ext type byte for object references, defaults to 0x7Du8 or '}'
fun ext_typ_ref=(ext_typ_ref: Int)

msgpack :: MsgPackEngine :: ext_typ_ref=

ext type byte for object references, defaults to 0x7Du8 or '}'
init from_deserializer(deserializer: Deserializer)

serialization :: Serializable :: from_deserializer

Create an instance of this class from the deserializer
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun keep_going: nullable Bool

serialization :: Deserializer :: keep_going

Should self keep trying to deserialize an object after an error?
fun keep_going=(keep_going: nullable Bool)

serialization :: Deserializer :: keep_going=

Should self keep trying to deserialize an object after an error?
protected fun msgpack_extra_array_items: Int

serialization :: Serializable :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
abstract fun notify_of_creation(new_object: Object)

serialization :: Deserializer :: notify_of_creation

Register a newly allocated object (even if not completely built)
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun path_arrays: Array[nullable Array[nullable Object]]

msgpack :: MsgPackDeserializer :: path_arrays

Metadata arrays with from the root deserialized object to the current object
protected fun path_arrays=(path_arrays: Array[nullable Array[nullable Object]])

msgpack :: MsgPackDeserializer :: path_arrays=

Metadata arrays with from the root deserialized object to the current object
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(plain: nullable Bool): Bytes

serialization :: Serializable :: serialize_msgpack

Serialize self to MessagePack bytes
fun serialize_to(serializer: Serializer)

serialization :: Serializable :: serialize_to

Serialize self to serializer
fun serialize_to_json(plain: nullable Bool, pretty: nullable Bool): String

serialization :: Serializable :: serialize_to_json

Serialize self to JSON
fun stream: Reader

msgpack :: MsgPackDeserializer :: stream

Source stream
protected fun stream=(stream: Reader)

msgpack :: MsgPackDeserializer :: stream=

Source stream
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_json: String

serialization :: Serializable :: to_json

Serialize self to plain JSON
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_pretty_json: String

serialization :: Serializable :: to_pretty_json

Serialize self to plain pretty JSON
fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun whitelist: Array[Text]

serialization :: SafeDeserializer :: whitelist

Accepted parameterized classes to deserialize
protected fun whitelist=(whitelist: Array[Text])

serialization :: SafeDeserializer :: whitelist=

Accepted parameterized classes to deserialize
package_diagram msgpack::MsgPackDeserializer MsgPackDeserializer serialization::CachingDeserializer CachingDeserializer msgpack::MsgPackDeserializer->serialization::CachingDeserializer msgpack::MsgPackEngine MsgPackEngine msgpack::MsgPackDeserializer->msgpack::MsgPackEngine serialization::SafeDeserializer SafeDeserializer msgpack::MsgPackDeserializer->serialization::SafeDeserializer serialization::Deserializer Deserializer serialization::CachingDeserializer->serialization::Deserializer core::Object Object msgpack::MsgPackEngine->core::Object serialization::SafeDeserializer->serialization::Deserializer ...serialization::Deserializer ... ...serialization::Deserializer->serialization::Deserializer ...core::Object ... ...core::Object->core::Object

Ancestors

abstract class Deserializer

serialization :: Deserializer

Abstract deserialization service
interface Object

core :: Object

The root of the class hierarchy.
interface Serializable

serialization :: Serializable

Instances of this class can be passed to Serializer::serialize

Parents

abstract class CachingDeserializer

serialization :: CachingDeserializer

A Deserializer with a cache
abstract class MsgPackEngine

msgpack :: MsgPackEngine

MessagePack serialization or deserialization engine
class SafeDeserializer

serialization :: SafeDeserializer

Deserialization engine limiting which types can be deserialized

Class definitions

msgpack $ MsgPackDeserializer
# Deserialize MessagePack format to full Nit objects
class MsgPackDeserializer
	super CachingDeserializer
	super MsgPackEngine
	super SafeDeserializer

	# Source stream
	var stream: Reader

	# Map of attributes from the root deserialized object to the current object
	private var path = new Array[Map[nullable Serializable, nullable Serializable]]

	# Metadata arrays with from the root deserialized object to the current object
	var path_arrays = new Array[nullable Array[nullable Object]]

	# Names of the attributes from the root to the object currently being deserialized
	var attributes_path = new Array[String]

	# Last encountered object reference id.
	#
	# See `id_to_object`.
	private var just_opened_id: nullable Int = null

	redef fun deserialize_attribute(name, static_type)
	do
		if path.is_empty then
			# The was a parsing error or the root is not an object
			deserialize_attribute_missing = false
			return null
		end

		var current = path.last

		var serialized_value = null
		var serialized_value_found = false
		if current.keys.has(name) then
			# Non-cached string
			serialized_value = current[name]
			serialized_value_found = true
		else
			# It may be cached, deserialize all keys until we find it
			for key in current.keys.to_a do
				if key isa Array[nullable Serializable] or key isa MsgPackExt then
					var str = convert_object(key, "String")
					if str isa String then
						var value = current[key]
						current.keys.remove key
						current[str] = value

						if str == name then
							serialized_value = value
							serialized_value_found = true
							break
						end
					end
				end
			end
		end

		if not serialized_value_found then
			# Let the generated code / caller of `deserialize_attribute` raise the missing attribute error
			deserialize_attribute_missing = true
			return null
		end

		attributes_path.add name
		var res = convert_object(serialized_value, static_type)
		attributes_path.pop

		deserialize_attribute_missing = false
		return res
	end

	# This may be called multiple times by the same object from defs of a same constructor
	redef fun notify_of_creation(new_object)
	do
		var id = just_opened_id
		if id == null then return
		cache[id] = new_object
	end

	# 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

	redef fun deserialize(static_type)
	do
		errors.clear

		var root = stream.read_msgpack
		return convert_object(root, static_type)
	end

	# Hook to customize the deserialization of MessagePack extensions
	#
	# Redefine this method in subclasses to return custom Nit objects from
	# an application specific extension.
	#
	# This method is invoked before dealing with the extensions used by the
	# Nit serialization metadata [0x40..0x43]. In general, you should ignore
	# them by returning `null`, but they can also be intercepted to comply to
	# a format from a remote server.
	protected fun deserialize_ext(ext: MsgPackExt, static_type: nullable String): nullable Object
	do
		return null
	end
end
lib/msgpack/serialization_read.nit:82,1--334,3