|
14 | 14 |
|
15 | 15 | ## Usage |
16 | 16 |
|
| 17 | +The BinaryFormatter class allow to serialize and deserialize to/from binary format. This allows a compact storage and, because it's implemented in native code, faster execution for serialization and deserialization operations. |
| 18 | + |
| 19 | +The only requirement is to decorate a class or it's fields with the `[Serializable]` attribute. |
| 20 | +Other attributes are available to provide hints to the serialization engine so the serialization data it's reduced as much as possible. More on this on the next section. |
| 21 | + |
| 22 | +> **Warning** the implementation of binary serialization for .NET **nanoFramework** is **NOT** compatible with the one of .NET Framework or .NET Core, menaning that it's not possible to use this to exchange data between the two frameworks. |
| 23 | +
|
| 24 | +### Serializing a class |
| 25 | + |
| 26 | +Follows a `Person` class that will be used in the following examples. |
| 27 | + |
| 28 | +```csharp |
| 29 | +[Serializable] |
| 30 | +public class Person |
| 31 | +{ |
| 32 | + public string FirstName { get; set; } |
| 33 | + public string LastName { get; set; } |
| 34 | + public string Address { get; set; } |
| 35 | + public DateTime Birthday { get; set; } |
| 36 | + public int ID { get; set; } |
| 37 | + public string[] ArrayProperty { get; set; } |
| 38 | + public Person Friend { get; set; } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +To serialize it, simply call the method `Serialize()` and pass the class instance. Like this: |
| 43 | + |
| 44 | +```csharp |
| 45 | +var nestedTestClass = new Person() |
| 46 | +{ |
| 47 | + FirstName = "John", |
| 48 | + LastName = "Doe", |
| 49 | + Birthday = new DateTime(1988, 4, 23), |
| 50 | + ID = 2700, |
| 51 | + Address = null, |
| 52 | + ArrayProperty = new string[] { "hello", "world" }, |
| 53 | + Friend = new Person() |
| 54 | + { |
| 55 | + FirstName = "Bob", |
| 56 | + LastName = "Smith", |
| 57 | + Birthday = new DateTime(1983, 7, 3), |
| 58 | + ID = 2000, |
| 59 | + Address = "123 Some St", |
| 60 | + ArrayProperty = new string[] { "hi", "planet" }, |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +byte[] serializedPerson = BinaryFormatter.Serialize(nestedTestClass); |
| 65 | +``` |
| 66 | + |
| 67 | +The `serializedPerson` byte array contains the binary representation for the `nestedTestClass`. |
| 68 | + |
| 69 | +### Deserializing a class |
| 70 | + |
| 71 | +In order to deserialize a class from it's binary representation to an instance of the class, call the `Deserialize()` method and pass the binary representation. |
| 72 | + |
| 73 | +```csharp |
| 74 | +[Serializable] |
| 75 | + |
| 76 | +Person anotherPerson = (Person)BinaryFormatter.Deserialize(serializedPerson); |
| 77 | +``` |
| 78 | + |
| 79 | +### Serialization hints |
| 80 | + |
| 81 | +There are optional attributes that can be used to provide hints to the serialization engine so the serialization data it's reduced as much as possible. |
| 82 | +Taking the `Person` class, follows the optimizations that are possible with the above example: |
| 83 | + |
| 84 | +```csharp |
| 85 | +[Serializable] |
| 86 | +public class Person |
| 87 | +{ |
| 88 | + public string FirstName { get; set; } |
| 89 | + public string LastName { get; set; } |
| 90 | + public string Address { get; set; } |
| 91 | + public DateTime Birthday { get; set; } |
| 92 | + [SerializationHints(RangeBias = 2000)] |
| 93 | + public int ID { get; set; } |
| 94 | + [SerializationHints(ArraySize = 2)] |
| 95 | + public string[] ArrayProperty { get; set; } |
| 96 | + public Person Friend { get; set; } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +The `SerializationHints` has several options to improve the data packing. |
| 101 | +Looking at the `ID` property above, which is of `int` type, without any optimization it takes 32bits to store. Now, this is used to store an ID which, let's assume for the sake of the example that only store IDs bigger than 2000. |
| 102 | +Using the RangeBias with a value of `2000` that value will be subtracted to the value being stored. |
| 103 | +In the code above, the ID with value 2700, would be serialized as (2700 - 2700 = 700) which can be stored as 16bits value instead of the 32bits that it would initially take. |
| 104 | + |
| 105 | +Another serialization hint is the array size. For the `ArrayProperty` let's assume that it will be always contain 2 elements. |
| 106 | +Decorating it with `ArraySize` and the size of the array, will store that information as part of the serialization data thus saving space that otherwise would be wasted with a generic count for the size of the array. |
| 107 | + |
17 | 108 | ## Feedback and documentation |
18 | 109 |
|
19 | 110 | For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home). |
|
0 commit comments