Rename REAMDE to README.md
[nit.git] / tests / test_deserialization.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import serialization
18 import json_serialization
19
20 # Simple class
21 class A
22 auto_serializable
23
24 var b = false
25 var c: Char
26 var f: Float
27 var i = 123
28 var s = "asdf"
29 var n: nullable Int
30
31 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int)
32 do
33 self.b = b
34 self.c = c
35 self.f = f
36 self.i = i
37 self.s = s
38 end
39
40 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null}>"
41 end
42
43 # Sub-class of A
44 class B
45 auto_serializable
46 super A
47
48 var ii: Int
49 var ss: String
50
51 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int, ii: Int, ss: String)
52 do
53 super(b, c, f, i, s, n)
54
55 self.ii = ii
56 self.ss = ss
57 end
58
59 redef fun to_s do return "<B: {super} {ii} {ss}>"
60 end
61
62 # Composed of an A and a B
63 class C
64 auto_serializable
65
66 var a: A
67 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
68 var aa: A
69
70 init(a: A, b: B)
71 do
72 self.a = a
73 self.b = b
74 self.aa = a
75 end
76
77 redef fun to_s do return "<C: {a} {b}>"
78 end
79
80 # Class with cyclic reference
81 class D
82 auto_serializable
83 super B
84
85 var d: nullable D = null
86
87 redef fun to_s do return "<D: {super} {d != null}>"
88 end
89
90 # Class with generics
91 class E
92 auto_serializable
93
94 var a = new Array[Object].with_items("hello", 1234, 123.4)
95 var b = new Array[nullable Serializable].with_items("hella", 2345, 234.5)
96 init do end
97
98 redef fun to_s do return "<E: a: {a.join(", ")}; b: {b.join(", ")}>"
99 end
100
101 # Parameterized class
102 class F[N: Numeric]
103 auto_serializable
104
105 var n: N
106 init(n: N) do self.n = n
107
108 redef fun to_s do return "<E: {n}>"
109 end
110
111 # Other collections
112 class G
113 auto_serializable
114
115 var hs = new HashSet[Int]
116 var s: Set[String] = new ArraySet[String]
117 var hm = new HashMap[String, Int]
118 var am = new ArrayMap[String, String]
119
120 init
121 do
122 hs.add -1
123 hs.add 0
124 s.add "one"
125 s.add "two"
126 hm["one"] = 1
127 hm["two"] = 2
128 am["three"] = 3.to_s
129 am["four"] = 4.to_s
130 end
131
132 redef fun to_s do return "<G: hs: {hs.join(", ")}; s: {s.join(", ")}; "+
133 "hm: {hm.join(", ", ". ")}; am: {am.join(", ", ". ")}>"
134 end
135
136 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
137 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
138 var c = new C(a, b)
139 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
140 d.d = d
141 var e = new E
142 var fi = new F[Int](2222)
143 var ff = new F[Float](33.33)
144 var g = new G
145
146 # Default works only with Nit serial
147 var tests = [a, b, c, d, e, fi, ff, g: Serializable]
148
149 # Alt1 should work without nitserial
150 #alt1# tests = new Array[Serializable].with_items(a, b, c, d)
151
152 for o in tests do
153 var stream = new StringWriter
154 var serializer = new JsonSerializer(stream)
155 serializer.serialize(o)
156
157 var deserializer = new JsonDeserializer(stream.to_s)
158 var deserialized = deserializer.deserialize
159
160 print "# Nit:\n{o}\n"
161 print "# Json:\n{stream}\n"
162 print "# Back in Nit:\n{deserialized or else "null"}\n"
163 end