{
"#if": "parserversion > 1.5",
"key": "somevalue",
"#else": "",
"key": "othervalue"
}
Thought I also don't really see any reason to include parsing directivesin JSON. {
"#if": "parserversion > 1.5",
"key": "somevalue",
"#else": "",
"key": "othervalue"
}
Thought I also don't really see any reason to include parsing directivesin JSON.I don't see any reason to include parsing directives in JSON either, but it's a wild world out there and people do all sorts of strange things. Seems that a few of those folks were working at Yahoo and made the mistake of letting Doug see their code when JSON was in prototype phase, so no JSON comments for anyone. Whoops!
Since JSON objects don't support order or repeated keys, that syntax can't be represented, edited or processed by the rich ecosystem of JSON tools. Most decent JSON editors will show that text with squigly red underlines. It's not worth giving up interoperability, and having to make yet another new set of tools for an incompatible syntax.
That was the mistake that the Angular 2 template syntax made.
But XML-based templating languages like Genshi [1] show how you can obey the rules of XML, use namespaces correctly, support element, attribute and text based expressions, looping, logic and macros, and it works just fine and interoperates perfectly with existing tools.
Genshi was based on another Python based XML templating system called Kid [2], which itself was influenced by Zope's page templates, TAL template attribute language, TALES expressions [3] and METAL templates [4]. Genshi and Kid templates are simple and easy to use compared to the conglomeration of Zope stuff.
Here is the essential trick, described in the Zope manual, which all those languages share, that makes it possible to sidestep the fact that attributes are not ordered:
When there is only one TAL statement per element, the order in which they are executed is simple. Starting with the root element, each element’s statements are executed, then each of its child elements is visited, in order, to do the same.
Any combination of statements may appear on the same elements, except that the content and replace statements may not appear together.
Due to the fact that TAL sees statements as XML attributes, even in HTML documents, it cannot use the order in which statements are written in the tag to determine the order in which they are executed. TAL must also forbid multiples of the same kind of statement on a single element, so it is sufficient to arrange the kinds of statement in a precedence list.
When an element has multiple statements, they are executed in this order:
1) define
2) condition
3) repeat
4) content or replace
5) attributes
6) omit-tag
It would be great to have a Genshi-like templating language for JSON, tightly integrated with JavaScript the same way Genshi is integrated with Python.[1] https://genshi.edgewall.org/
[2] http://turbogears.org/1.0/docs/GettingStarted/Kid.html
[3] https://docs.zope.org/zope2/zope2book/AppendixC.html
[4] https://docs.zope.org/zope2/zope2book/AppendixC.html#metal-o...
But… that already exists. It's called "Python". Just define your data structure using bog-standard Python and serialise it.
Zope has this supposedly "restricted subset" of Python that you're allowed to use as expressions and scripts, but it's missing important features and isn't meaningfully safe from a paranoid security perspective, and you end up having to drop down to lower level Zope external methods to write real Python code, which is very inconvenient.
If you trust someone enough to give them access to editing templates with "restricted python expressions", then you can probably trust them enough to use real Python expressions. You'd be unwise to give somebody you don't actually trust access to even a "restricted subset" of Python running on your server. That's just asking for trouble.
… it's the exact same process except with Javascript as the language? Generate your JS datastructure then JSON.stringify it? I don't understand what the issue is or why you'd want a templating language when much of JSON's point is that it matches directly to common standard datastructures.