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
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
lib/json/serialization_read.nit:91,2--287,4