Skip to content

Serialization

Jirka Dell'Oro-Friedl edited this page Feb 4, 2021 · 6 revisions

Concept

Related to Mutation is the concept of Serialization. While mutation is used to modify existing objects, serialization is used to recreate objects. It creates a simple object of the type Serialization which holds information needed to recreate an object identical to the one it originated from, In the simplest form, the serialization object stores the type of the object to recreate and a mutator holding the values. However, serialization may, and often does, significantly differ from mutation.

Human readable

Serialization also serves the purpose of storing information in separate files in a human readable format using the JSON-methods. Thus, serialization-objects are created in such a way, as to yield a neatly formatted strings when using JSON.stringify() and turn into valid serialization-objects with JSON.parse().

Process

As serialization is used to create copies of existing objects at runtime or to store objects as strings or recreate them from strings, this simple scheme depicts the process:

 [Serializable] → (serialize) → [Serialization] → (stringify) → [String] → (save or send)
                                       ↓                            ↓                  ↓         
               [Serializable] ← (deserialize) ← [Serialization] ← (parse) ← (load) ← [Medium]

Interface

A class implementing the interface Serializable requires the implementation of the methods public serialize(): Serialization and public async deserialize(_serialization: Serialization): Promise<Serializable>. In order to use the serialization-Methods of the classes Serializer and Project, the objects to serialize or deserialize must be instances of classes implementing this interface.

Namespace registration

In order to use the serialization with classes created in other namespaces outside of FUDGE, register the namespace with the Serializer or the Project using Project.registerScriptNamespace(_namespace: Object) or Serializer.registerNamespace(_namespace: Object) respectively.