Skip to content

Serialization Schema

jacobdufault edited this page Jul 15, 2014 · 4 revisions

Serialization Schema

Full Serializer uses a simple schema for serialization that easily supports storing extra serialization metadata.

Prior Schema

Full Serializer initially shipped with a different serialization schema. In deserialization, the old schema is automatically converted into the new one, so serialized objects written using the prior versions of Full Serializer will still correctly import, ensuring no data loss.

Inheritance

When the object being serialized has a different type than the given storage type, a $type field is included in the serialization data.

For example, object o = new Vector2() will be serialized as

{
    "$type": "UnityEngine.Vector2",
    "x": 0,
    "y": 0
}

Cyclic Object Graphs

We have this model, which can contain cycles:

class Cyclic {
    public Cyclic Ref;
    public int Val;
}

Our serialized value is defined as:

Cyclic serialized = new Cyclic() {
    Val = 1,
    Ref = new Cyclic {
        Val = 2
    }
};
serialized.Ref.Ref = serialized; // introduce a cycle

When we serialize serialized, we get

{
    "$id": "0",
    "Ref": {
        "Ref": {
            "$ref": "0"
        },
        "Val": 2
    },
    "Val": 1,
}

Note that the $id field is only inserted when there is a corresponding $ref field; if we had just serialized Cyclic c = new Cyclic { Val = 3 } we get this data

{
    "Ref": null,
    "Val": 3
}

Versioned Objects

When we have a versioned model such as:

[fsObject("v1")]
class Versioned {
    public int Value;
}

and we serialize an instance of it, like this one var c = new Versioned { Value = 1 };, then we get this data

{
    "$version": "v1",
    "Value": 1
}

Special Case: What happens when we need metadata but the data is not a dictionary?

Simple: we wrap the original data in a dictionary and return that dictionary instead. Using inheritance as an example of required metadata, when we serialize object o = 3 we get

{
    "$type": "System.Int32",
    "$content": 3
}