Skip to content

5.2.1

Compare
Choose a tag to compare
@marcus-pousette marcus-pousette released this 17 Jan 09:49
· 6 commits to master since this release
  • feat: Custom class serializer

Custom class serialization that is useful if you want to override serialization behaviour, like doing caching

Example

import { serializer } from '@dao-xyz/borsh'
class TestStruct {

  @field({type: 'u8'})
  public number: number;

  constructor(number: number) {
    this.number = number;
  }

  cache: Uint8Array | undefined;

  @serializer()
  override(writer: BinaryWriter, serialize: (obj: this) => Uint8Array) {
    if (!this.cache) {
      this.cache = serialize(this)
    }
    writer.set(this.cache)
  }
}