Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_msgpack_deserialization.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # alt0 non-generics plain
16 # alt1 non-generics metadata
17 # alt2 generics plain
18 # alt3 generics metadata
19 # alt4 generics metadata cache_metadata_strings
20
21 import test_deserialization
22 import msgpack
23 import msgpack::read
24 import json
25 #alt2# import test_deserialization_serial
26 #alt3# import test_deserialization_serial
27 #alt4# import test_deserialization_serial
28
29 var entities = new TestEntities
30
31 var tests = entities.without_generics
32 #alt2#tests = entities.with_generics
33 #alt3#tests = entities.with_generics
34 #alt4#tests = entities.with_generics
35
36 for o in tests do
37 var plain = true
38 #alt1#plain = false
39 #alt3#plain = false
40 #alt4#plain = false
41
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
48
49 # Nit source
50 print "# 1. Nit source:\n{o}\n"
51
52 # Generated MessagePack
53 print "# 2. MsgPack:\n{bytes.chexdigest}\n"
54
55 # Python deserialization line to ensure compliance and otherwise debug
56 #print "msgpack.unpackb(b'{bytes.chexdigest}')"
57
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)
62 else "null"
63 print "# 3. JSON:\n{json}\n"
64
65 # Deserialize
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"
70 else
71 var deserialized = deserializer.deserialize
72 print "# 4. Back in Nit (with metadata):\n{deserialized or else "null"}\n"
73 end
74 end