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

前端 未结 9 1872
一个人的身影
一个人的身影 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: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.

提交回复
热议问题