Installation
npm i ezmsg-node
Features
- Tiny (~1KB minified and gzipped).
- Ready to use with Typescript (this library is written in Typescript actually).
Basic usage
import { BType, serialize, deserialize } from 'ezmsg-node';
const schema = {
id: BType.U8,
name: BType.STR,
age: BType.U8,
isMarried: BType.BOOL
};
const data = {
id: 1,
name: 'John',
age: 28,
isMarried: false
}
const buffer = serialize(data, schema);
const result = deserialize(buffer, schema);
Array and Object type
import { BType, serialize, deserialize } from 'ezmsg-web';
const foodSchema = {
id: BType.U8,
name: BType.STR,
price: BType.U32
};
const schema = {
id: BType.U8,
name: BType.STR,
age: BType.U8,
isMarried: BType.BOOL,
favoriteFood: foodSchema,
hatedFoods: [foodSchema]
};
const data = {
id: 1,
name: 'John',
age: 28,
isMarried: false,
favoriteFood: {
id: 1,
name: 'Banana',
price: 100
},
hatedFoods: [{
id: 2,
name: 'Cabbage',
price: 50
}, {
id: 3,
name: 'Carrot',
price: 75
}]
}
const buffer = serialize(data, schema);
const result = deserialize(buffer, schema);
Caveats
- There will be decimal errors when deserializing f32 and f64 values. It is recommended that you always round it when you are going to use the value.