The issue only happens when the associative array is programmatically apposed to via a literal definition. Using stringify on a literal definition works.
Trying to understand, I used this to test with a literal definition.
var test = {
voice : { state : 'Ready' }
};
console.log('Stringify test: ' + JSON.stringify(test));
And the output is exactly what I'd expect:
Stringify test: {"voice":{"state":"Ready"}}
This isn't happening when I initialize it programmatically. I should mention that the variable is a private member of an object that I've created, which has accessor/getter methods. Within the constructor, I have:
var states = {};
this.getStates = function()
{
return states;
}
this.setState = function(newState, mediaType)
{
states[mediaType] = newState
}
Now I run the same test.
customObj.setState('{ state: 'Ready' }', 'voice');
var test = customObj.getStates();
console.log('Stringify test: ' + JSON.stringify(test));
And the output is not what I expect:
Stringify Test: []
Lastly I double check what the test variable has with:
for(var x in test)
{
console.log('State in test: ' + x);
console.log('Value of ' + x + ': ' + JSON.stringify(test[x]));
}
And with that I get:
State in test: voice
Value of voice: {"state":"Ready"}
OK, so that's telling me that it contains what I'm expecting, but stringify() doesn't format it. Now, I'm left slightly confused what's going on.
customObj.setState('{ state: 'Ready' }', 'voice');
You really wanted a string???