json: fixme interpreter bug in constructors with return (#2546)
[nit.git] / lib / json / serialization_read.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Services to read JSON: `from_json_string` and `JsonDeserializer`
16 module serialization_read
17
18 import serialization::caching
19 private import serialization::engine_tools
20 import serialization::safe
21
22 private import static
23 private import string_parser
24 import poset
25
26 # Deserializer from a Json string.
27 class JsonDeserializer
28 super CachingDeserializer
29 super SafeDeserializer
30
31 # Json text to deserialize from.
32 private var text: Text
33
34 # Root json object parsed from input text.
35 private var root: nullable Object is noinit
36
37 # Depth-first path in the serialized object tree.
38 private var path = new Array[Map[String, nullable Object]]
39
40 # Names of the attributes from the root to the object currently being deserialized
41 var attributes_path = new Array[String]
42
43 # Last encountered object reference id.
44 #
45 # See `id_to_object`.
46 var just_opened_id: nullable Int = null
47
48 init do
49 var root = text.parse_json
50 if root isa Map[String, nullable Object] then path.add(root)
51 self.root = root
52 end
53
54 redef fun deserialize_attribute(name, static_type)
55 do
56 if path.is_empty then
57 # The was a parsing error or the root is not an object
58 if not root isa Error then
59 errors.add new Error("Deserialization Error: parsed JSON value is not an object.")
60 end
61 deserialize_attribute_missing = false
62 return null
63 end
64
65 var current = path.last
66
67 if not current.keys.has(name) then
68 # Let the generated code / caller of `deserialize_attribute` raise the missing attribute error
69 deserialize_attribute_missing = true
70 return null
71 end
72
73 var value = current[name]
74
75 attributes_path.add name
76 var res = convert_object(value, static_type)
77 attributes_path.pop
78
79 deserialize_attribute_missing = false
80 return res
81 end
82
83 # This may be called multiple times by the same object from constructors
84 # in different nclassdef
85 redef fun notify_of_creation(new_object)
86 do
87 var id = just_opened_id
88 if id == null then return # Register `new_object` only once
89 cache[id] = new_object
90 end
91
92 # Convert the simple JSON `object` to a Nit object
93 private fun convert_object(object: nullable Object, static_type: nullable String): nullable Object
94 do
95 if object isa JsonParseError then
96 errors.add object
97 return null
98 end
99
100 if object isa Map[String, nullable Object] then
101 var kind = null
102 if object.keys.has("__kind") then
103 kind = object["__kind"]
104 end
105
106 # ref?
107 if kind == "ref" then
108 if not object.keys.has("__id") then
109 errors.add new Error("Deserialization Error: JSON object reference does not declare a `__id`.")
110 return object
111 end
112
113 var id = object["__id"]
114 if not id isa Int then
115 errors.add new Error("Deserialization Error: JSON object reference declares a non-integer `__id`.")
116 return object
117 end
118
119 if not cache.has_id(id) then
120 errors.add new Error("Deserialization Error: JSON object reference has an unknown `__id`.")
121 return object
122 end
123
124 return cache.object_for(id)
125 end
126
127 # obj?
128 if kind == "obj" or kind == null then
129 var id = null
130 if object.keys.has("__id") then
131 id = object["__id"]
132
133 if not id isa Int then
134 errors.add new Error("Deserialization Error: JSON object declaration declares a non-integer `__id`.")
135 return object
136 end
137
138 if cache.has_id(id) then
139 errors.add new Error("Deserialization Error: JSON object with `__id` {id} is deserialized twice.")
140 # Keep going
141 end
142 end
143
144 var class_name = object.get_or_null("__class")
145 if class_name == null then
146 # Fallback to custom heuristic
147 class_name = class_name_heuristic(object)
148
149 if class_name == null and static_type != null then
150 # Fallack to the static type, strip the `nullable` prefix
151 class_name = static_type.strip_nullable
152 end
153 end
154
155 if class_name == null then
156 errors.add new Error("Deserialization Error: JSON object declaration does not declare a `__class`.")
157 return object
158 end
159
160 if not class_name isa String then
161 errors.add new Error("Deserialization Error: JSON object declaration declares a non-string `__class`.")
162 return object
163 end
164
165 if not accept(class_name, static_type) then return null
166
167 # advance on path
168 path.push object
169
170 just_opened_id = id
171 var value = deserialize_class(class_name)
172 just_opened_id = null
173
174 # revert on path
175 path.pop
176
177 return value
178 end
179
180 # char?
181 if kind == "char" then
182 if not object.keys.has("__val") then
183 errors.add new Error("Deserialization Error: JSON `char` object does not declare a `__val`.")
184 return object
185 end
186
187 var val = object["__val"]
188
189 if not val isa String or val.is_empty then
190 errors.add new Error("Deserialization Error: JSON `char` object does not declare a single char in `__val`.")
191 return object
192 end
193
194 return val.chars.first
195 end
196
197 # byte?
198 if kind == "byte" then
199 var val = object.get_or_null("__val")
200 if not val isa Int then
201 errors.add new Error("Serialization Error: JSON `byte` object does not declare an integer `__val`.")
202 return object
203 end
204
205 return val.to_b
206 end
207
208 errors.add new Error("Deserialization Error: JSON object has an unknown `__kind`.")
209 return object
210 end
211
212 # Simple JSON array without serialization metadata
213 if object isa Array[nullable Object] then
214 # Can we use the static type?
215 if static_type != null then
216 opened_array = object
217 var value = deserialize_class(static_type.strip_nullable)
218 opened_array = null
219 return value
220 end
221
222 # This branch should rarely be used:
223 # when an array is the root object which is accepted but illegal in standard JSON,
224 # or in strange custom deserialization hacks.
225
226 var array = new Array[nullable Object]
227 var types = new HashSet[String]
228 var has_nullable = false
229 for e in object do
230 var res = convert_object(e)
231 array.add res
232
233 if res != null then
234 types.add res.class_name
235 else has_nullable = true
236 end
237
238 if types.length == 1 then
239 var array_type = types.first
240
241 var typed_array
242 if array_type == "ASCIIFlatString" or array_type == "UnicodeFlatString" then
243 if has_nullable then
244 typed_array = new Array[nullable FlatString]
245 else typed_array = new Array[FlatString]
246 else if array_type == "Int" then
247 if has_nullable then
248 typed_array = new Array[nullable Int]
249 else typed_array = new Array[Int]
250 else if array_type == "Float" then
251 if has_nullable then
252 typed_array = new Array[nullable Float]
253 else typed_array = new Array[Float]
254 else
255 # TODO support all array types when we separate the constructor
256 # `from_deserializer` from the filling of the items.
257
258 if not has_nullable then
259 typed_array = new Array[Object]
260 else
261 # Unsupported array type, return as `Array[nullable Object]`
262 return array
263 end
264 end
265
266 assert typed_array isa Array[nullable Object]
267
268 # Copy item to the new array
269 for e in array do typed_array.add e
270 return typed_array
271 end
272
273 # Uninferrable type, return as `Array[nullable Object]`
274 return array
275 end
276
277 if object isa String and object.length == 1 and static_type == "Char" then
278 # Char serialized as a JSON string
279 return object.chars.first
280 end
281
282 if object isa Int and static_type == "Byte" then
283 # Byte serialized as an integer
284 return object.to_b
285 end
286
287 return object
288 end
289
290 # Current array open for deserialization, used by `SimpleCollection::from_deserializer`
291 private var opened_array: nullable Array[nullable Object] = null
292
293 redef fun deserialize(static_type)
294 do
295 errors.clear
296 return convert_object(root, static_type)
297 end
298
299 # User customizable heuristic to infer the name of the Nit class to deserialize `json_object`
300 #
301 # This method is called only when deserializing an object without the metadata `__class`.
302 # Use the content of `json_object` to identify what Nit class it should be deserialized into.
303 # Or use `self.attributes_path` indicating where the deserialized object will be stored,
304 # is is less reliable as some objects don't have an associated attribute:
305 # the root/first deserialized object and collection elements.
306 #
307 # Return the class name as a `String` when it can be inferred,
308 # or `null` when the class name cannot be found.
309 #
310 # If a valid class name is returned, `json_object` will then be deserialized normally.
311 # So it must contain the attributes of the corresponding class, as usual.
312 #
313 # ~~~
314 # class MyData
315 # serialize
316 #
317 # var data: String
318 # end
319 #
320 # class MyError
321 # serialize
322 #
323 # var error: String
324 # var related_data: MyData
325 # end
326 #
327 # class MyJsonDeserializer
328 # super JsonDeserializer
329 #
330 # redef fun class_name_heuristic(json_object)
331 # do
332 # # Infer the Nit class from the content of the JSON object.
333 # if json_object.keys.has("error") then return "MyError"
334 # if json_object.keys.has("data") then return "MyData"
335 #
336 # # Infer the Nit class from the attribute where it will be stored.
337 # # This line duplicates a previous line, and would only apply when
338 # # `MyData` is within a `MyError`.
339 # if attributes_path.not_empty and attributes_path.last == "related_data" then return "MyData"
340 #
341 # return null
342 # end
343 # end
344 #
345 # var json = """{"data": "some data"}"""
346 # var deserializer = new MyJsonDeserializer(json)
347 # var deserialized = deserializer.deserialize
348 # assert deserializer.errors.is_empty
349 # assert deserialized isa MyData
350 #
351 # json = """{"error": "some error message",
352 # "related_data": {"data": "some other data"}}"""
353 # deserializer = new MyJsonDeserializer(json)
354 # deserialized = deserializer.deserialize
355 # assert deserializer.errors.is_empty
356 # assert deserialized isa MyError
357 # ~~~
358 protected fun class_name_heuristic(json_object: Map[String, nullable Object]): nullable String
359 do
360 return null
361 end
362 end
363
364 redef class Text
365
366 # Deserialize a `nullable Object` from this JSON formatted string
367 #
368 # Warning: Deserialization errors are reported with `print_error` and
369 # may be returned as a partial object or as `null`.
370 #
371 # This method is not appropriate when errors need to be handled programmatically,
372 # manually use a `JsonDeserializer` in such cases.
373 fun from_json_string: nullable Object
374 do
375 var deserializer = new JsonDeserializer(self)
376 var res = deserializer.deserialize
377 if deserializer.errors.not_empty then
378 print_error "Deserialization Errors: {deserializer.errors.join(", ")}"
379 end
380 return res
381 end
382 end
383
384 redef class SimpleCollection[E]
385 redef init from_deserializer(v)
386 do
387 super
388 if v isa JsonDeserializer then
389 v.notify_of_creation self
390 init
391
392 var open_array: nullable SequenceRead[nullable Object] = v.opened_array
393 if open_array == null then
394 # With metadata
395 var arr = v.path.last.get_or_null("__items")
396 if not arr isa SequenceRead[nullable Object] then
397 # If there is nothing, we consider that it is an empty collection.
398 if arr != null then v.errors.add new Error("Deserialization Error: invalid format in {self.class_name}")
399 return
400 end
401 open_array = arr
402 end
403
404 # Name of the dynamic name of E
405 var items_type_name = (new GetName[E]).to_s
406
407 # Fill array
408 for o in open_array do
409 var obj = v.convert_object(o, items_type_name)
410 if obj isa E then
411 add obj
412 else v.errors.add new AttributeTypeError(self, "items", obj, items_type_name)
413 end
414 end
415 end
416 end
417
418 redef class Map[K, V]
419 redef init from_deserializer(v)
420 do
421 super
422
423 if v isa JsonDeserializer then
424 v.notify_of_creation self
425 init
426
427 var keys_type_name = (new GetName[K]).to_s
428 var values_type_name = (new GetName[V]).to_s
429
430 var length = v.deserialize_attribute("__length")
431 var keys = v.path.last.get_or_null("__keys")
432 var values = v.path.last.get_or_null("__values")
433
434 if keys == null and values == null then
435 # Fallback to a plain object
436 for key, value_src in v.path.last do
437
438 var value = v.convert_object(value_src, values_type_name)
439
440 if not key isa K then
441 v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
442 continue
443 end
444
445 if not value isa V then
446 v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
447 continue
448 end
449
450 self[key] = value
451 end
452 return
453 end
454
455 # Length is optional
456 if length == null and keys isa SequenceRead[nullable Object] then length = keys.length
457
458 # Consistency check
459 if not length isa Int or length < 0 or
460 not keys isa SequenceRead[nullable Object] or
461 not values isa SequenceRead[nullable Object] or
462 keys.length != values.length or length != keys.length then
463
464 # If there is nothing or length == 0, we consider that it is an empty Map.
465 if (length != null and length != 0) or keys != null or values != null then
466 v.errors.add new Error("Deserialization Error: invalid format in {self.class_name}")
467 end
468 return
469 end
470
471 # First, convert all keys to follow the order of the serialization
472 var converted_keys = new Array[K]
473 for i in length.times do
474 var key = v.convert_object(keys[i], keys_type_name)
475
476 if not key isa K then
477 v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
478 continue
479 end
480
481 converted_keys.add key
482 end
483
484 # Then convert the values and build the map
485 for i in length.times do
486 var key = converted_keys[i]
487 var value = v.convert_object(values[i], values_type_name)
488
489 if not value isa V then
490 v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
491 continue
492 end
493
494 if has_key(key) then
495 v.errors.add new Error("Deserialization Error: duplicated key '{key or else "null"}' in {self.class_name}, previous value overwritten")
496 end
497
498 self[key] = value
499 end
500 end
501 end
502 end
503
504 # ---
505 # Metamodel
506
507 # Class inheritance graph as a `POSet[String]` serialized to JSON
508 private fun class_inheritance_metamodel_json: CString is intern
509
510 redef class Sys
511 redef var class_inheritance_metamodel is lazy do
512 var engine = new JsonDeserializer(class_inheritance_metamodel_json.to_s)
513 engine.check_subtypes = false
514 engine.whitelist.add_all(
515 ["String", "POSet[String]", "POSetElement[String]",
516 "HashSet[String]", "HashMap[String, POSetElement[String]]"])
517
518 var poset = engine.deserialize
519 if engine.errors.not_empty then
520 print_error "Deserialization errors in class_inheritance_metamodel:"
521 print_error engine.errors.join("\n* ")
522 return new POSet[String]
523 end
524
525 if poset isa POSet[String] then return poset
526 return new POSet[String]
527 end
528 end