Skip to content

Commit aca1a96

Browse files
committed
add array support.
1 parent c55e743 commit aca1a96

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

src/BinarySeralize.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,27 @@ class BinaryBuffer {
136136
this.writeType(DataType.Null);
137137
return;
138138
}
139+
140+
if(type == DataType.Object){
141+
throw new Error('DataType.Object is not support currently.');
142+
}
143+
139144
this.writeType(type);
140-
let f = BinaryBuffer.WriteFuncMap[type];
141-
this[f](val);
145+
let f:(v:any)=>void =this[BinaryBuffer.WriteFuncMap[type]];
146+
if(!isary){
147+
f.call(this,val);
148+
return;
149+
}
150+
if(!Array.isArray(val)){
151+
throw new Error(`target property: ${val} is not an array.`)
152+
}
153+
let ary = <Array<any>>val;
154+
155+
let arylen = ary.length;
156+
this.writeUint16(arylen);
157+
for(let i=0;i<arylen;i++){
158+
f.call(this,ary[i]);
159+
}
142160
}
143161

144162
public readProperty(type : DataType,isary= false) : any {
@@ -148,8 +166,20 @@ class BinaryBuffer {
148166
if (t != type)
149167
throw new Error(`data type mismatch ${type} ${t}`);
150168

151-
let f = BinaryBuffer.ReadFuncMap[type];
152-
return this[f]();
169+
let f:(v:any)=>void = this[BinaryBuffer.ReadFuncMap[type]];
170+
171+
if(!isary){
172+
return f.call(this);
173+
}
174+
175+
let arylen = this.readUint16();
176+
if(arylen == 0) return [];
177+
178+
let ary:any[] = [];
179+
for(let i=0;i<arylen;i++){
180+
ary.push(f.call(this));
181+
}
182+
return ary;
153183
}
154184

155185
public writeFloat32(v : number) {

src/TestClass.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ export class TestClass{
88
public index:number;
99
@seralizeField(DataType.Float32,true)
1010
public floatAry:number[];
11+
@seralizeField(DataType.Int16,true)
12+
public int16:number[];
13+
@seralizeField(DataType.String,true)
14+
public strlist:string[];
1115
}
1216

src/index.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
import { TestClass } from "./TestClass";
2-
import { BinarySerialize, BinaryDeserialize } from "./BinarySeralize";
1+
import {TestClass} from "./TestClass";
2+
import {BinarySerialize, BinaryDeserialize} from "./BinarySeralize";
3+
import {TextEncoder} from 'text-encoding';
34

5+
let t = new TestClass();
46

5-
let t =new TestClass();
7+
t.index = 14;
8+
t.title = "dwdwdw";
9+
t.floatAry = [1.2, 433.3, -3243.1];
10+
t.int16 = [13, 5454, -124, 32212];
11+
t.strlist = ['hello', 'world', 'typescript', '!'];
612

7-
t.index= 14;
8-
t.title= "dwdwdw";
9-
10-
let data= BinarySerialize(t);
13+
let data = BinarySerialize(t);
1114

1215
let off = data.byteOffset;
13-
let len =data.byteLength + off;
16+
let len = data.byteLength + off;
1417

15-
let braw = data.buffer.slice(off,len);
16-
let b = new Uint8Array(braw);
18+
let braw = data
19+
.buffer
20+
.slice(off, len);
1721

18-
let ary:number[] =[];
19-
for(var i=0;i<b.byteLength;i++){
20-
ary.push(b[i]);
21-
}
22+
console.log('binary length: ' + data.byteLength);
23+
let jstr = JSON.stringify(t, null, 0);
24+
let enc = new TextEncoder();
25+
console.log('json length: ' + enc.encode(jstr).length);
2226

23-
console.log(ary);
27+
let t1 = BinaryDeserialize < TestClass > (TestClass, braw);
2428

25-
let t1 = BinaryDeserialize<TestClass>(TestClass,braw);
29+
console.log('--------------------');
2630

27-
console.log(t,Object.getPrototypeOf(t));
28-
console.log(t1,Object.getPrototypeOf(t1));
31+
console.log(t, Object.getPrototypeOf(t));
32+
console.log(t1, Object.getPrototypeOf(t1));

0 commit comments

Comments
 (0)