-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow leading comma and empty values in arrays #19
Comments
I'm in favor of allowing this. Reason being is that there is no other way to describe "nth item of Array" except for having null (or empty) spots. For instance; Say I am doing some validation on an array that I want to only have numbers in: [
45,
18,
"eleventeen",
65535,
"the end"
] I'd like to respond with something like: [
,
,
"Numbers only please",
,
"Numbers only please"
] I asked Crockford why he didn't allow this (it has been allowed in JS for a VERY long time) and he said it was because not all languages handle sparse arrays. Because javascript doesn't allocate based on the length of an array- it is easy to have a seemingly huge array: var x = [];
x.length = 4294967295; //Chrome's limit Indeed, it could be trouble for a |
@williamwicks: Are you saying you specifically want the array to contain JSON.stringify([,,,])
# "[null,null,null]" Does that work fine for you? |
Yes. It seems wrong for serialization to change the representation of the object. For example: var original = [,,,];
var stringified = JSON.stringify(original); // "[null,null,null]"
var parsed = JSON.parse(stringified);
assert.strictEqual(parsed[1], original[1])
// AssertionError: null === "undefined" Using
That being the case, it seems this should be acceptable. It is the only representation that allows it to be One other thing to point out to anyone that comes along and reads this: To hold true to how Javascript handles [,,,].length; // 3
[1,2,3,].length; // 3
[1,2,,4].length; // 4 Thanks for considering it! |
That's a good point about the representation changing, but ultimately I think what this boils down to is adding the notion of (This also fits the "adds no new data types" part of the mission statement.) And without So ultimately it seems like your best bet is to explicitly include |
But I suspect it would most likely end up like this...
Languages that do have
Yes- explicitly injecting |
as @william mentions, i think care has to be taken for breaking
Jason Swearingen Technical Director: Novaleaf Softwarehttp://www.Novaleaf.comJasonS@Novaleaf.com Cell: +66-089-775-0111 Office: +66-02-234-3031 Fax: +66-02-234-3032On Mon, Jul 29, 2013 at 3:20 AM, William Wicks notifications@github.comwrote:
|
Lemme re-open this case for consideration. |
I move to continue disallowing leading commas and empty values. And I apologize for the following novel. There's a TL;DR at the bottom. :) One of the headaches of JSON is that it doesn't allow trailing commas. For instance, you start to write a JSON file, and like any normal JavaScript developer, you add a comma after each value because it makes it easier to copy and paste and move values around. {
"end_of_line": "lf",
"indent_style": "space",
"indent_size": 4,
} Of course, JSON throws {
"end_of_line": "lf"
,"indent_style": "space"
,"indent_size": 4
} It gets the job done, but it's ugly IMO, and it creates another problem. If you remove the first value and forget to remove the proceeding comma, you get Fast forward to today. You start using JSON5 because trailing commas are awesome and should have been allowed by JSON in the first place. Even better, you don't have to change your current JSON files because JSON5 is completely backward compatible. And that's great because you have a lot of JSON files using the hacky leading comma syntax. You edit one of your old JSON files, removing the first value, and you forget to also remove the proceeding comma because you're involved in several projects, overworked, and having trouble paying attention to detail. But that's okay because JSON5 throws Actually, JSON5 throws TL;DR If JSON5 allowed leading commas and empty values, old JSON files that used to throw would no longer do so, which could introduce silent and/or hard to find bugs. Also, Thoughts? |
Very interesting point @jordanbtucker! But I'm not sure I totally buy this argument:
Because there are plenty of examples where invalid JSON (that would throw) is valid JSON5 — e.g. unquoted keys, comments, even trailing commas as you mention. =) — I do agree that this still feels like it'd implicitly be adding a new data type. Your suggestion of leaving the element types unspecified/"empty" is an interesting one though, @williamwicks. On the notion of round-tripping, @jasons-novaleaf, I'm still a bit unconvinced, because both native JSON and JSON3 fail the round-trip test here too. So this isn't new to JSON5. At this point, I'm in agreement with @jordanbtucker that I don't think we should add this. |
Hey, everyone. I'm going to close this issue because it is incompatible with the official specification. If you would like to continue this discussion, please open an issue on the official specification repository. Please see the new issues template for more information. Thanks. |
Edit: the original title of this case was to disallow these things, considered bugs. That was fixed, but now the conversation below asks to consider re-allowing them.
ES5 allows these:
But I don't think that's needed in JSON5. Let's keep it simple and closer to JSON here.
This implementation currently allows these (by design); let's fix.
The text was updated successfully, but these errors were encountered: