Class CodecRegistry

Singleton class that acts as a global registry for codecs.

  1. Define a default codec with a new instance of the object to be handled.

    const codec = new ObjectCodec(new Transactions());
    
  2. Define the functions required for encoding and decoding objects.

    codec.encode = function(enc, obj) { ... }
    codec.decode = function(dec: Codec, node: Element, into: any): any { ... }
  3. Register the codec in the CodecRegistry.

    CodecRegistry.register(codec);
    

ObjectCodec.decode may be used to either create a new instance of an object or to configure an existing instance, in which case the into argument points to the existing object. In this case, we say the codec "configures" the object.

Constructors

Properties

aliases: {
    [key: string]: string | undefined;
} = {}

Maps from classnames to codec names.

codecs: {
    [key: string]: ObjectCodec | undefined;
} = {}

Methods

  • Adds an alias for mapping a classname to a codec name.

    Parameters

    • classname: string
    • codecname: string

    Returns void

  • Returns a codec that handles objects that are constructed using the given constructor or a codec registered under the provided name.

    When passing a name, the method first check if an alias exists for the name, and if so, it uses it to retrieve the codec.

    If there is no registered Codec, the method tries to register a new Codec using the provided constructor.

    Parameters

    • constructorOrName: any

      JavaScript constructor function of the Codec or Codec name.

    Returns null | ObjectCodec

  • First try to get the codec by the name it is registered with. If it doesn't exist, use the alias eventually declared to get the codec.

    Parameters

    • name: string

      the name of the codec that is willing to be retrieved.

    Returns null | ObjectCodec