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().

James Armstead
  • 398
  • 1
  • 4
  • 14
  • BTW, this is just an example.. I realize that this.function wouldn't eval right. =) – James Armstead Jun 24 '10 at 17:09
  • 4
    It doesn't really make sense to do this, because you're assuming all of the functions have no arguments. – Evan Trimboli Jun 24 '10 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. – Jonathon Faust Jun 24 '10 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 ;) – James Armstead Jun 24 '10 at 17:15
  • 1
    http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery could be useful – mati Jun 24 '10 at 17:17
  • Jonathon - Yea, that actually does sound like a pretty good idea. And realistically probably just as(if not simpler) than adding code inside of the JSON2 stringify to do it. -- Matias - I'm pretty sure I have tried this out actually. This would probably be what I extend if I made my own parser. – James Armstead Jun 24 '10 at 17:29

2 Answers2

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.

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;
});
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 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 Armstead Jun 24 '10 at 18:13
  • @James, Thanks, oh, I forgot to tell you that Firefox 3.6 has an [implementation bug](http://skysanders.net/subtext/archive/2010/02/24/confirmed-bug-in-firefox-3.6-native-json-implementation.aspx) using this `replacer` argument. Another option would be to implement your custom `stringify` function, something like [this](http://github.com/remy/console/blob/master/console.js#L4). – Christian C. Salvadó Jun 24 '10 at 18:17