Is there any possibility to have JSON.stringify preserve functions?

前端 未结 9 1849
一个人的身影
一个人的身影 2020-12-01 05:14

Take this object:

x = {
 \"key1\": \"xxx\",
 \"key2\": function(){return this.key1}
}

If I do this:

y = JSON.parse( JSON.st         


        
相关标签:
9条回答
  • 2020-12-01 06:18

    It is entirely possible to create functions from string without eval()

    var obj = {a:function(a,b){
        return a+b;
    }};
    
    var serialized = JSON.stringify(obj, function(k,v){
        //special treatment for function types
        if(typeof v === "function")
            return v.toString();//we save the function as string
        return v;
    });
    /*output:
    "{"a":"function (a,b){\n        return a+b;\n    }"}"
    */
    

    now some magic to turn string into function with this function

    var compileFunction = function(str){
        //find parameters
        var pstart = str.indexOf('('), pend = str.indexOf(')');
        var params = str.substring(pstart+1, pend);
        params = params.trim();
    
        //find function body
        var bstart = str.indexOf('{'), bend = str.lastIndexOf('}');
        var str = str.substring(bstart+1, bend);
    
        return Function(params, str);
    }
    

    now use JSON.parse with reviver

    var revivedObj = JSON.parse(serialized, function(k,v){
        // there is probably a better way to determ if a value is a function string
        if(typeof v === "string" && v.indexOf("function") !== -1)
            return compileFunction(v);
        return v;
    });
    
    //output:
    
     revivedObj.a
    
     function anonymous(a,b
     /**/) {
    
        return a+b;
    
     }
    
     revivedObj.a(1,2)
    3
    
    0 讨论(0)
  • 2020-12-01 06:18

    To my knowledge, there are no serialization libraries that persist functions - in any language. Serialization is what one does to preserve data. Compilation is what one does to preserve functions.

    0 讨论(0)
  • 2020-12-01 06:19

    I've had a similar requirement lately. To be clear, the output looks like JSON but in fact is just javascript.

    JSON.stringify works well in most cases, but "fails" with functions.

    I got it working with a few tricks:

    1. make use of replacer (2nd parameter of JSON.stringify())
    2. use func.toString() to get the JS code for a function
    3. remember which functions have been stringified and replace them directly in the result

    And here's how it looks like:

    // our source data
    const source = {
        "aaa": 123,
        "bbb": function (c) {
            // do something
            return c + 1;
        }
    };
    
    // keep a list of serialized functions
    const functions = [];
    
    // json replacer - returns a placeholder for functions
    const jsonReplacer = function (key, val) {
        if (typeof val === 'function') {
      	    functions.push(val.toString());
            
            return "{func_" + (functions.length - 1) + "}";
        }
            
        return val;
    };
    
    // regex replacer - replaces placeholders with functions
    const funcReplacer = function (match, id) {
       return functions[id];
    };
    
    const result = JSON
        .stringify(source, jsonReplacer)               // generate json with placeholders
        .replace(/"\{func_(\d+)\}"/g, funcReplacer);   // replace placeholders with functions
    
    // show the result
    document.body.innerText = result;
    body { white-space: pre-wrap; font-family: monospace; }

    Important: Be careful about the placeholder format - make sure it's not too generic. If you change it, also change the regex as applicable.

    0 讨论(0)
提交回复
热议问题