1

I'm working on a Firefox extension and I'm trying to stringify a JSON object.

I'm using this stringify function but I'm getting this error:

Could not convert JavaScript argument "NS_ERROR_XPC_BAD_CONVERT_JS"

I really just care about the first level or two or properties inside the object, and I don't care about the methods / functions. Is there a simpler way to stringify an object if I don't need all this?

Here's the bit of code I'm using:

    var s=JSONstring.make('abc');

    try{

        Firebug.Console.log(gContextMenu);

        s = JSON.stringify(gContextMenu);

        Firebug.Console.log(s);
    }catch(e){
        Firebug.Console.log('error');
        Firebug.Console.log(e);
    }
    var s=JSONstring.make('abc');
    Firebug.Console.log(s);
    Firebug.Console.log(gContextMenu);

Here is the error in the console window:

image

This is what I was able to copy out of the Firebug console window:

http://pastebin.com/KPXceRag

Here is a screenshot of the object:

image http://img143.imageshack.us/img143/2603/pictureos.png

3
  • Please provide a sample string that you want to stringify. Thanks. Sep 1, 2011 at 21:19
  • Does the object reference itself somehow? Sep 1, 2011 at 21:22
  • @ChaosPandion - probably, yes. But the function I used was supposed to deal with recursion. I only copy / pasted the function, though, so perhaps it needs tweaking. I'm guessing JSON.stringify does not deal with it, or that is not always available?
    – cwd
    Sep 1, 2011 at 21:27

1 Answer 1

1

You can define a custom function on your object called toJSON() that returns only the elements of the object you want. Somewhat counter-intuitively, your toJSON function should not return a JSON string - it just returns an object representing what should be used as the input for JSON.stringify.

For example:

// an object with some attributes you want and some you don't
var o = {
    a:"value1", 
    b:"value2", 
    doCalc: function() { return this.a + " " +  this.b } 
};
// define a custom toJSON() method
o.toJSON = function() { 
    return {
        a:this.a, 
        calc: this.doCalc() 
    } 
};

JSON.stringify(o); // '{"a":"value","calc":"value1 value2"}'

In your case, you should be able to define this method for gContextMenu on the fly, before calling JSON.stringify. This does require you to explicitly define what you want and don't want, but I don't think there's a better way.

Edit: If you want to pull all non-method values, you could try something like this:

o.toJSON = function() { 
    var attrs = {};
    for (var attr in this) {
        if (typeof this[attr] != "function") {
            attrs[attr] = String(this[attr]); // force to string
        }
    }
    return attrs;
};

You will probably end up with a few attributes set to "[object Object]", but if all you want is inspection, this probably isn't an issue. You could also try checking for typeof this[attr] == "string" || typeof this[attr] == "number" if you wanted to get only those types of attributes.

1
  • 1
    How can we add a loop to return all properties but not methods? Or, perhaps all properties that are strings? I dont know which items will be present and was trying to grab all of them.
    – cwd
    Sep 1, 2011 at 21:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.