1

Is there a function or library that will take a JS Object that has properties and functions and stringify it to JSON but when it comes to a Function, it will change it into a property and call the function to return its value??

function object() {
    this.property = 12;
    this.function = function() { return "This"; }
} 

So this would stringify into:

{property: 12, function: "This"}

Any ideas? Just curious if there is one already.. if not I will take a stab at one. It shouldn't be to hard to extend a JSON.stringify().

6
  • BTW, this is just an example.. I realize that this.function wouldn't eval right. =) Commented Jun 24, 2010 at 17:09
  • 4
    It doesn't really make sense to do this, because you're assuming all of the functions have no arguments. Commented Jun 24, 2010 at 17:11
  • 1
    If you don't have a lot of these objects and they have special interpretations like the stringified version you've described, you might be better off writing your own toJson() method on the objects. Commented Jun 24, 2010 at 17:13
  • I actually hadn't thought of that... hrmm.. Well why not just stringify the ones that don't have parameters? The basic goal is to get a JS object to another language and allow it to have all of the needed information without having to do much work on the JS side. So if you built your Object properly with the realization that this stringify would happen. Then it is prepared, thus I only would need the ones without parameters ;) Commented Jun 24, 2010 at 17:15
  • 1
    stackoverflow.com/questions/191881/… could be useful Commented Jun 24, 2010 at 17:17

2 Answers 2

2

Some objects have no trivial serialization. If you wish you serialize them, you must do so yourself with you own set of assumptions.

Functions (esp. those with closures) and IO Streams are some examples. In the case of a JS function, serialization (without serializaing the entire context!) violates the semantics of the function with respect to the execution context and scope chains. Also, the ability for assistance from the browser to return the "text" of a function depends upon the browser.

Sign up to request clarification or add additional context in comments.

Comments

1

The JSON.stringify method provides the option to include a callback argument, called replacer, the function is invoked passing the key and value of each property of your object, there you are able to detect if the value is a function and invoke it:

var obj = {
  "property": 12,
  "function": function() { return "This"; }
};

JSON.stringify(obj, function (key, value) {
  if (typeof value == 'function') {
    return value();
  }
  return value;
});

2 Comments

This almost worked. It doesn't seem to call the callback() if it is a function. It seems as if the stringify code excludes those automatically. So I will/would have to just modify the JSON code to have it call into the callback if one is detected. Great find!
@James, Thanks, oh, I forgot to tell you that Firefox 3.6 has an implementation bug using this replacer argument. Another option would be to implement your custom stringify function, something like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.