1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 #alt2#module test_serialization_alt2 is serialize
20 import json
::serialization
29 #alt2#var c: Char is noserialize
31 #alt4#var f: Float is serialize
35 var array
= new Array[nullable Object].with_items
(88, "hello", null)
37 init(b
: Bool, c
: Char, f
: Float, i
: Int, s
: String, n
: nullable Int)
46 redef fun to_s
do return "<A: {b} {c} {f} {i} {s} {n != null}>"
51 auto_serializable
#alt2##alt3#
59 init(b
: Bool, c
: Char, f
: Float, i
: Int, s
: String, n
: nullable Int, ii
: Int, ss
: String)
61 super(b
, c
, f
, i
, s
, n
)
67 redef fun to_s
do return "<B: {super} {ii} {ss}>"
70 # Composed of an A and a B
75 var b
= new B
(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
85 redef fun to_s
do return "<C: {a} {b}>"
88 # Class with cyclic reference
93 var d
: nullable D
= null
95 redef fun to_s
do return "<D: {super} {d != null}>"
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")
101 var d
= new D
(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\
/")
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)
110 print "# Nit:\n{o}\n"
111 print
"# Json:\n{stream}\n"