Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
ts-binary-serializer
Advanced tools
Binary serializer for typescript, support NodeJs/Browser.
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' }
//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 @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,
}
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>;
}
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;
}
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[];
}
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[];
}
class TestClass{
@SerializeField(DataType.Map,false,TestObj)
public map:{[key:string]:string};
}
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
simple benchmark
type | Size(byte) | SerializeTime(ms) | DeserializeTime(ms) |
---|---|---|---|
Json | 641395 | 7.0295 | 8.5726 |
Binary | 157420 | 19.1871 | 19.8457 |
MIT
FAQs
Binary serializer for typescript
We found that ts-binary-serializer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.