Skip to content

Commit

Permalink
use custom Reader/Writer (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
exromany authored Aug 18, 2021
1 parent 0b96510 commit 5e6d4cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
12 changes: 6 additions & 6 deletions borsh-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ function serializeStruct(schema: Schema, obj: any, writer: BinaryWriter) {

/// Serialize given object using schema of the form:
/// { class_name -> [ [field_name, field_type], .. ], .. }
export function serialize(schema: Schema, obj: any): Uint8Array {
const writer = new BinaryWriter();
export function serialize(schema: Schema, obj: any, Writer = BinaryWriter): Uint8Array {
const writer = new Writer();
serializeStruct(schema, obj, writer);
return writer.toArray();
}
Expand Down Expand Up @@ -391,8 +391,8 @@ function deserializeStruct(schema: Schema, classType: any, reader: BinaryReader)
}

/// Deserializes object from bytes using schema.
export function deserialize<T>( schema: Schema, classType: { new (args: any): T }, buffer: Buffer): T {
const reader = new BinaryReader(buffer);
export function deserialize<T>( schema: Schema, classType: { new (args: any): T }, buffer: Buffer, Reader = BinaryReader): T {
const reader = new Reader(buffer);
const result = deserializeStruct(schema, classType, reader);
if (reader.offset < buffer.length) {
throw new BorshError(`Unexpected ${buffer.length - reader.offset} bytes after deserialized data`);
Expand All @@ -401,7 +401,7 @@ export function deserialize<T>( schema: Schema, classType: { new (args: any): T
}

/// Deserializes object from bytes using schema, without checking the length read
export function deserializeUnchecked<T>(schema: Schema, classType: {new (args: any): T}, buffer: Buffer): T {
const reader = new BinaryReader(buffer);
export function deserializeUnchecked<T>(schema: Schema, classType: {new (args: any): T}, buffer: Buffer, Reader = BinaryReader): T {
const reader = new Reader(buffer);
return deserializeStruct(schema, classType, reader);
}
27 changes: 25 additions & 2 deletions borsh-ts/test/serialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('serialize/deserialize with class methods', () => {

const buf = borsh.serialize(null, item);
const newValue = borsh.deserialize(null, Serializable, buf);

expect(newValue).toEqual(item);
});

Expand All @@ -104,7 +104,7 @@ test('serialize/deserialize fixed array', () => {

const buf = borsh.serialize(schema, value);
const deserializedValue = borsh.deserialize(schema, Test, buf);

expect(buf).toEqual(Buffer.from([5, 0, 0, 0, 104, 101, 108, 108, 111, 5, 0, 0, 0, 119, 111, 114, 108, 100]));
expect(deserializedValue.a).toEqual(['hello', 'world']);
});
Expand Down Expand Up @@ -158,3 +158,26 @@ test('base encode and decode test', async () => {
const value = '244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM';
expect(borsh.baseEncode(borsh.baseDecode(value))).toEqual(value);
});

test('serialize with custom writer/reader', async () => {
class ExtendedWriter extends borsh.BinaryWriter {
writeDate(value) {
this.writeU64(value.getTime());
}
}

class ExtendedReader extends borsh.BinaryReader {
readDate() {
const value = this.readU64();
return new Date(value.toNumber());
}
}

const time = 'Aug 12, 2021 12:00:00 UTC+00:00';
const value = new Test({ x: new Date(time) });
const schema = new Map([[Test, {kind: 'struct', fields: [['x', 'date']]}]]);

const buf = borsh.serialize(schema, value, ExtendedWriter);
const newValue = borsh.deserialize(schema, Test, buf, ExtendedReader);
expect(newValue.x).toEqual(new Date(time));
});
6 changes: 3 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export declare class BinaryReader {
readFixedArray(len: number): Uint8Array;
readArray(fn: any): any[];
}
export declare function serialize(schema: Schema, obj: any): Uint8Array;
export declare function serialize(schema: Schema, obj: any, Writer?: typeof BinaryWriter): Uint8Array;
export declare function deserialize<T>(schema: Schema, classType: {
new (args: any): T;
}, buffer: Buffer): T;
}, buffer: Buffer, Reader?: typeof BinaryReader): T;
export declare function deserializeUnchecked<T>(schema: Schema, classType: {
new (args: any): T;
}, buffer: Buffer): T;
}, buffer: Buffer, Reader?: typeof BinaryReader): T;
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ function serializeStruct(schema, obj, writer) {
}
/// Serialize given object using schema of the form:
/// { class_name -> [ [field_name, field_type], .. ], .. }
function serialize(schema, obj) {
const writer = new BinaryWriter();
function serialize(schema, obj, Writer = BinaryWriter) {
const writer = new Writer();
serializeStruct(schema, obj, writer);
return writer.toArray();
}
Expand Down Expand Up @@ -398,8 +398,8 @@ function deserializeStruct(schema, classType, reader) {
throw new BorshError(`Unexpected schema kind: ${structSchema.kind} for ${classType.constructor.name}`);
}
/// Deserializes object from bytes using schema.
function deserialize(schema, classType, buffer) {
const reader = new BinaryReader(buffer);
function deserialize(schema, classType, buffer, Reader = BinaryReader) {
const reader = new Reader(buffer);
const result = deserializeStruct(schema, classType, reader);
if (reader.offset < buffer.length) {
throw new BorshError(`Unexpected ${buffer.length - reader.offset} bytes after deserialized data`);
Expand All @@ -408,8 +408,8 @@ function deserialize(schema, classType, buffer) {
}
exports.deserialize = deserialize;
/// Deserializes object from bytes using schema, without checking the length read
function deserializeUnchecked(schema, classType, buffer) {
const reader = new BinaryReader(buffer);
function deserializeUnchecked(schema, classType, buffer, Reader = BinaryReader) {
const reader = new Reader(buffer);
return deserializeStruct(schema, classType, reader);
}
exports.deserializeUnchecked = deserializeUnchecked;

0 comments on commit 5e6d4cb

Please sign in to comment.