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