-
Notifications
You must be signed in to change notification settings - Fork 128
/
lesson11.slide
392 lines (183 loc) · 6.75 KB
/
lesson11.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
Marshalling and unmarshalling, configuration files
Lesson 11
13 Dec 2024
Tags: golang, go
Pavel Tišnovský
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
@RedHat
* Sources
- [[https://github.com/RedHatOfficial/GoCourse]]
.image ./common/qr_address.png
* Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/fiveyears.jpg
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/fiveyears.jpg _ 900
* Marshalling and unmarshalling
- process of transforming the memory representation of an object into a data format suitable for storage or transmission
- unmarshalling (un-marshalling) performs opposite operation
- allows communication between client and server
- intermediate (serialized) object representation
- marshalling vs serialization (we use serialization in order to implement marshalling)
* Data formats
- XML
- JSON
- JAXB
- gob
- CBOR
- BSON
- MessagePack
- https://en.wikipedia.org/wiki/Comparison_of_data-serialization_formats
* Marshalling in Go
- package `encoding`
- encodes the receiver into a binary form and returns the result
.code lesson11/BinaryMarshaler.go
- encodes the receiver into UTF-8-encoded text and returns the result
.code lesson11/TextMarshaler.go
- can be implemented by any structure (where needed)
* Unmarshalling in Go
- package `encoding` as well
- must be able to decode the form generated by MarshalBinary
.code lesson11/BinaryUnmarshaler.go
- must be able to decode the form generated by MarshalText
.code lesson11/TextUnmarshaler.go
- can be implemented by any structure (where needed)
* Why BinaryMarshaler and BinaryUnmarshaler
- "universal" container for moving data around
.code lesson11/Reader.go
.code lesson11/Writer.go
* JSON
- `encoding/json` package
.code lesson11/JsonMarshal.go
.code lesson11/JsonMarshalIndent.go
.code lesson11/JsonUnmarshal.go
* JSON marshaling
.play lesson11/json_marshal_struct.go
* Indenting the JSON output
.play lesson11/json_marshal_struct_indent.go
* Attribute names in JSON
.play lesson11/json_marshal_struct_names.go
* Skip attribute(s)
.play lesson11/json_marshal_struct_names_skip.go
* Marshaling array
.play lesson11/json_marshal_array.go
* Array of any values
- `any` is an alias for `interface{}`
.play lesson11/json_marshal_array_any.go
* Map
.play lesson11/json_marshal_map.go
* Floating point numbers
.play lesson11/json_marshal_floats.go
* Floating point special values 1/2
.play lesson11/json_marshal_floats_specvalues.go /^package main/,/^func main/
* Floating point special values 2/2
.play lesson11/json_marshal_floats_specvalues.go /^func main/,/^}/
* Floating point special values with error checks 1/2
.play lesson11/json_marshal_floats_specvalues_err.go /^package main/,/^func main/
* Floating point special values with error checks 2/2
.play lesson11/json_marshal_floats_specvalues_err.go /^func main/,/^}/
* Unmarshalling 1/2
.play lesson11/json_unmarshal.go /^package main/,/^func main/
* Unmarshalling 2/2
.play lesson11/json_unmarshal.go /^func main/,/^}/
* Unmarshalling with error checking
.play lesson11/json_unmarshal_error.go
* Unmarshalling struct with lowercase item 1/2
.play lesson11/json_unmarshal_lowercase.go /^package main/,/^func main/
* Unmarshalling struct with lowercase item 2/2
.play lesson11/json_unmarshal_lowercase.go /^func main/,/^}/
* XML
- `encoding/xml` package
- attributes -> nodes or attributes
- root node name specification
- sub-nodes support
.code lesson11/XMLMarshal.go
.code lesson11/XMLMarshalIndent.go
.code lesson11/XMLUnmarshal.go
* XML marshalling
.play lesson11/xml_marshal_struct.go
* Indenting
.play lesson11/xml_marshal_struct_indent.go
* Specific node names
.play lesson11/xml_marshal_struct_names.go
* Root node name specification
.play lesson11/xml_marshal_xml_name.go
* XML and Go: advanced features
* XML and JSON specifiers 1/2
.play lesson11/xml_and_json.go /^package main/,/^func main/
* XML and JSON specifiers 2/2
.play lesson11/xml_and_json.go /^func main/,/^}/
* Controlling XML tree shape: subnodes
.play lesson11/xml_marshal_control_1.go
* Controlling XML tree shape: attributes
.play lesson11/xml_marshal_control_2.go
* Controlling XML tree shape: combination
.play lesson11/xml_marshal_control_3.go
* Special types and values in XML 1/2
.play lesson11/xml_marshal_special_types_1.go /^package main/,/^func main/
* Special types and values in XML 2/2
.play lesson11/xml_marshal_special_types_1.go /^func main/,/^}/
* Nested structures in XML 1/2
.play lesson11/xml_marshal_special_types_2.go /^package main/,/^func main/
* Nested structures in XML 2/2
.play lesson11/xml_marshal_special_types_2.go /^func main/,/^}/
* MessagePack
- binary format
- efficient data storage
- objects can be stored as maps
- true arrays
* MessagePack: supported data types
- Nil (null, None)
- Boolean
- Integer (small ones stored efficently)
- Float (single, double) incl. Nan + Infinity
- String
- Binary (byte array)
- Array (sequence of objects)
- Map (sequence of key-value pairs)
- Extension
* MessagePack: serializing `nil`
.play lesson11/msgpack_nil.go /^package main/,/^func main/
* cont.
.play lesson11/msgpack_nil.go /^func main/,/^}/
* MessagePack: serializing small integer
.play lesson11/msgpack_small_int.go /^func main/,/^}/
* MessagePack: serializing single float
.play lesson11/msgpack_single.go /^func main/,/^}/
* MessagePack: serializing a map
.play lesson11/msgpack_map.go /^func main/,/^}/
* MessagePack: serializing map of any 1/2
.play lesson11/msgpack_map2.go /^func main/,/Encoder created/
* MessagePack: serializing map of any 2/2
.play lesson11/msgpack_map2.go /Encoder created/,/^}/
* gob
- Gobs of data
- based on reflection
- very efficient (size+speed)
- built-in into Go standard library
* gob marshalling 1/2
.play lesson11/gob_marshal_struct.go /^package/,/^func main/
* gob marshalling 2/2
.play lesson11/gob_marshal_struct.go /^func main/,/^}/
* BSON
- "binary JSON"
* BSON serialization 1/2
.play lesson11/bson_serialize.go /^package/,/^func main/
* BSON serialization 2/2
.play lesson11/bson_serialize.go /^func main/,/^}/
* BSON deserialization 1/2
.play lesson11/bson_deserialize.go /^package/,/^func main/
* BSON deserialization 2/2
.play lesson11/bson_deserialize.go /^func main/,/^}/
* Size comparisons
.play lesson11/size_comparison.go
* Size comparisons
.code lesson11/sizes.txt
#last slide
* More Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/bumper.png
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/bumper.png _ 900