0.0.20 • Published 5 years ago

byte-serializer v0.0.20

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

byte-serializer

Support Node of LTS npm version Build passing dependencies rf24 nan License mit


Description

The main purpose of this library is create a small and reusable serialization engine able to manage serial message protocol. The use of property decorators has allowed to reduce serialization/deserialization to a loop around properties metadata obtained at initialization time. The module has two model: one for data and another one for messages. Data model is made to allow data (de)serialization, message module to allow to send/receive datas. Both work with buffer (byte array) and Message model is a specialization of Data model.

How to use

Install dependencies:

The main dependencie of this module is typescript compiler tsc.

npm install -g typescript

Import in node js

Include the lib in your project simply run:

npm install byte-serializer

Use in your project

After the library were added in your project, you have to import some modules dependencies:

  • TextEncoding: enum
  • NumberType: enum
  • BitOrder: enum
  • CrcLength: enum
  • PropertyType: enum
  • CRC: interface
  • Serializable: abstract class
  • Message: abstract class
  • MessageInfo: decorators module
  • SerializerInfo: decorators module

To create a serializable payload you have to extends imported type serializable and then use decorators contained in SerializerInfo to define position, length, type and data specification of properties you have inside.

A serializable payload is just an object that can be serialized in a byte array (buffer). If you want to send, or receive, the paylod you have to add some metadatas such as start byte, expected length (or just length), id, crc and abviously data. You can choose to add a end byte to mark the end of message. For this pourpose you have to extend abstract class Message.

Define a payload

import {Serializable, SerializerInfo, BitOrder, NumberType, TextEncoding} from 'byte-serializer'

export class DataExample extends Serializable {
    @SerializerInfo.position(0)
    @SerializerInfo.length(4)
    @SerializerInfo.bitOrder(BitOrder.BE)
    @SerializerInfo.numberType(NumberType.Int32)
    public Pippo:number;

    @SerializerInfo.position(4)
    @SerializerInfo.length(2)
    @SerializerInfo.bitOrder(BitOrder.BE)
    @SerializerInfo.numberType(NumberType.Int16)
    public Pluto :number;

    @SerializerInfo.position(6)
    @SerializerInfo.length(10)
    @SerializerInfo.textEncoding(TextEncoding.ASCII)
    public Text :string;
}

Use the constructor to initialize data.

Create a message

let data = new DataExample();
data.Number1 = 50;
data.Number2 = 2000;
data.Text = "A long string"; // More the 10 chars

Serialize a message

let payload = data.serialize();

Deserialize a message

let newData = new DataExample();
newData.deserialize(payload);
0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago