Deserializer from a Json string.

Introduced properties

fun attributes_path: Array[String]

json :: JsonDeserializer :: attributes_path

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

json :: JsonDeserializer :: attributes_path=

Names of the attributes from the root to the object currently being deserialized
protected fun class_name_heuristic(json_object: Map[String, nullable Object]): nullable String

json :: JsonDeserializer :: class_name_heuristic

User customizable heuristic to infer the name of the Nit class to deserialize json_object
fun just_opened_id: nullable Int

json :: JsonDeserializer :: just_opened_id

Last encountered object reference id.
protected fun just_opened_id=(just_opened_id: nullable Int)

json :: JsonDeserializer :: just_opened_id=

Last encountered object reference id.

Redefined properties

redef type SELF: JsonDeserializer

json $ JsonDeserializer :: SELF

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

json $ JsonDeserializer :: deserialize

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

json $ JsonDeserializer :: deserialize_attribute

Deserialize the attribute with name from the object open for deserialization
redef init init

json $ JsonDeserializer :: init

redef fun notify_of_creation(new_object: Object)

json $ JsonDeserializer :: notify_of_creation

This may be called multiple times by the same object from constructors

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]

json :: JsonDeserializer :: attributes_path

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

json :: JsonDeserializer :: 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.
protected fun class_name_heuristic(json_object: Map[String, nullable Object]): nullable String

json :: JsonDeserializer :: class_name_heuristic

User customizable heuristic to infer the name of the Nit class to deserialize json_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
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
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 just_opened_id: nullable Int

json :: JsonDeserializer :: just_opened_id

Last encountered object reference id.
protected fun just_opened_id=(just_opened_id: nullable Int)

json :: JsonDeserializer :: just_opened_id=

Last encountered object reference id.
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 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
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 json::JsonDeserializer JsonDeserializer serialization::CachingDeserializer CachingDeserializer json::JsonDeserializer->serialization::CachingDeserializer serialization::SafeDeserializer SafeDeserializer json::JsonDeserializer->serialization::SafeDeserializer serialization::Deserializer Deserializer serialization::CachingDeserializer->serialization::Deserializer serialization::SafeDeserializer->serialization::Deserializer ...serialization::Deserializer ... ...serialization::Deserializer->serialization::Deserializer github::GithubDeserializer GithubDeserializer github::GithubDeserializer->json::JsonDeserializer

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
class SafeDeserializer

serialization :: SafeDeserializer

Deserialization engine limiting which types can be deserialized

Children

class GithubDeserializer

github :: GithubDeserializer

JsonDeserializer specific for Github objects.

Class definitions

