tanagra-core

Methods

(static) addSerializableClasses(clazz)

Adds the prototype of the specified decorated class, and those of all classes referenced by it, to the global cache. This prototype is then retrieved when a serialized object is deserialized.
Parameters:
Name Type Description
clazz Decorated class to add to cache.
Source:
Example
const serializable = require('tanagra-core').serializable

const SerializableFoo = serializable(class Foo {
   constructor(bar) {
     this.bar = bar
   }
}, [Bar])

const SerializableBar = serializable(class Bar {
   constructor(baz) {
     this.baz = baz
   }
}, [Baz])

const SerializableBaz = serializable(class Baz {
   constructor(string) {
     this.string = string
   }
})

addSerializableClasses(SerializableFoo) // adds SerializableFoo, SerializableBar and SerializableBaz

(static) serializable(clazz, nestedClazzes, previousVersions, customSerializationKey)

Decorates a class with serialization metadata, required when deserializing it.
Parameters:
Name Type Description
clazz Class to decorate with serialization metadata.
nestedClazzes Referenced classes. (Note that the library traverses this list recursively, so there's no need to list all classes recursively.)
previousVersions Lists of referenced classes for previous versions of this class (an array of arrays).
customSerializationKey By default, when the class is serialized, it is keyed on its name; this default can be overridden by setting this parameter.
Source:
Example
const serializable = require('tanagra-core').serializable
class Foo {
   constructor(string, bar) {
     this.string = string // a primitive type - no need to specify it as a dependency
     this.bar = bar
   }
}
module.exports = serializable(Foo, [Bar])

// ...

const serializable = require('tanagra-core').serializable
class Bar {
   constructor(number, baz, fooBar) {
     this.number = number // another primitive
     this.baz = baz
     this.fooBar = fooBar
   }
}
module.exports = serializable(Bar, [Baz, FooBar], [
   // referenced types for previous versions of Bar
  [Baz3, FooBar],
  [Baz2],
  [Baz1]
])

// ...

module.exports = require('tanagra-core').serializable(class Baz1 { })
// etc.

(static) serializableClassMappings()

Returns an ES6 Map, mapping the _serializationKey of a serialised class to its prototype.
Source:
Example
const serializableClassMappings = require('tanagra-core').serializableClassMappings
serializableClassMappings.get().set(Foo._serializationKey, Foo.prototype)