compiler: handle multi-iterators
[nit.git] / tests / test_serialization.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 #alt2#module test_serialization_alt2 is serialize
18
19 import serialization
20 import json::serialization
21
22 # Simple class
23 class A
24 serialize
25 #alt5# serialize
26
27 var b = false
28 var c: Char#alt2#
29 #alt2#var c: Char is noserialize
30 var f: Float#alt4#
31 #alt4#var f: Float is serialize
32 var i = 123
33 var s = "asdf"
34 var n: nullable Int
35 var array = new Array[nullable Object].with_items(88, "hello", null)
36
37 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int)
38 do
39 self.b = b
40 self.c = c
41 self.f = f
42 self.i = i
43 self.s = s
44 end
45
46 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null}>"
47 end
48
49 # Sub-class of A
50 class B
51 auto_serializable#alt2##alt3#
52 #alt2# noserialize
53 #alt3# noserialize
54 super A
55
56 var ii: Int
57 var ss: String
58
59 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int, ii: Int, ss: String)
60 do
61 super(b, c, f, i, s, n)
62
63 self.ii = ii
64 self.ss = ss
65 end
66
67 redef fun to_s do return "<B: {super} {ii} {ss}>"
68 end
69
70 # Composed of an A and a B
71 class C
72 auto_serializable
73
74 var a: A
75 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
76 var aa: A
77
78 init(a: A, b: B)
79 do
80 self.a = a
81 self.b = b
82 self.aa = a
83 end
84
85 redef fun to_s do return "<C: {a} {b}>"
86 end
87
88 # Class with cyclic reference
89 class D
90 auto_serializable
91 super B
92
93 var d: nullable D = null
94
95 redef fun to_s do return "<D: {super} {d != null}>"
96 end
97
98 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
99 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
100 var c = new C(a, b)
101 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
102 d.d = d
103
104 for o in new Array[Serializable].with_items(a, b, c, d) do
105 var stream = new StringWriter
106 var serializer = new JsonSerializer(stream)
107 #alt1#serializer.plain_json = true
108 serializer.serialize(o)
109
110 print "# Nit:\n{o}\n"
111 print "# Json:\n{stream}\n"
112 end