Does it use advanced constructors (functions, new, function calls) in JSON?
JSON is a great way to pass complex data from my server side code to client side JavaScript. For example, in PHP, I can write:
<script type="text/javascript>
var MyComplexVariable = <?= BigFancyObjectGraph.GetJSON() ?>;
DoMagic(MyComplexVariable);
</script>
This is pretty cool, but sometimes you want to pass more than a base date, like dates, or even function definitions. There is a simple and easy way to do this, for example:
<script type="text/javascript>
var MyComplexVariable = {
'SimpleProperty' : 42,
'FunctionProperty' : function()
{
return 6*7;
},
'DateProperty' : new Date(989539200000),
'ArbitraryProperty' : GetTheMeaningOfLifeUniverseAndEverything()
};
DoMagic(MyComplexVariable);
</script>
And it works like a charm in all browsers I've seen so far. But according to JSON.org such syntax is not valid. On the other hand, I've seen this syntax used in so many places, including some popular JavaScript frameworks. So that...
Can I expect any problems if I use "unsupported" JSON functions like above? Why is it wrong or not?
Added clarification: . If I expected my JSON to be consumed by some unknown third party software, or even a well-known parser that was not a browser, then such exotic would most likely not work, and I did not try to implement them. But I am interested in the case where the JSON code is written directly inside a JavaScript code block that is executed by the internet browser. Like the examples above.
a source to share
According to JSON.org, JSON object only supports the following object member values:
(source: json.org )
Since none of these features are a function, I would suggest not using it as, as you said, it is not officially supported in the spec.
Also, what happens when a non-Javascript client (such as a Python program) tries to use your JSON? How will your JavaScript code work?
a source to share