I’m using a custom JavaScriptConverter to serialize two objects. One of them is a list of objects and it works just fine; the other one is a single object.
I serialize both and put them in the source code of the page. When I look at them HTML source in a browser, I see that the one that works looks like this:
var Object1 = '[{....}]';
while the one that doesn’t work looks like this:
var Object2 = '{...}';
When I run an eval, it doesn’t work with Object2. I’m just not seeing why the serialization is different since I’m using the same principal for both; I’m obviously doing something wrong. If you’ve run into a similar issue or have a suggestion, then please let me know.
Thanks.
You are running into a Javascript parsing ambiguity.
Instead of:
you need:
In Javascript, an open brace can start either an object literal:
or a block:
However, a block cannot appear in an expression, so adding the parentheses resolves the ambiguity.