Class SerializableAbstract

Base class for serializable classes. Features:

  • All instances are automatically assigned a 128 bit unique identifier.
  • Automatically registers the class with

See

registerClass.

  • All instances are stored in a static map.
  • All instances can be serialized and deserialized while handling circular references and special types normally not correctyly stringified: Date, Set, Map, BigInt, Symbol.

Example

class Person extends Serializable {
constructor(public name: string, public age: number) {
super()
}
}

class Boy extends Person {
sex = 'male'
friends: Person[] = []
constructor(name: string, age: number) {
super(name, age)
}
}

class Girl extends Person {
sex = 'female'
friends: Person[] = []
constructor(name: string, age: number) {
super(name, age)
}
}

const girl = new Girl('Anna', 12)
const boy = new Boy('Peter', 11)
girl.friends.push(boy)
boy.friends.push(girl)

console.log('----------------------------')
console.log(girl)
console.log(boy)
console.log('----------------------------')
const jsonGirls = Girl.serializeInstances(2)
console.log(jsonGirls)
const jsonBoys = Boy.serializeInstances(2)
console.log(jsonBoys)
console.log('----------------------------')
// serialize both classes' instances together so object references are restored
Serializable.deserializeInstances(jsonGirls, jsonBoys)
console.dir(Girl.instances, { depth: null })
console.dir(Boy.instances, { depth: null })
console.log('----------------------------')

Hierarchy

  • Serializable

Implements

Constructors

Properties

id: string

The 128 bit unique identifier of the instance. The first 32 bits are a timestamp encoded as 'base64url'.

type: string

The name of the class of the instance.

Accessors

Methods

  • Deserializes JSON string(s) and instantiate objects as class instances. If you need to deserialize instances from multiple classes at once, just pass them all to this method and all cross-class object references are handles. The json strings must have been produced by using

    Parameters

    • Rest ...jsons: string[]

      One or more json strings to deserialize.

    Returns void

    See

    Serialized.serializeInstances

Generated using TypeDoc