Hello, Nim community!
I'd like to introduce you my little serialization library that allows to
perform [de]serialization of objects in two steps:
Step 1: annotate your object as **serializable** or convert it to
**serializable** via **toSerializable** macro
serializable:
type MyObject = object
my_string: string
my_int: int16
Step 2: use special procedures generated by the macro in your code
let testObject = MyObject(my_string: "Hello, world!", my_int: 1024)
var targetStream = newStringStream()
testObject.serialize(targetStream) # serialize testObject and put result to
targetStream
...
targetStream.setPosition(0) # reset the stream position to read from
begining
let targetObject = MyObject.deserialize(targetStream) # read data from
targetStream and represent it as MyObject
More examples can be found in the
[documentation](https://xomachine.github.io/NESM/) and
[tests](https://github.com/xomachine/NESM/tree/master/tests).
The serialization is being performed in back-to-back style without any gaps.
Serialized data is not human readable due to using actual representation of
basic types in memory.
For those who is already familiar with NESM here goes a list of changes that
have been done since the library first appearance in the nimble package list.
* **Changed syntax of serialization settings**
The previous syntax caused compilation error when applied to tuples, so it has
replaced to this:
serializable:
type TestTuple = tuple
set: {endian: bigEndian} # here we set endian to bigEndian
testValue: seq[int32]
set: bool # name 'set' still can be used as a field name
* **Added support of enums and sets**
After first version released I'd got an request for enum support. So here it
come. The sets support is a bonus =)
* **Possibility to convert existing types to serializables**
It may be useful to avoid reinventing types that already exist in standart
library.
* **Optional size definition for basic types**
In most cases the previous change would be useless because almost all standart
library types have no size specifiers (int32 vs int) and can not be converted
to serializables. To make a situation a bit better the
**-d:allow_undefined_type_size** compiler switch was added. This switch
disables strict checking of size specifier and allows the size to be equal to
**<type>.sizeof**. So the NESM can not guarantee anymore that on different
architectures the object will be [de]serialized in proper way. Use this switch
on your own risk.
* * *
There are also a few other changes and bugfixes. The latest version for a now
is [v0.3.1](https://github.com/xomachine/NESM/releases/tag/v0.3.1).