-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSerializationDemo.cs
46 lines (39 loc) · 1.88 KB
/
SerializationDemo.cs
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
using System;
using HelloAvro.DTO;
using Newtonsoft.Json;
namespace HelloAvro
{
/// <summary>
/// This class demonstrates how to perform just serialization and deserialization in Avro.
/// No RPC/IPC related demonstration here.
/// </summary>
class SerializationDemo
{
public void Run()
{
Console.WriteLine("Running the Avro serialization demo ...");
var employee = Helper.CreateEmployee();
// Serialization
var bytes = SerializerAvro.Serialize(employee);
Console.WriteLine("Serialized object to {0} bytes", bytes.Length);
Console.WriteLine("Bytes are: {0}", BitConverter.ToString(bytes));
// Deserialization
Console.WriteLine("Deserializing bytes back into object ... " + Environment.NewLine);
var regenerated = SerializerAvro.Deserialize<EmployeeDTO>(bytes);
// Verification : We compare original object with the object regenerated
// after passing through a serialize=>deserialize round trip.
// We compare the Json equivalent of this object to keep it simple and lazy
// and don't feel like implementing a proper 'Equals' method
// FYI, Json usage here has NOTHING to do with Avro serialization
var origJson = JsonConvert.SerializeObject(employee);
var regenJson = JsonConvert.SerializeObject(regenerated);
if (origJson.Equals(regenJson))
Console.WriteLine("Success. Object through the serialize=>deserialize round trip are identical.");
else
Console.WriteLine("FAILED! We lost data during the serialize=>deserialize round trip");
//Console.WriteLine(Environment.NewLine + "Press any key to exit ...");
//Console.ReadLine();
Console.WriteLine(Helper.HorizontalLine);
}
}
}