Skip to content

gorkermann/toastpoint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

toastpoint

JSON with pointers

description

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.

caveats

  • 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__

flexibility

  • any object converted to JSON is checked for toJSON() and fromJSON() methods. If present, these are used instead of the standard full conversion.

About

JSON with pointers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published