Skip to content

Commit

Permalink
Fix: convert error when format includes __repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
ykisii committed Mar 31, 2023
1 parent c444887 commit 0d1b586
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 7 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ const data: any = b2j.convert(arry, format); // default endian is big

// => {"dat": 168496141 } # 0x0A0B0C0D
```

## Example
### binary
```bin
FF FF 01 02 03 04 05 06 07 08 09 0A 05 01 02 03 04 05
```
### format
```json
[
{"__reserve": 2},
{"__repeat": 10},
{"dat1": [{"v1": 1}]},
{"__repeat/num": 1},
{"dat2": [{"v1": 1}]}
]
```
### output
```json
{
dat1: [
{ v1: 1 }, { v1: 2 },
{ v1: 3 }, { v1: 4 },
{ v1: 5 }, { v1: 6 },
{ v1: 7 }, { v1: 8 },
{ v1: 9 }, { v1: 10 }
],
"__repeat/num": 5,
dat2: [ { v1: 1 }, { v1: 2 }, { v1: 3 }, { v1: 4 }, { v1: 5 } ]
}
```

more info: please refer to test.ts.
9 changes: 6 additions & 3 deletions binary_to_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class BinaryToJSON {

const output: Obj = {};
const array: object[] = [];

for (const format of formats) {
const [key, val] = Object.entries(format)[0];
if (Array.isArray(val)) {
Expand Down Expand Up @@ -59,13 +58,17 @@ export class BinaryToJSON {
br.seek(br.position + Number(val));
return null;
}

if (key == "__repeat") {
this.#array_size = Number(val);
return null;
}

let value: number = this.readBytes(br, Number(val));

if (/^__repeat/.test(key)) {
this.#array_size = value;
if (key === "__repeat") return null;
}

return value;
}

Expand Down
Binary file modified sample.dat
Binary file not shown.
2 changes: 2 additions & 0 deletions sample2.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
��

4 changes: 3 additions & 1 deletion sample_format.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
{"type":1},
{"x":1},
{"y":1}
]}
]},
{"__repeat":3},
{"tag":1}
]
7 changes: 7 additions & 0 deletions sample_format2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{"__reserve": 2},
{"__repeat": 10},
{"dat1": [{"v": 1}]},
{"__repeat/num": 1},
{"dat2": [{"v": 1}]}
]
20 changes: 18 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEquals, assertNotEquals } from "https://deno.land/std/testing/asserts.ts";
import { BinaryToJSON } from "https://deno.land/x/binary_to_json@v0.1.0/mod.ts";
//import { BinaryToJSON } from "https://deno.land/x/binary_to_json@v0.1.0/mod.ts";
import { BinaryToJSON } from "./binary_to_json.ts";

Deno.test(
"constructor",
Expand All @@ -26,11 +27,26 @@ Deno.test(
const data: any = b2j.convert(buffer, format);
console.log(data);
assertEquals(0x0A, data['state']);
assertEquals(0x07, data['infos'][0]['type']);
assertEquals(0x01, data['infos'][0]['type']);
assertEquals(0x00, data['dat'][0]['type']);
},
);

Deno.test(
"convert repeat",
function():void {
const file = Deno.openSync("sample2.dat");
const buffer = Deno.readAllSync(file);
Deno.close(file.rid);
const format = JSON.parse(Deno.readTextFileSync("sample_format2.json"));
const b2j = new BinaryToJSON();
const data: any = b2j.convert(buffer, format);
console.log(data);
assertEquals(0x01, data['dat1'][0]['v']);
assertEquals(0x01, data['dat2'][0]['v']);
},
);

Deno.test(
"little endian",
function(): void {
Expand Down

0 comments on commit 0d1b586

Please sign in to comment.