json $ JsonDeserializer
# Deserializer from a Json string.
class JsonDeserializer
	super CachingDeserializer
	super SafeDeserializer

	# Json text to deserialize from.
	private var text: Text

	# Root json object parsed from input text.
	private var root: nullable Object is noinit

	# Depth-first path in the serialized object tree.
	private var path = new Array[Map[String, 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`.
	var just_opened_id: nullable Int = null

	init do
		var root = text.parse_json
		if root isa Map[String, nullable Object] then path.add(root)
		self.root = root
	end

	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
			if not root isa Error then
				errors.add new Error("Deserialization Error: parsed JSON value is not an object.")
			end
			deserialize_attribute_missing = false
			return null
		end

		var current = path.last

		if not current.keys.has(name) then
			# Let the generated code / caller of `deserialize_attribute` raise the missing attribute error
			deserialize_attribute_missing = true
			return null
		end

		var value = current[name]

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

		deserialize_attribute_missing = false
		return res
	end

	# This may be called multiple times by the same object from constructors
	# in different nclassdef
	redef fun notify_of_creation(new_object)
	do
		var id = just_opened_id
		if id == null then return # Register `new_object` only once
		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
		if object isa JsonParseError then
			errors.add object
			return null
		end

		if object isa Map[String, nullable Object] then
			var kind = null
			if object.keys.has("__kind") then
				kind = object["__kind"]
			end

			# ref?
			if kind == "ref" then
				if not object.keys.has("__id") then
					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("Deserialization Error: JSON object reference declares a non-integer `__id`.")
					return object
				end

				if not cache.has_id(id) then
					errors.add new Error("Deserialization Error: JSON object reference has an unknown `__id`.")
					return object
				end

				return cache.object_for(id)
			end

			# obj?
			if kind == "obj" or kind == null then
				var id = null
				if object.keys.has("__id") then
					id = object["__id"]

					if not id isa Int then
						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("Deserialization Error: JSON object with `__id` {id} is deserialized twice.")
						# Keep going
					end
				end

				var class_name = object.get_or_null("__class")
				if class_name == null then
					# Fallback to custom heuristic
					class_name = class_name_heuristic(object)

					if class_name == null and static_type != null then
						# Fallack to the static type, strip the `nullable` prefix
						class_name = static_type.strip_nullable
					end
				end

				if class_name == null then
					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("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

				just_opened_id = id
				var value = deserialize_class(class_name)
				just_opened_id = null

				# revert on path
				path.pop

				return value
			end

			# char?
			if kind == "char" then
				if not object.keys.has("__val") then
					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("Deserialization Error: JSON `char` object does not declare a single char in `__val`.")
					return object
				end

				return val.chars.first
			end

			# 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

		# Simple JSON array without serialization metadata
		if object isa Array[nullable Object] then
			# Can we use the static type?
			if static_type != null then
				opened_array = object
				var value = deserialize_class(static_type.strip_nullable)
				opened_array = null
				return value
			end

			# This branch should rarely be used:
			# when an array is the root object which is accepted but illegal in standard JSON,
			# or in strange custom deserialization hacks.

			var array = new Array[nullable Object]
			var types = new HashSet[String]
			var has_nullable = false
			for e in object do
				var res = convert_object(e)
				array.add res

				if res != null then
					types.add res.class_name
				else has_nullable = true
			end

			if types.length == 1 then
				var array_type = types.first

				var typed_array
				if array_type == "ASCIIFlatString" or array_type == "UnicodeFlatString" then
					if has_nullable then
						typed_array = new Array[nullable FlatString]
					else typed_array = new Array[FlatString]
				else if array_type == "Int" then
					if has_nullable then
						typed_array = new Array[nullable Int]
					else typed_array = new Array[Int]
				else if array_type == "Float" then
					if has_nullable then
						typed_array = new Array[nullable Float]
					else typed_array = new Array[Float]
				else
					# TODO support all array types when we separate the constructor
					# `from_deserializer` from the filling of the items.

					if not has_nullable then
						typed_array = new Array[Object]
					else
						# Unsupported array type, return as `Array[nullable Object]`
						return array
					end
				end

				assert typed_array isa Array[nullable Object]

				# Copy item to the new array
				for e in array do typed_array.add e
				return typed_array
			end

			# Uninferrable type, return as `Array[nullable Object]`
			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(static_type)
	do
		errors.clear
		return convert_object(root, static_type)
	end

	# User customizable heuristic to infer the name of the Nit class to deserialize `json_object`
	#
	# This method is called only when deserializing an object without the metadata `__class`.
	# Use the content of `json_object` to identify what Nit class it should be deserialized into.
	# Or use `self.attributes_path` indicating where the deserialized object will be stored,
	# is is less reliable as some objects don't have an associated attribute:
	# the root/first deserialized object and collection elements.
	#
	# Return the class name as a `String` when it can be inferred,
	# or `null` when the class name cannot be found.
	#
	# If a valid class name is returned, `json_object` will then be deserialized normally.
	# So it must contain the attributes of the corresponding class, as usual.
	#
	# ~~~
	# class MyData
	#     serialize
	#
	#     var data: String
	# end
	#
	# class MyError
	#     serialize
	#
	#     var error: String
	#     var related_data: MyData
	# end
	#
	# class MyJsonDeserializer
	#     super JsonDeserializer
	#
	#     redef fun class_name_heuristic(json_object)
	#     do
	#         # Infer the Nit class from the content of the JSON object.
	#         if json_object.keys.has("error") then return "MyError"
	#         if json_object.keys.has("data") then return "MyData"
	#
	#         # Infer the Nit class from the attribute where it will be stored.
	#         # This line duplicates a previous line, and would only apply when
	#         # `MyData` is within a `MyError`.
	#         if attributes_path.not_empty and attributes_path.last == "related_data" then return "MyData"
	#
	#         return null
	#     end
	# end
	#
	# var json = """{"data": "some data"}"""
	# var deserializer = new MyJsonDeserializer(json)
	# var deserialized = deserializer.deserialize
	# assert deserializer.errors.is_empty
	# assert deserialized isa MyData
	#
	# json = """{"error": "some error message",
	#            "related_data": {"data": "some other data"}}"""
	# deserializer = new MyJsonDeserializer(json)
	# deserialized = deserializer.deserialize
	# assert deserializer.errors.is_empty
	# assert deserialized isa MyError
	# ~~~
	protected fun class_name_heuristic(json_object: Map[String, nullable Object]): nullable String
	do
		return null
	end
end
lib/json/serialization_read.nit:25,1--361,3