Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_json_deserialization_safe.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 import json
16
17 class A
18 serialize
19
20 var other: nullable A
21
22 type C: nullable A
23 var next: C
24
25 redef fun to_s do return "<{class_name} other:{other or else "null"} next:{next or else "null"}>"
26 end
27
28 class B
29 super A
30 serialize
31
32 redef type C: nullable B
33 end
34
35 class DangerSup
36 serialize
37
38 var dangerous_setter: String
39 end
40
41 class DangerSub
42 super DangerSup
43 serialize
44
45 redef fun dangerous_setter=(val)
46 do
47 print "DANGER '{val}'"
48 super
49 end
50 end
51
52 class G[E: A]
53 serialize
54 var e: E
55
56 redef fun to_s do return "<{class_name} e:{if isset _e then e.to_s else "not-set"}>"
57 end
58
59 redef class Deserializer
60 redef fun deserialize_class(name)
61 do
62 if name == "G[A]" then return new G[A].from_deserializer(self)
63 if name == "G[B]" then return new G[B].from_deserializer(self)
64 return super
65 end
66 end
67
68 var json = """{
69 "__class": "A",
70 "other": {"__class": "DangerSub", "dangerous_setter": "My text 1"},
71 "next": {"__class": "DangerSub", "dangerous_setter": "My text 2"}
72 }"""
73
74 # OK, accept only valid types, so don't try to deserialize DangerSub
75 var deserializer = new JsonDeserializer(json)
76 var res = deserializer.deserialize
77 print deserializer.errors.join("\n")
78 print res or else "null"
79
80 print "---"
81
82 # Accept any types, so deserialize the dangerous classes
83 deserializer = new JsonDeserializer(json)
84 deserializer.check_subtypes = false
85 res = deserializer.deserialize
86 print deserializer.errors.join("\n")
87 print res or else "null"
88
89 print "---"
90
91 # Valid virtual type for `next: B`
92 json = """{"__class": "B", "next": {"__class": "B"}}"""
93 deserializer = new JsonDeserializer(json)
94 res = deserializer.deserialize
95 print deserializer.errors.join("\n")
96 print res or else "null"
97
98 print "---"
99
100 # Virtual type error for `next: B`
101 json = """{"__class": "B", "next": {"__class": "A"}}"""
102 deserializer = new JsonDeserializer(json)
103 res = deserializer.deserialize
104 print deserializer.errors.join("\n")
105 print res or else "null"
106
107 print "---"
108
109 # Valid parameter type for `e: A`
110 json = """{"__class": "G[A]", "e": {"__class": "A"}}"""
111 deserializer = new JsonDeserializer(json)
112 res = deserializer.deserialize
113 print deserializer.errors.join("\n")
114 print res or else "null"
115
116 print "---"
117
118 # Parameter type error for `e: B`
119 json = """{"__class": "G[B]", "e": {"__class": "A"}}"""
120 deserializer = new JsonDeserializer(json)
121 res = deserializer.deserialize
122 print deserializer.errors.join("\n")
123 print res or else "null"