Codec

The @liskhq/lisk-codec package implements a decoder and encoder using Lisk JSON (JavaScript Object Notation), schema according to the Lisk protocol.

Installation

npm install @liskhq/lisk-codec

Update

To perform an upgrade, execute the following command:

npm update @liskhq/lisk-codec

Usage

//import * as codecFunctions from '@liskhq/lisk-codec';
import { codec as codecFunctions } from 'lisk-sdk';
// Codec can be used in two ways:
// 1) Use exposed default instance
codecFunctions.codec.encode(schema, object);
// 2) Create a new instance
const newCodec = new codecFunctions.Codec();
newCodec.encode(schema, object);

Codec interface

interface Codec {
  // adds schema to memory to improve the performance
  addSchema(schema: Schema): boolean;
  // encode the JavaScript Object message to Buffer using the schema provided
  encode(schema: Schema, message: object): Buffer;
  // decode the Buffer message to JavaScript Object
  decode<T>(schema: Schema, message: Buffer): T;
  // decode the message input to JSON object
  decodeJSON<T>(schema: Schema, message: Buffer): T;
  // encode the JSON object message to Buffer using the schema provided
  encodeJSON(schema: Schema, message: object): Buffer;
  // Convert JavaScript object to JSON object using the schema provided
  toJSON<T = object>(schema: Schema, message: object): T;
  // Convert JSON object to JavaScript object using the schema provided
  fromJSON<T = object>(schema: Schema, message: object): T;
}