btree-typescript icon indicating copy to clipboard operation
btree-typescript copied to clipboard

Serialization of sorted btree

Open mcndt opened this issue 2 years ago • 2 comments

What would be the best method for serialization of a sorted btree (using interface ISortedMap)?

Is it safe to use JSON.stringify, or would you recommend serializing the iterable and then reconstructing the tree from the resulting array?

mcndt avatar Mar 05 '22 23:03 mcndt

^ Also have same question

kamarmack avatar Mar 06 '22 17:03 kamarmack

BTree should be serialized in array form, like this:

let btree1 = new BTree<number>([[1, 'one'], [2, 2.22]]);
let json   = JSON.stringify([... btree1.entries()]);

let btree2 = new BTree<number>(JSON.parse(json));

If you use JSON.stringify you'll get the internal contents of the BTree as JSON, and (1) I don't know any way to reconstruct the BTree object from that, (2) but if I did, I would still advise against storing it that way because future versions could theoretically use different names for private properties.

qwertie avatar Mar 09 '22 05:03 qwertie