Merge: doc: fixed some typos and other misc. corrections
[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
21
22 # Simple class
23 class A
24 serialize
25 #alt5# serialize
26
27 var b: Bool
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: Int
33 var s: String
34 var n: nullable Int
35 var array = new Array[nullable Object].with_items(88, "hello", null)
36
37 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null}>"
38 end
39
40 # Sub-class of A
41 class B
42 auto_serializable#alt2##alt3#
43 #alt2# noserialize
44 #alt3# noserialize
45 super A
46
47 var ii: Int
48 var ss: String
49
50 redef fun to_s do return "<B: {super} {ii} {ss}>"
51 end
52
53 # Composed of an A and a B
54 class C
55 auto_serializable
56
57 var a: A
58 var b: B
59 var aa: A
60
61 redef fun to_s do return "<C: {a} {b}>"
62 end
63
64 # Class with cyclic reference
65 class D
66 auto_serializable
67 super B
68
69 var d: nullable D = null
70
71 redef fun to_s do return "<D: {super} {d != null}>"
72 end
73
74 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
75 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
76 var c = new C(a, b, a)
77 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
78 d.d = d
79
80 for o in new Array[Serializable].with_items(a, b, c, d) do
81 var stream = new StringWriter
82 var serializer = new JsonSerializer(stream)
83 #alt1#serializer.plain_json = true
84 serializer.serialize(o)
85
86 print "# Nit:\n{o}\n"
87 print "# Json:\n{stream}\n"
88 end