Cause
This is because you are assigning properties to an array, and array properties will not be included in JSON serialization. For instance:
var a = []; a["test"] = "some value"; JSON.stringify(a);
You need to use a simple object:
var o = {}; o["test"] = "some value"; JSON.stringify(o);
Decision
Change your Cache_Backend_LocalStorage_TagThree_Interface interface to use a dictionary, for example :
interface Cache_Backend_LocalStorage_TagThree_Interface { _tags : { [tag: string] : Cache_Backend_LocalStorage_Tag; }; }
Then update all areas of the code that will now have a compilation error to use the same type. For example, change:
constructor(tags : Cache_Backend_LocalStorage_Tag[] = []) {
To:
constructor(tags : { [tag: string] : Cache_Backend_LocalStorage_Tag; } = {}) {
Just for fun - changing the default serialization behavior (not recommended)
If you really want to do this work with an array with your current setting (I'm not sure why), you can override how serialization is done. To do this, add the toJSON method to the toJSON array in Cache_Backend_LocalStorage_TagThree . This allows you to control how the object is serialized when JSON.stringify is called on it. For instance:
this._tags.toJSON = function() { var values = []; for (var v in this) { if (this[v] instanceof Cache_Backend_LocalStorage_Tag) { values.push(this[v]); } } return JSON.stringify(values); };
source share