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.
15 # alt0 non-generics plain
16 # alt1 non-generics metadata
18 # alt3 generics metadata
19 # alt4 generics metadata cache_metadata_strings
21 import test_deserialization
25 #alt2# import test_deserialization_serial
26 #alt3# import test_deserialization_serial
27 #alt4# import test_deserialization_serial
29 var entities
= new TestEntities
31 var tests
= entities
.without_generics
32 #alt2#tests = entities.with_generics
33 #alt3#tests = entities.with_generics
34 #alt4#tests = entities.with_generics
42 var writer
= new BytesWriter
43 var serializer
= new MsgPackSerializer(writer
)
44 serializer
.plain_msgpack
= plain
45 #alt4#serializer.cache_metadata_strings = true
46 serializer
.serialize o
47 var bytes
= writer
.bytes
50 print
"# 1. Nit source:\n{o}\n"
52 # Generated MessagePack
53 print
"# 2. MsgPack:\n{bytes.chexdigest}\n"
55 # Python deserialization line to ensure compliance and otherwise debug
56 #print "msgpack.unpackb(b'{bytes.chexdigest}')"
58 # Show readable structure of the generated MessagePack as JSON
59 var msg
= (new BytesReader(bytes
)).read_msgpack
60 var json
= if msg
!= null then
61 msg
.serialize_to_json
(plain
=true, pretty
=true)
63 print
"# 3. JSON:\n{json}\n"
66 var deserializer
= new MsgPackDeserializer(new BytesReader(bytes
))
67 if serializer
.plain_msgpack
then
68 var deserialized
= deserializer
.deserialize
(o
.class_name
)
69 print
"# 4. Back in Nit (no metadata):\n{deserialized or else "null"}\n"
71 var deserialized
= deserializer
.deserialize
72 print
"# 4. Back in Nit (with metadata):\n{deserialized or else "null"}\n"