4 ms·
Probably a hot take, but I'd like an extended JSON syntax where you can do this: foo = "something to use later", bar = { "a": 1.1, "b": 2.2 }, baz
by panzi 3y ago
Probably a hot take, but I'd like an extended JSON syntax where you can do this:
foo = "something to use later",
bar = { "a": 1.1, "b": 2.2 },
baz = [ 1n, 999999999999n, ],
/* comments! */
{
[foo]: `\
multiline template string
${foo}`,
...bar, /* spread! */
"baz": [ ...baz, 1234n ],
"more": my_extension("something like !foo in YAML, but just JavaScript function syntax")
}
- ghkbrew 3y agoHow is this meaningfully different than using Javascript?
- panzi 3y agoIt doesn't run actual code. The function call syntax is just for extensions like `!vault` in Ansible YAML. And yes, it is supposed to be a new JavaScript subset and JSON superset.
- moritzwarhier 3y agoI'm confused, apart from the question where you'd like to limit JS, fundamentelly with your mix of statements and and an object literal, how would you even extract a key from this? E.g., how to retrieve that nameless object literal? Maybe defining the format as one big comma expression [^1] and disallowing functions+eval would have it's merit... but then, what to do about circular references? Very creative though :) [1] So that way, the whole config would evaluate to the expression behind the last top-level comma
- LordDragonfang 3y agoI assume the nameless object literal is, to steal a term from XML, the "root element", the same as with normal JSON.
- panzi 3y agoExactly.
- panzi 3y agoJSON also has one nameless object literal. That literal is the payload. The assignments before are just things that can be referenced in the actual payload. Yes, its like a big comma expression. The idea is that it is a syntactically correct JavaScript expression. It's just a thought. Obviously haven't written a spec. I just came up with that idea when writing complex YAML configurations for things like docker-compose, OpenAPI specs, and Ansible. I want to reuse the same strings (credentials) in multiple places without repeating myself, I want to do string interpolation, multiline strings, bigint, comments, and something that enables something like Ansible's `!vault`. I DON'T want certain YAML features like yes/no for booleans, timestamps, cryptic |- and >-, and other weird things one usually doesn't think of. And I saw someone who is blind writing that they very much prefer curly braced syntax over indentation based syntax, because indentation is very hard to reason over for them in their head, which is the whole reason that makes me side with non-indentation based syntax now. Given those constraints I ad-hoc came up with that JavaScript subset. Haven't thought about it much, maybe there are issues I haven't thought of.
- panzi 3y agoOh yes, and I'd like to have hex-float for easy exact floating point representation, but not if JavaScript doesn't support it because then it wouldn't be a JavaScript subset anymore.
- moritzwarhier 3y agoYes I was also enjoying to explore the implications of the thought :) Thanks for your elaboration