You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Serializer call includes two `static` methods that allow you serialize (nearly) any Squirrel object into a blob, and deserialize perviously serialized objects. This is particulairly useful if you're planning to store information with [hardware.spiflash](https://electricimp.com/docs/api/hardware/spiflash) or the with the [SPIFlash Library](https://github.com/electricimp/spiflash/tree/v1.0.0).
4
+
5
+
*NOTE:* The *Serializer* class only uses `static` methods, and as a result does not to be initialized through a constructor.
6
+
7
+
**To add this library to your project, add `#require "Serializer.class.nut:1.0.0"`` to the top of your device code.**
8
+
9
+
You can view the library’s source code on [GitHub](https://github.com/electricimp/serializer/tree/v1.0.0).
10
+
11
+
## Serializable Squirrel
12
+
13
+
The Serializer class currently supports the following types:
If a *prefix* was passed to the method, the Serializer will write this data at the beginning of the blob. Immediatly proceeding the prefix data, the Serializer will write 3 bytes of header information: a 16-bit unsigned integer representing the length of the serialized data, and an 8-bit unsigned integer representing a CRC.
59
+
60
+
| Byte | Description |
61
+
| ---- | ---------------------------- |
62
+
| 0 | The lower byte of the length |
63
+
| 1 | The upper byte of the length |
64
+
| 2 | The CRC byte |
65
+
66
+
**NOTE:** The 16-but length value does include the length of the prefix (if included) or the header data (3 bytes).
The *Serializer.deserialize* method will deserialize a blob that was previous serialized with the *Serializer.serialize* method. If the blob was serialized with a *prefix*, the same *prefix* must be passed into the *Serializer.deserialize* method.
71
+
72
+
```squirrel
73
+
# require "Serializer.class.nut:1.0.0"
74
+
75
+
// Setup SpiFlash object
76
+
// ...
77
+
78
+
spiFlash.enable();
79
+
80
+
// Read the header information
81
+
local dataBlob = spiFlash.read(0x00, 3);
82
+
// Get the length from the first two bytes
83
+
local len = dataBlob.readn('w');
84
+
85
+
// Move to the end of the blob
86
+
dataBlob.seek(0, 'e');
87
+
88
+
// Read the length of the data starting at the end of the header
89
+
spiFlash.readintoblob(0x03, dataBlob, len);
90
+
91
+
// Disable the SPIFlash since we're done
92
+
spiFlash.disable();
93
+
94
+
// Deserialize the blob
95
+
local data = Serializer.deserialize(dataBlob);
96
+
97
+
98
+
99
+
// Log some data to make sure it worked:
100
+
server.log(data.foo); // bar
101
+
server.log(data.otherData.state); // true
102
+
server.log(data.otherData.test); // test
103
+
104
+
server.log("Readings:");
105
+
for(local i = 0; i < data.timestamps.len(); i++) {
The *Serializer* class needs to add a variety of metadata to Serialized objects in order to properly know how to deserialize the blobs. The *Serializer.sizeof* method can be used to quickly determin the size of an object after serialization.
0 commit comments