json: rename and improve the quick and easy `deserialize_json`
[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: `deserialize_json` 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 # If a `static_type` is given, only subtypes of the `static_type` are accepted.
368 #
369 # Warning: Deserialization errors are reported with `print_error` and
370 # may be returned as a partial object or as `null`.
371 #
372 # This method is not appropriate when errors need to be handled programmatically,
373 # manually use a `JsonDeserializer` in such cases.
374 fun deserialize_json(static_type: nullable String): nullable Object
375 do
376 var deserializer = new JsonDeserializer(self)
377 var res = deserializer.deserialize(static_type)
378 if deserializer.errors.not_empty then
379 print_error "Deserialization Errors: {deserializer.errors.join(", ")}"
380 end
381 return res
382 end
383 end
384
385 redef class SimpleCollection[E]
386 redef init from_deserializer(v)
387 do
388 super
389 if v isa JsonDeserializer then
390 v.notify_of_creation self
391 init
392
393 var open_array: nullable SequenceRead[nullable Object] = v.opened_array
394 if open_array == null then
395 # With metadata
396 var arr = v.path.last.get_or_null("__items")
397 if not arr isa SequenceRead[nullable Object] then
398 # If there is nothing, we consider that it is an empty collection.
399 if arr != null then v.errors.add new Error("Deserialization Error: invalid format in {self.class_name}")
400 return
401 end
402 open_array = arr
403 end
404
405 # Name of the dynamic name of E
406 var items_type_name = (new GetName[E]).to_s
407
408 # Fill array
409 for o in open_array do
410 var obj = v.convert_object(o, items_type_name)
411 if obj isa E then
412 add obj
413 else v.errors.add new AttributeTypeError(self, "items", obj, items_type_name)
414 end
415 end
416 end
417 end
418
419 redef class Map[K, V]
420 redef init from_deserializer(v)
421 do
422 super
423
424 if v isa JsonDeserializer then
425 v.notify_of_creation self
426 init
427
428 var keys_type_name = (new GetName[K]).to_s
429 var values_type_name = (new GetName[V]).to_s
430
431 var length = v.deserialize_attribute("__length")
432 var keys = v.path.last.get_or_null("__keys")
433 var values = v.path.last.get_or_null("__values")
434
435 if keys == null and values == null then
436 # Fallback to a plain object
437 for key, value_src in v.path.last do
438
439 var value = v.convert_object(value_src, values_type_name)
440
441 if not key isa K then
442 v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
443 continue
444 end
445
446 if not value isa V then
447 v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
448 continue
449 end
450
451 self[key] = value
452 end
453 return
454 end
455
456 # Length is optional
457 if length == null and keys isa SequenceRead[nullable Object] then length = keys.length
458
459 # Consistency check
460 if not length isa Int or length < 0 or
461 not keys isa SequenceRead[nullable Object] or
462 not values isa SequenceRead[nullable Object] or
463 keys.length != values.length or length != keys.length then
464
465 # If there is nothing or length == 0, we consider that it is an empty Map.
466 if (length != null and length != 0) or keys != null or values != null then
467 v.errors.add new Error("Deserialization Error: invalid format in {self.class_name}")
468 end
469 return
470 end
471
472 # First, convert all keys to follow the order of the serialization
473 var converted_keys = new Array[K]
474 for i in length.times do
475 var key = v.convert_object(keys[i], keys_type_name)
476
477 if not key isa K then
478 v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
479 continue
480 end
481
482 converted_keys.add key
483 end
484
485 # Then convert the values and build the map
486 for i in length.times do
487 var key = converted_keys[i]
488 var value = v.convert_object(values[i], values_type_name)
489
490 if not value isa V then
491 v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
492 continue
493 end
494
495 if has_key(key) then
496 v.errors.add new Error("Deserialization Error: duplicated key '{key or else "null"}' in {self.class_name}, previous value overwritten")
497 end
498
499 self[key] = value
500 end
501 end
502 end
503 end
504
505 # ---
506 # Metamodel
507
508 # Class inheritance graph as a `POSet[String]` serialized to JSON
509 private fun class_inheritance_metamodel_json: CString is intern
510
511 redef class Sys
512 redef var class_inheritance_metamodel is lazy do
513 var engine = new JsonDeserializer(class_inheritance_metamodel_json.to_s)
514 engine.check_subtypes = false
515 engine.whitelist.add_all(
516 ["String", "POSet[String]", "POSetElement[String]",
517 "HashSet[String]", "HashMap[String, POSetElement[String]]"])
518
519 var poset = engine.deserialize
520 if engine.errors.not_empty then
521 print_error "Deserialization errors in class_inheritance_metamodel:"
522 print_error engine.errors.join("\n* ")
523 return new POSet[String]
524 end
525
526 if poset isa POSet[String] then return poset
527 return new POSet[String]
528 end
529 end