1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Services to read JSON: `from_json_string` and `JsonDeserializer`
16 module serialization_read
18 import ::serialization
::caching
19 private import ::serialization
::engine_tools
21 private import string_parser
24 # Deserializer from a Json string.
25 class JsonDeserializer
26 super CachingDeserializer
28 # Json text to deserialize from.
29 private var text
: Text
31 # Accepted parameterized classes to deserialize
33 # If `whitelist.empty`, all types are accepted.
36 # import json::serialization
42 # var json_string = """
43 # {"__class" = "MyClass"}
46 # var deserializer = new JsonDeserializer(json_string)
47 # var obj = deserializer.deserialize
48 # assert deserializer.errors.is_empty
49 # assert obj isa MyClass
51 # deserializer = new JsonDeserializer(json_string)
52 # deserializer.whitelist.add "Array[String]"
53 # deserializer.whitelist.add "AnotherAcceptedClass"
54 # obj = deserializer.deserialize
55 # assert deserializer.errors.length == 1
58 var whitelist
= new Array[Text]
60 # Should objects be checked if they a subtype of the static type before deserialization?
62 # Defaults to `true`, as it should always be activated.
63 # It can be turned off to implement the subtype check itself.
64 var check_subtypes
= true is writable
66 # Root json object parsed from input text.
67 private var root
: nullable Object is noinit
69 # Depth-first path in the serialized object tree.
70 private var path
= new Array[Map[String, nullable Object]]
72 # Names of the attributes from the root to the object currently being deserialized
73 var attributes_path
= new Array[String]
75 # Last encountered object reference id.
78 var just_opened_id
: nullable Int = null
81 var root
= text
.parse_json
82 if root
isa Map[String, nullable Object] then path
.add
(root
)
86 redef fun deserialize_attribute
(name
, static_type
)
89 # The was a parsing error or the root is not an object
90 if not root
isa Error then
91 errors
.add
new Error("Deserialization Error: parsed JSON value is not an object.")
93 deserialize_attribute_missing
= false
97 var current
= path
.last
99 if not current
.keys
.has
(name
) then
100 # Let the generated code / caller of `deserialize_attribute` raise the missing attribute error
101 deserialize_attribute_missing
= true
105 var value
= current
[name
]
107 attributes_path
.add name
108 var res
= convert_object
(value
, static_type
)
111 deserialize_attribute_missing
= false
115 # This may be called multiple times by the same object from constructors
116 # in different nclassdef
117 redef fun notify_of_creation
(new_object
)
119 var id
= just_opened_id
120 if id
== null then return # Register `new_object` only once
121 cache
[id
] = new_object
124 # Convert the simple JSON `object` to a Nit object
125 private fun convert_object
(object
: nullable Object, static_type
: nullable String): nullable Object
127 if object
isa JsonParseError then
132 if object
isa Map[String, nullable Object] then
134 if object
.keys
.has
("__kind") then
135 kind
= object
["__kind"]
139 if kind
== "ref" then
140 if not object
.keys
.has
("__id") then
141 errors
.add
new Error("Serialization Error: JSON object reference does not declare a `__id`.")
145 var id
= object
["__id"]
146 if not id
isa Int then
147 errors
.add
new Error("Serialization Error: JSON object reference declares a non-integer `__id`.")
151 if not cache
.has_id
(id
) then
152 errors
.add
new Error("Serialization Error: JSON object reference has an unknown `__id`.")
156 return cache
.object_for
(id
)
160 if kind
== "obj" or kind
== null then
162 if object
.keys
.has
("__id") then
165 if not id
isa Int then
166 errors
.add
new Error("Serialization Error: JSON object declaration declares a non-integer `__id`.")
170 if cache
.has_id
(id
) then
171 errors
.add
new Error("Serialization Error: JSON object with `__id` {id} is deserialized twice.")
176 var class_name
= object
.get_or_null
("__class")
177 if class_name
== null then
178 # Fallback to custom heuristic
179 class_name
= class_name_heuristic
(object
)
181 if class_name
== null and static_type
!= null then
182 # Fallack to the static type, strip the `nullable` prefix
183 var prefix
= "nullable "
184 if static_type
.has_prefix
(prefix
) then
185 class_name
= static_type
.substring_from
(prefix
.length
)
186 else class_name
= static_type
190 if class_name
== null then
191 errors
.add
new Error("Serialization Error: JSON object declaration does not declare a `__class`.")
195 if not class_name
isa String then
196 errors
.add
new Error("Serialization Error: JSON object declaration declares a non-string `__class`.")
200 if whitelist
.not_empty
and not whitelist
.has
(class_name
) then
201 errors
.add
new Error("Deserialization Error: '{class_name}' not in whitelist")
205 if static_type
!= null and check_subtypes
then
206 var static_class
= static_type
.strip_nullable_and_params
207 var dynamic_class
= class_name
.strip_nullable_and_params
208 if not class_inheritance_metamodel
.has_edge
(dynamic_class
, static_class
) then
209 errors
.add
new Error("Deserialization Error: `{class_name}` is not a subtype of the static type `{static_type}`")
218 var value
= deserialize_class
(class_name
)
219 just_opened_id
= null
228 if kind
== "char" then
229 if not object
.keys
.has
("__val") then
230 errors
.add
new Error("Serialization Error: JSON `char` object does not declare a `__val`.")
234 var val
= object
["__val"]
236 if not val
isa String or val
.is_empty
then
237 errors
.add
new Error("Serialization Error: JSON `char` object does not declare a single char in `__val`.")
241 return val
.chars
.first
244 errors
.add
new Error("Serialization Error: JSON object has an unknown `__kind`.")
248 # Simple JSON array without serialization metadata
249 if object
isa Array[nullable Object] then
250 # Can we use the static type?
251 if static_type
!= null then
252 var prefix
= "nullable "
253 var class_name
= if static_type
.has
(prefix
) then
254 static_type
.substring_from
(prefix
.length
)
257 opened_array
= object
258 var value
= deserialize_class
(class_name
)
263 # This branch should rarely be used:
264 # when an array is the root object which is accepted but illegal in standard JSON,
265 # or in strange custom deserialization hacks.
267 var array
= new Array[nullable Object]
268 var types
= new HashSet[String]
269 var has_nullable
= false
271 var res
= convert_object
(e
)
275 types
.add res
.class_name
276 else has_nullable
= true
279 if types
.length
== 1 then
280 var array_type
= types
.first
283 if array_type
== "ASCIIFlatString" or array_type
== "UnicodeFlatString" then
285 typed_array
= new Array[nullable FlatString]
286 else typed_array
= new Array[FlatString]
287 else if array_type
== "Int" then
289 typed_array
= new Array[nullable Int]
290 else typed_array
= new Array[Int]
291 else if array_type
== "Float" then
293 typed_array
= new Array[nullable Float]
294 else typed_array
= new Array[Float]
296 # TODO support all array types when we separate the constructor
297 # `from_deserializer` from the filling of the items.
299 if not has_nullable
then
300 typed_array
= new Array[Object]
302 # Unsupported array type, return as `Array[nullable Object]`
307 assert typed_array
isa Array[nullable Object]
309 # Copy item to the new array
310 for e
in array
do typed_array
.add e
314 # Uninferrable type, return as `Array[nullable Object]`
321 # Current array open for deserialization, used by `SimpleCollection::from_deserializer`
322 private var opened_array
: nullable Array[nullable Object] = null
324 redef fun deserialize
(static_type
)
327 return convert_object
(root
, static_type
)
330 # User customizable heuristic to infer the name of the Nit class to deserialize `json_object`
332 # This method is called only when deserializing an object without the metadata `__class`.
333 # Use the content of `json_object` to identify what Nit class it should be deserialized into.
334 # Or use `self.attributes_path` indicating where the deserialized object will be stored,
335 # is is less reliable as some objects don't have an associated attribute:
336 # the root/first deserialized object and collection elements.
338 # Return the class name as a `String` when it can be inferred,
339 # or `null` when the class name cannot be found.
341 # If a valid class name is returned, `json_object` will then be deserialized normally.
342 # So it must contain the attributes of the corresponding class, as usual.
355 # var related_data: MyData
358 # class MyJsonDeserializer
359 # super JsonDeserializer
361 # redef fun class_name_heuristic(json_object)
363 # # Infer the Nit class from the content of the JSON object.
364 # if json_object.keys.has("error") then return "MyError"
365 # if json_object.keys.has("data") then return "MyData"
367 # # Infer the Nit class from the attribute where it will be stored.
368 # # This line duplicates a previous line, and would only apply when
369 # # `MyData` is within a `MyError`.
370 # if attributes_path.not_empty and attributes_path.last == "related_data" then return "MyData"
376 # var json = """{"data": "some data"}"""
377 # var deserializer = new MyJsonDeserializer(json)
378 # var deserialized = deserializer.deserialize
379 # assert deserializer.errors.is_empty
380 # assert deserialized isa MyData
382 # json = """{"error": "some error message",
383 # "related_data": {"data": "some other data"}}"""
384 # deserializer = new MyJsonDeserializer(json)
385 # deserialized = deserializer.deserialize
386 # assert deserializer.errors.is_empty
387 # assert deserialized isa MyError
389 protected fun class_name_heuristic
(json_object
: Map[String, nullable Object]): nullable String
397 # Deserialize a `nullable Object` from this JSON formatted string
399 # Warning: Deserialization errors are reported with `print_error` and
400 # may be returned as a partial object or as `null`.
402 # This method is not appropriate when errors need to be handled programmatically,
403 # manually use a `JsonDeserializer` in such cases.
404 fun from_json_string
: nullable Object
406 var deserializer
= new JsonDeserializer(self)
407 var res
= deserializer
.deserialize
408 if deserializer
.errors
.not_empty
then
409 print_error
"Deserialization Errors: {deserializer.errors.join(", ")}"
414 # Strip the `nullable` prefix and the params from the class name `self`
417 # assert "String".strip_nullable_and_params == "String"
418 # assert "Array[Int]".strip_nullable_and_params == "Array"
419 # assert "Map[Set[String], Set[Int]]".strip_nullable_and_params == "Map"
421 private fun strip_nullable_and_params
: String
423 var class_name
= to_s
425 var prefix
= "nullable "
426 if class_name
.has_prefix
(prefix
) then class_name
= class_name
.substring_from
(prefix
.length
)
428 var bracket_index
= class_name
.index_of
('[')
429 if bracket_index
== -1 then return class_name
430 return class_name
.substring
(0, bracket_index
)
434 redef class SimpleCollection[E
]
435 redef init from_deserializer
(v
)
438 if v
isa JsonDeserializer then
439 v
.notify_of_creation
self
442 var open_array
: nullable SequenceRead[nullable Object] = v
.opened_array
443 if open_array
== null then
445 var arr
= v
.path
.last
.get_or_null
("__items")
446 if not arr
isa SequenceRead[nullable Object] then
447 # If there is nothing, we consider that it is an empty collection.
448 if arr
!= null then v
.errors
.add
new Error("Deserialization Error: invalid format in {self.class_name}")
454 # Try to get the name of the single parameter type assuming it is E.
455 # This does not work in non-generic subclasses,
456 # when the first parameter is not E, or
457 # when there is more than one parameter. (The last one could be fixed)
458 var class_name
= class_name
459 var items_type
= null
460 var bracket_index
= class_name
.index_of
('[')
461 if bracket_index
!= -1 then
462 var start
= bracket_index
+ 1
463 var ending
= class_name
.last_index_of
(']')
464 items_type
= class_name
.substring
(start
, ending-start
)
468 for o
in open_array
do
469 var obj
= v
.convert_object
(o
, items_type
)
472 else v
.errors
.add
new AttributeTypeError(self, "items", obj
, "E")
478 redef class Map[K
, V
]
479 redef init from_deserializer
(v
)
483 if v
isa JsonDeserializer then
484 v
.notify_of_creation
self
487 var length
= v
.deserialize_attribute
("__length")
488 var keys
= v
.path
.last
.get_or_null
("__keys")
489 var values
= v
.path
.last
.get_or_null
("__values")
492 if length
== null and keys
isa SequenceRead[nullable Object] then length
= keys
.length
495 if not length
isa Int or length
< 0 or
496 not keys
isa SequenceRead[nullable Object] or
497 not values
isa SequenceRead[nullable Object] or
498 keys
.length
!= values
.length
or length
!= keys
.length
then
500 # If there is nothing or length == 0, we consider that it is an empty Map.
501 if (length
!= null and length
!= 0) or keys
!= null or values
!= null then
502 v
.errors
.add
new Error("Deserialization Error: invalid format in {self.class_name}")
507 for i
in length
.times
do
508 var key
= v
.convert_object
(keys
[i
])
509 var value
= v
.convert_object
(values
[i
])
511 if not key
isa K
then
512 v
.errors
.add
new AttributeTypeError(self, "keys", key
, "K")
516 if not value
isa V
then
517 v
.errors
.add
new AttributeTypeError(self, "values", value
, "V")
522 v
.errors
.add
new Error("Deserialization Error: duplicated key '{key or else "null"}' in {self.class_name}, previous value overwritten")
534 # Class inheritance graph as a `POSet[String]` serialized to JSON
535 private fun class_inheritance_metamodel_json
: NativeString is intern
538 # Class inheritance graph
541 # var hierarchy = class_inheritance_metamodel
542 # assert hierarchy.has_edge("String", "Object")
543 # assert not hierarchy.has_edge("Object", "String")
545 var class_inheritance_metamodel
: POSet[String] is lazy
do
546 var engine
= new JsonDeserializer(class_inheritance_metamodel_json
.to_s
)
547 engine
.check_subtypes
= false
548 engine
.whitelist
.add_all
(
549 ["String", "POSet[String]", "POSetElement[String]", "HashSet[String]", "HashMap[String, POSetElement[String]]"])
550 var poset
= engine
.deserialize
551 if engine
.errors
.not_empty
then
552 print_error engine
.errors
.join
("\n")
553 return new POSet[String]
555 if poset
isa POSet[String] then return poset
556 return new POSet[String]
560 redef class Deserializer
561 redef fun deserialize_class
(name
)
563 if name
== "POSet[String]" then return new POSet[String].from_deserializer
(self)
564 if name
== "POSetElement[String]" then return new POSetElement[String].from_deserializer
(self)
565 if name
== "HashSet[String]" then return new HashSet[String].from_deserializer
(self)
566 if name
== "HashMap[String, POSetElement[String]]" then return new HashMap[String, POSetElement[String]].from_deserializer
(self)