116 points · by galfarragem · 8y ago · 55 comments
Is this going full circle then? JS -> JSON -> JS.
More seriously and like someone already said, to many brackets and quotes for rapid prototyping.
The 1st party bindings are for C, Go and Python; so it a cross-language JSON DSL. And making it a strict DSL has significant advantages.
{
person: self.person,
}
RUNTIME ERROR: max stack frames exceeded.
{
"self": "a",
b: self.self
}
STATIC ERROR, expected token identifier but got self
Looks good.https://plugins.jetbrains.com/plugin/10852-jsonnet
Gives you syntax highlighting and basic jump-2-definition, though more advanced features are TODO.
You can still helm-ify a ksonnet app.
At a prior job we created extremely large, complicated CloudFormation templates in JSON that could have been made much simpler with Jsonnet and the ability to do comprehension and some other tricks to keep it DRY. A coworker ended up writing a bunch of Python scripts to templatize the CF templates (ugh).
At my current job we ended up using Jsonnet as a configuration language for Apache Aurora jobs [1], which was also helpful when trying to make it easy for others to pick it up and create their own jobs from base templates.
That being said, it can be frustrating to get going with and for some cases I do find myself having to trace through things very carefully in order to to figure out exactly what's going on.
You could also go to town with reader macros in Lisp or use Racket.
I mention this not to be pedantic, but in case it helps another coder from wasting a couple days chasing a bug like I once had to, when I thought I could template raw JSON into a script file to make some server-side data available as a global var on in the browser.
XSLT was brilliant except it was extremely verbose and difficult to use due to wanting to be expressed in XML.
XSLT as prior art demonstrates that Jsonnet made the right choice here.
For example, merging two objects is trivial in jsonnet yet pretty close to impossible in jinja2.
{{ merge(foo, bar)|tojson|safe }}
Even something polymorphic to render a template, then merge into a known dict, then output json would be easy. A signature that might look like: {{ render_json_dict(foo, "foo.json")|merge(bar)|tojson|safe. }}In the last jsonnet template I wrote, I merged a few keys into the top level, merged a few keys into a deeper level, and added some random characters to the end of a string value to make it unique. Trivial in jsonnet.
https://www.wired.com/1996/11/es-marimba/
http://www.suck.com/netmoguls/96/10/18/front.html
http://www.suck.com/netmoguls/96/10/18/back.html
Kim said: "It was fun naming stuff back then. You could actually get the trademark/website!"