foo: 4,
...produce the value 4, "4", or "4,"?While I have been hoping for "JSON plus comments" to be a real and common thing for quite a while now, one of the strengths of JSON is right there on json.org. See it? A set of five simple syntax diagrams that entirely and virtually unambiguously define the json syntax.
It’s tough to know when to stop when simplifying a syntax. (For an extreme example, see Stylus, which was like Sass but so extreme that mixins and properties became ambiguous for each other.) I, too, would like to see the return of quotes for string values, for increased clarity.
> When you omit quotes the string ends at the newline.
> Preceding and trailing whitespace is ignored as are escapes.
>
> A value that is a number, true, false or null in JSON is
> parsed as a value. E.g. 3 is a valid number while 3 times is
> a string.
(edit: formatting)The first time, and the second time if you haven't looked at it in a month, and a third time a month after that. And so on.
The site even has an ambiguous example: three: 4 # oops Well, is that "4 # oops" or 4? I saw no rule about ending comments, and they say that three: 3 times is a string.
I have seen no formal specification of the grammar, so we already have lot of ambiguity. Good luck to implementers...
Seriously, removing the "no quotes needed" rule would improve greatly the format. If you want to include HTML with double quotes literally, just use the multiline string format and be done.
- Accidentally left in a final comma on a list? That's okay, that only means one thing, we understand.
- Allow non-quoted keys on objects? Well, we understand JavaScript generally allows this, so we'll let it slide. This time.
- Make newline significant and define new items? Okay, are we just ignoring space efficient payloads now? Should making it space efficient mean changing formats from Hjson to json?
- Considering all terms in place of a object value a string until a newline? Are you just trolling me now? How is that more human readable? Does your spoken language not use quotes to distinguish distinct chunks of communication or something, and if so, does it use a Latin alphabet so it's off-putting when you see them?
Needless to say, I'm really confused by the reason this even exists.
we just need JSON, but with a couple things fixed up to make it nicer to use for configuration files.
Using JSON for configuration is just the whole situation of using XML for data exchange redux. One of the major points for JSON over XML for data exchange was that it was so much better because it was optimized for data, not markup. Why are we ignoring this argument now that JSON is on the other side? JSON is used for configuration because it's ubiquitous, not because it fits the problem domain well. Let's just choose a more appropriate format.
Choosing the most common set of rules for INI files (what is proposed by Wikipedia[1] is probably sufficient) would serve us MUCH better than coaxing a data interchange format into that role.
(1) lots of general tooling support, in particular you get at least decent editor support for your config file, and
(2) you can autogenerate the code needed to read your configuration into structured data without having to do any unnecessary duplication of "key names" (tags) as you have to with e.g. INI or JSON. You also get the data sturctures themselves for "free" (based on the XSD).
Alright, it's not the end of the world to not have these things, but they're both very nice to have.
[0] https://en.wikipedia.org/wiki/Algebraic_data_type [1] I should note that support for ADTs in XSD is sometimes sketchy in the various code generators, but at least the XSD specification supports it. (It could also be argued that XSD supports something even more general... which it probably shouldn't since ADTs basically cover the whole data structure space unless you go to higher kinds, inheritance and such.)
> Numbers can include Infinity, -Infinity, NaN, and -NaN.
E.g.
x: yes
>>> str(yaml['x'])
yes
>>> bool(yaml['x'])
TrueLearn from Perl. The quote operator is your friend (and I frequently lament it's omission in Bash). You could simplify it by not using the matching enclusures ({ and }, [ and ], etc). It's easy to parse. and if you keep the quoting character somewhat rare, it's not hard to read.
E.g.
{
"string" : "A string without inner quotes",
"quotes1" : q!A string "with" inner quotes!,
"quotes2" : q|A string "with" inner quotes|,
"quotes3" : q@A string "with" inner quotes@,
"quotes4" : qTA string "with" inner quotesT,
}
Edit: To be clear, I wish JavaScript had a quote operator, and JSON started with it. :/1: http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Ope...
This, on the other hand, is a 'solution' to escaping quotes that is completely mad. Using non-standard quotes, especially mixing and matching them is a disaster for readability and maintainability (using a T in your string now? need to change the quotes!). Triple quotes are just find if you want to avoid escapes, and hjson seems to support them.
Meh, Perl's solution is fine. You can throw up your hands and say it's crazy, but as a person who worked with Perl for 20 years, I've never had the problem you describe. I tend not to use the qq() or q() style quotes, but I've used s@@@ and s,,, so many times I can't count. It's really quite nice (and perfectly readable unless you do something weird like 'sxxx'.
Oh, like in C and C++ where single quotes denote a char, and double quotes a string?
Or in Perl, PHP and Ruby where double quotes interpolate, and single quotes don't?
Or in JavaScript and Python, where there's no functional difference between single and double quotes?
Or C#'s string literals which only support double quotes, but you prefix the string with @ to denote it's verbatim?
Or systems that allow repeated double quotes within a double quote string literal to stand in for an escape ("foo ""bar"" baz"), as many SQL systems do?
Or what about systems that interpolate, and the differences between what they do and do not interpolate? Variables? Escape characters? Hexadecimal escapes?
You're fooling yourself if you think it unambiguous in anything except for the language you are dealing with, and if you're within that language, who cares what you use as long as it's consistent? You learn it, and then it's unambiguous (if implemented well).
This is no different than if your language supports hex numbers (usually done through prefixing it with 0x). Those are two different ways to specify the exact same thing (a binary number!). The benefit comes from using it in the circumstance where it's appropriate. That is, where it enhances readability, not where it detracts from it.
> This, on the other hand, is a 'solution' to escaping quotes that is completely mad. Using non-standard quotes, especially mixing and matching them is a disaster for readability and maintainability
Maybe you think
"{\"foo\":\"bar\",\"baz\":\"it's that's they're we're\"}"
is fine, or you may prefer '{"foo":"bar","baz":"it\'s that\'s they\'re we\'re"}'
(if your language supports it).I prefer
q|{"foo":"bar","baz":"it's that's they're we're"}|
because I think it's clearer, and learning once that a literal q defines a new quote operator that is in effect until it's next seen is simple, easy to remember, and yields very useful readability gains.> using a T in your string now? need to change the quotes!
I included the qTT example just to show how it worked, not to endorse its use. I thought that would have been obvious from my statement "You could simplify it by not using the matching enclosures ({ and }, [ and ], etc). It's easy to parse. and if you keep the quoting character somewhat rare, it's not hard to read."
In any case, I fail to see how how that's a problem beyond any other quote character. Including that character in your string will result in a compile time error in all but the most esoteric of cases, making it easy to find.
"string with no newlines"
{A multi-line string
that can run over over many lines
and {even be} nested}
For unbalanced {} string then you would need to escape it... {escape closing brace ^} & opening brace ^{ is all you need}Trailing commas and JSON comments are are already supported in the newer browsers (try the Chrome console for instance).
Fortunately quoteless strings or optional-commas/newline-separator as proposed in Hjson will never fly. They are brittle and ambiguous. Who knows what will this get parsed as:
{
a: hello's and hi's have
'misplaced' apostrophes
b: ball: a round # and # bouncy object
c: cakes and
candy: both have sugar
# but how do I include a hash at the start of a multiline-unquoted string?
}Version 49.0.2623.112 (64-bit)
> JSON.parse('{"foo": "bar",}')
> VM124:1 Uncaught SyntaxError: Unexpected token }
Javascript object literals != JSON. JSON is a restricted subset of JS object literals (and not actually a strict subset: a JSON string can contain unescaped U+2028 "LINE SEPARATOR" and U+2029 "PARAGRAPH SEPARATOR" codepoints, a Javascript string can not)Take a look at example 2.11 in the YAML spec [2], for example, and see if you can make heads or tails of it.
[1]: https://pairlist6.pair.net/pipermail/markdown-discuss/2011-A...
(and pyYAML itself can't always parse its own output correctly...)
At least perl doesn't support this, so it's inherently insecure there, but you can always use YAML::Syck which didn't go this way.
"YAML expresses structure through whitespace. Significant whitespace is a common source of mistakes that we shouldn't have to deal with."
I can think of ONE time when that causes a problem, and that's with indentation with multi-line strings. Oh look, HJSON included that feature. That's like throwing the baby out and keeping all the bathwater.
Although admittedly I haven't had to work with YAML a lot but I have liked it when i've touched it.
There are a number of others given the length of the spec. Yaml is a complicated beast that generally has more than one way to do any given thing
Primary goals were to remove as much syntax as possible and make it play well with line-based diffs (with the hopes that someone who knows knowing about the language could resolve conflicts without getting tripped up by surrounding quotes, trailing comments, etc).
Granted, if the number of conflicts which cannot be automatically resolved is reduced by enough, then it might not matter in the grand scheme of things. However, I'd be worried that this would make "accidental" automatic resolution of semantic conflicts more common. That may be an unfounded/irrational fear, I don't know.
The thing where you can leave quotes off strings makes me nervous, especially the example where the value is HTML with its own embedded double quotes for attribute values.
Not requiring quotes on strings like that looks like an obvious vector for injection attacks. I guess Hjson isn't designed to be generated automatically, but I'd prefer a format that is easy to generate safely.
What I really want is JSON plus comments plus multi-line strings plus relaxed rules on trailing commas... While maintaining as simple and unambiguous a parsing model as possible.