JSON with pointers
The code
JSON.stringify( obj )
attempts to turn obj
into a JSON string. This doesn't work well if the object contains pointers to other objects, which you also want to stringify. At best, space is wasted, and at worst, the conversion fails due to loops in the object tree.
Toastpoint converts an array of 'first-class' objects to JSON, except that if one first-class object contains another, that entry is replaced by a 'pointer', which looks thus:
{
"__pointer__": true,
id: 2
}
Each first class object contains a unique parameter id
which serves as an address. With toastpoint, complex object trees can be saved and reconstructed easily.
- Each first-class object must have a unique
id
parameter - Each first-class object must have a constructor that can be run with no arguments
- Initialization code for first-class objects that depend on other first class objects may need to be run in a separate
init()
function - Objects cannot have parameters named
__pointer__
or__class__
- any object converted to JSON is checked for
toJSON()
andfromJSON()
methods. If present, these are used instead of the standard full conversion.