Using JSON.stringify on custom class

前端 未结 5 1996
执笔经年
执笔经年 2020-12-06 12:51

I\'m trying to store an object in redis, which is an instance of a class, and thus has functions, here\'s an example:

function myClass(){
    this._attr = \"         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-06 13:35

    Scanales, I had the same issue and tried a technique similar to Bergi's recommendation of creating new serialization/deserialization methods...but found it didn't work for me because I have objects nested in objects (several deep). If that's your case then here's how I solved it. I wrote a base class (clsPersistableObject) from which all objects that I wanted to persist inherited from. The base class has a method called deserialize, which is passed the JSON string. This method sets the properties one by one (but does not wipe out the exist methods) and then recursively defer to the child object to do the same (as many times as necessary).

    deserialize: function (vstrString) {
                    //.parse: convert JSON string to object state
    
    
                    //Use JSON to quickly parse into temp object (does a deep restore of all properties)
                    var tmpObject = JSON.parse(vstrString);
    
                    //objZoo2.animal.move();
    
                    //Note: can't just do something like this: 
                    //  CopyProperties(tmpObject, this);
                    //because it will blindly replace the deep objects 
                    //completely...inadvertently wiping out methods on it.  Instead:
                    //1) set the properties manually/one-by-one.
                    //2) on objects, defer to the deserialize on the child object (if it inherits clsPersistableObject)
                    //2b) if it doesn't inherit it, it's an intrinsic type, etc...just do a JSON parse.
    
                    //loop through all properties
                    var objProperty;
                    for (objProperty in tmpObject) {
    
                        //get property name and value
                        var strPropertyName = objProperty;
                        var strPropertyValue = tmpObject[objProperty]; //note: doing this .toString() will cause
    
                        if (objProperty !== undefined) {
                            //check type of property
                            if (typeof strPropertyValue == "object") {
                                //object property: call it recursively (and return that value)
    
                                var strPropertyValue_AsString = JSON.stringify(strPropertyValue);
    
                                //see if has a deserialize (i.e. inherited from clsPeristableObject)
                                if ("deserialize" in this[objProperty]) {
                                    //yes: call it
                                    this[objProperty]["deserialize"](strPropertyValue_AsString);
                                }
                                else {
                                    //no: call normal JSON to deserialize this object and all below it
                                    this[objProperty] = JSON.parse(strPropertyValue_AsString);
                                }  //end else on if ("deserialize" in this[objProperty]) 
                            }
                            else {
                                //normal property: set it on "this"
                                this[objProperty] = tmpObject[objProperty];
                            } //end else on if (typeof strPropertyValue == "object") 
                        } //end if (objProperty !== undefined) 
                    }
    
                }
    

提交回复
热议问题