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.
23 redef fun to_s
do return "<MyData {data}>"
31 redef fun to_s
do return "<MyError {error}>"
34 class MyJsonDeserializer
35 super JsonDeserializer
37 redef fun class_name_heuristic
(json_object
)
39 if json_object
.keys
.has
("error") then return "MyError"
40 if json_object
.keys
.has
("data") then return "MyData"
45 var tests
= new Array[String]
47 # Object with meta data, does not need the heuristic
49 {"__class": "MyData", "data": "some data"}"""
51 # Objects without meta data
53 {"data": "some other data"}"""
55 {"error": "some error message"}"""
58 var deserializer
= new MyJsonDeserializer(o
)
59 var deserialized
= deserializer
.deserialize
62 if deserializer
.errors
.not_empty
then print
"# Errors: '{deserializer.errors.join("', '")}'"
63 print
"# Nit: {deserialized or else "null"}\n"