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.
25 redef fun to_s
do return "<{class_name} other:{other or else "null"} next:{next or else "null"}>"
32 redef type C
: nullable B
38 var dangerous_setter
: String
45 redef fun dangerous_setter
=(val
)
47 print
"DANGER '{val}'"
56 redef fun to_s
do return "<{class_name} e:{if isset _e then e.to_s else "not-set"}>"
59 redef class Deserializer
60 redef fun deserialize_class
(name
)
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)
70 "other": {"__class": "DangerSub", "dangerous_setter": "My text 1"},
71 "next": {"__class": "DangerSub", "dangerous_setter": "My text 2"}
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"
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"
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"
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"
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"
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"