-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { BaseTypeHandler, BASIC_TYPES, serializeToBinary, deserializeFromBinary } from 'structpack'; | ||
|
||
class JSONHandler extends BaseTypeHandler { | ||
sizeof(value) { | ||
const json = JSON.stringify(value); | ||
return BASIC_TYPES.str.sizeof(json); // delegate to the string handler | ||
} | ||
|
||
serialize(view, offset, value) { | ||
const json = JSON.stringify(value); | ||
return BASIC_TYPES.str.serialize(view, offset, json); // delegate to the string handler | ||
} | ||
|
||
deserialize(view, offset) { | ||
const res = BASIC_TYPES.str.deserialize(view, offset); // delegate to the string handler | ||
// res is a DeserializedResult object, let's parse the value | ||
res.value = JSON.parse(res.value); | ||
return res; | ||
} | ||
} | ||
|
||
const JSONType = new JSONHandler(); | ||
|
||
const obj = { a: 1, b: 'hello' }; | ||
const binary = serializeToBinary(obj, JSONType); | ||
const deserialized = deserializeFromBinary(binary, JSONType); | ||
console.log(deserialized); // { a: 1, b: 'hello' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { BASIC_TYPES, CompoundTypeHandler, deserializeFromBinary } from 'structpack'; | ||
|
||
// For built-in types or your custom types | ||
const view = new DataView(new ArrayBuffer(100)); | ||
BASIC_TYPES.str.serialize(view, 0, 'Hello, world!'); | ||
console.log(deserializeFromBinary(view, BASIC_TYPES.str)); | ||
|
||
// For structs, you need to create a CompoundTypeHandler | ||
class Vec2 { | ||
constructor(x, y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
toString() { | ||
return `(${this.x}, ${this.y})`; | ||
} | ||
|
||
static typedef = [ | ||
{ field: 'x', type: BASIC_TYPES.f32 }, | ||
{ field: 'y', type: BASIC_TYPES.f32 }, | ||
]; | ||
} | ||
|
||
const p = new Vec2(1, 2); | ||
const handler = new CompoundTypeHandler(Vec2); | ||
handler.serialize(view, 50, p); | ||
|
||
console.log(deserializeFromBinary(new DataView(view.buffer, 50), handler)); |