json :: JsonDeserializer :: defaultinit
# 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