npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-binary-serializer

v2.1.3

Published

Binary serializer for typescript

Downloads

3,716

Readme

ts-binary-serializer

Binary serializer for typescript, support NodeJs/Browser.

npm

Quick Start

Install

//For npm
npm install ts-binary-serializer --save

//For yarn
yarn add ts-binary-serializer

enable decorators on tsconfig.json

"experimentalDecorators": true
import { SerializeField, DataType, BinarySerializer } from 'ts-binary-serializer';

class A{
    @SerializeField(DataType.String)
    public str:string = "HelloWorld";
}
let binaryData = BinarySerializer.Serialize(new A(),A);

console.log(binaryData); //Uint8Array [ 9, 22, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ]

let a1 = BinarySerializer.Deserialize(binaryData,A);
console.log(a1); A { str: 'Hello world' }

Usage

//Serialize
export function Serialize<T>(obj:T,type?:{new():T},options?:BinarySerializeOptions):Uint8Array

//Deserialize
export function Deserialize<T>(databuffer:Uint8Array|ArrayBuffer,type:{new():T},options?:BinaryDeserializeOptions):T

sample

classA{
//...
}

let a1 = new ClassA();
let data = BinarySerializer.Serialize(a,ClassA);
let a2 = BinarySerializer.Deserialize(data,ClassA);

Decorator

//Decorator @seralizeField
export function SerializeField(type : DataType,array:boolean = false,ptype?:any | DataType)

//All support DataType
//remark: all js [Number] type can be attached with strict typed [DataType],
//Use shorter [DataType] may have smaller binary serialization size.But result will be different if there has numeric overflow.
export enum DataType {
    Null = 0,
    Float32 = 1,
    Float64 = 2,
    Int32 =  3,
    Int16 = 4,
    Int8 = 5,
    Uint32 = 6,
    Uint16 = 7,
    Uint8 = 8,
    String = 9,
    Bool = 10,
    Object = 11,
    Float16 = 12,
    Map = 13,
    Varint32 = 14,
    UVarint32 = 15,
    TypedArray = 16,
}

Object Array

set the second param of @seralizeField to true when the property is an Array object.

class ClassA{
    @SerializeField(DataType.String,true)
    public stringArray:string[];
    @SerializeField(DataType.Float32,true)
    public numAry:Array<Number>;
}

TypedArray

class ClassWithTypedArray{
    @SerializeField(DataType.TypedArray,false,Uint8Array)
    public uint8:Uint8Array;
    @SerializeField(DataType.TypedArray,false,Int32Array)
    public int32:Int32Array;
    @SerializeField(DataType.TypedArray,false,Float64Array)
    public float64:Float64Array;
}

Nested Class

class ClassB{
    @SerializeField(DataType.Bool)
    public valid:boolean;
}
class ClassC{
    @SerializeField(DataType.Object,false,ClassB)
    public b:ClassB;
}
// ...
let buffer = BinarySerializer.Serialize(new ClassC(),ClassC)

nested class array is also supported.

class ClassC{
    @SerializeField(DataType.Object,true,ClassB)
    public b:ClassB[];
}

Variant Encoding

Varint32/UVarint32

class ClassVariant{
    @SerializeField(DataType.Varint32)
    public vint:number;
    @SerializeField(DataType.UVarint32)
    public uvint:number;
    @SerializeField(DataType.Varint32,true)
    public vintary:number[];
    @SerializeField(DataType.UVarint32,true)
    public uvintary:number[];
}

Map Encoding

class TestClass{
    @SerializeField(DataType.Map,false,TestObj)
    public map:{[key:string]:string};
}

Dev And Test

This lib is packed using rollup.

Dist files:

ESModule: dist/BinarySerializer.es.js

UMD: dist/BinarySerializer.umd.js

build distribution

yarn build

run unitest

yarn test

Benchmark

simple benchmark

| type | Size(byte) | SerializeTime(ms) | DeserializeTime(ms) | | --- | ---| --- | --- | | Json | 641395 | 7.0295 | 8.5726 | | Binary | 157420 | 19.1871 | 19.8457 |

License

MIT