You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue to discuss add variables as proposed at #106.
I think we should not think as variables but instead as constants, at least by the moment since they could be easily replaced. There could not be compatibility issues because it's just a parsing issue, because serialization remains the same as JSON (in this case, constants and variables would have been resolved to their final literal value). Since this is only about parsing, we can set it at beggining of the json5 serialized string before the json5 objects so they are available later, and in a next iteration we could think about how we could be able to allow to define them in several levels hidding the outer ones.
Thanks for this write-up. I agree with everything you've said. Here's what I'd like to see in this discussion:
Use cases (why we would implement this)
JSON5 document examples (how this would look in a file)
API (how we generate a document with constants)
I can see how this might be useful in configuration files (similar to how LESS and SASS use them) but I'm struggling to see a data serialization use case.
My first thoughts on how this would look in a document is this:
vare=2.72;varpi=355/113;// using the new operator syntax in #111varcontact='Obi-Wan Kenobi <[email protected]>';{e: e,pi: pi,authors: [contact,],}
For the API, I have some questions. The API wouldn't be able to generate a document that uses variables. It could declare them, but not use them without some special function.
Maybe it would work something like this. Add another argument to stringify (or modify an existing one to use some kind of config format) and pass it an object of name/value pairs. The names must be valid identifiers or it will throw. Add a special JSON5 function (e.g. JSON5.useConstant()) that lets you use a constant name in your generated document.
varoptions={quote_keys: false,// don't put quotes around keys unless requiredquote: "'",// use single quotesconstants: {e: 2.72,pi: 355/113,// using the new operator syntax in #111contact: 'Obi-Wan Kenobi <[email protected]>',},};vardoc={e: JSON5.useConstant('e'),pi: JSON5.useConstant('pi'),authors: [contact: JSON5.useConstant('contact'),],};varjson5=JSON5.stringify(doc,options);
For the parse API, does it parse the constants into values before it gives it to the revivier, or does it notify the reviver that a constant was used?
Besides the fact I would use const instead of var because this last one has the implicit concept that they could be change later (and I think it's something we don't want to allow), I had in mind everything exactly as how you've put it here, +1000 for me :-)
For the parse API, does it parse the constants into values before it gives it to the revivier, or does it notify the reviver that a constant was used?
I would transparently parse the constants. I think constants is more oriented towards make it easier to hand-written JSON5 files instead of generate them programatically, since the stringify will be generate valid JSON files, so constants are mostly an implementation detail...
The reason I didn't use the word const is because it's not part of the ES5 spec, so using it would break compatibility with ES5. It is part of the ES2015 spec though.
Also, JSON5.stringify doesn't produce valid JSON documents, it produces valid JSON5 documents.
I'm still on the fence about whether the API should allow generating documents with constants, but we could certainly implement constants without it until there is demand.
The reason I didn't use the word const is because it's not part of the
ES5 spec, so using it would break compatibility with ES5. It is part of the
ES2015 spec though.
Good point, ok then.
Also, JSON5.stringify doesn't produce valid JSON documents, it produces
valid JSON5 documents.
Wasn't it the intention that stringify produce valid JSON documents at
least originally, and JSON5 only offered more flexibility? At least I
remember so...
I'm still on the fence about whether the API should allow generating
documents with constants, but we could certainly implement constants
without it until there is demand.
Wasn't it the intention that stringify produce valid JSON documents at
least originally, and JSON5 only offered more flexibility? At least I
remember so...
As always, I did want to add: I appreciate the thoughtful discussion, however. And suggestions for improvement are always welcome. =)
I just feel part of my (limited) role is to be a steward for keeping this project tightly scoped. It's already an uphill battle in many places to use JSON5 at all — even though all it adds (so far) is mainly just comments, unquoted keys, and trailing commas. Crossing the always ambiguous "too much" line will start moving it towards YAML, and JSON5 has always had a selling point that it's simpler.
Re: JSON5.stringify specifically: I might be forgetting some earlier discussions, but I still often wonder: what really is JSON5.stringify's purpose — and what should it be?
The problem JSON5 set out to solve was "make it easier and more human-friendly to write JSON out by hand", with common use cases like config files and declarative data. "Write" == parse.
Tooling is a legitimate and great need: it's really helpful if tools can parse human input, amend it in some way, and output it back again. E.g. IDE auto-formatting, npm install --save, etc. But we'll never have this ability as long as comments are stripped, which JSON5.parse does. So if we really want "parse the JSON5 document in its full original syntax", JSON5.parse is not it — so JSON5.stringify shouldn't be considered the counterpart to it. (Another angle: as long as JSON5.stringify takes data, e.g. a JS object, and not a syntax tree, it doesn't serve this purpose.)
Similar to my point about variables, what problem is JSON5.stringify really trying to solve?
Thanks for the discussion again! Looking forward to responses.
The problem JSON5 set out to solve was "make it easier and more human-friendly to write JSON out by hand", with common use cases like config files and declarative data. "Write" == parse.
Totally agree.
But we'll never have this ability as long as comments are stripped, which JSON5.parse does.
If that's the case, then comments should be preserved. Don't know about whitespaces, but probably they should be preserved too, but this will make the parser too much more difficult...
If add variables support, I pretty to write js instead of json5.
That would make sense if we export the content instead of left it as a statement... besides that, I don't think it's too much of a problem to have constants (not variables) in json5.
TBH, I didn't expect this to get implemented, but I thought it would be good to have a conversation about it. I'm in full agreement with @aseemk on this.
Activity
jordanbtucker commentedon Mar 12, 2016
Thanks for this write-up. I agree with everything you've said. Here's what I'd like to see in this discussion:
I can see how this might be useful in configuration files (similar to how LESS and SASS use them) but I'm struggling to see a data serialization use case.
My first thoughts on how this would look in a document is this:
For the API, I have some questions. The API wouldn't be able to generate a document that uses variables. It could declare them, but not use them without some special function.
Maybe it would work something like this. Add another argument to
stringify(or modify an existing one to use some kind of config format) and pass it an object of name/value pairs. The names must be valid identifiers or it will throw. Add a special JSON5 function (e.g.JSON5.useConstant()) that lets you use a constant name in your generated document.For the
parseAPI, does it parse the constants into values before it gives it to therevivier, or does it notify the reviver that a constant was used?piranna commentedon Mar 12, 2016
Besides the fact I would use
constinstead ofvarbecause this last one has the implicit concept that they could be change later (and I think it's something we don't want to allow), I had in mind everything exactly as how you've put it here, +1000 for me :-)I would transparently parse the constants. I think constants is more oriented towards make it easier to hand-written JSON5 files instead of generate them programatically, since the stringify will be generate valid JSON files, so constants are mostly an implementation detail...
jordanbtucker commentedon Mar 13, 2016
The reason I didn't use the word
constis because it's not part of the ES5 spec, so using it would break compatibility with ES5. It is part of the ES2015 spec though.Also,
JSON5.stringifydoesn't produce valid JSON documents, it produces valid JSON5 documents.I'm still on the fence about whether the API should allow generating documents with constants, but we could certainly implement constants without it until there is demand.
piranna commentedon Mar 13, 2016
Good point, ok then.
Wasn't it the intention that stringify produce valid JSON documents at
least originally, and JSON5 only offered more flexibility? At least I
remember so...
Ok, I agree with that.
jordanbtucker commentedon Mar 14, 2016
Yes, but that changed with #32.
aseemk commentedon Mar 14, 2016
I have to admit I'm pretty hesitant about this feature.
aseemk commentedon Mar 14, 2016
Whoops, submitted too early.
As always, I did want to add: I appreciate the thoughtful discussion, however. And suggestions for improvement are always welcome. =)
I just feel part of my (limited) role is to be a steward for keeping this project tightly scoped. It's already an uphill battle in many places to use JSON5 at all — even though all it adds (so far) is mainly just comments, unquoted keys, and trailing commas. Crossing the always ambiguous "too much" line will start moving it towards YAML, and JSON5 has always had a selling point that it's simpler.
aseemk commentedon Mar 14, 2016
Re:
JSON5.stringifyspecifically: I might be forgetting some earlier discussions, but I still often wonder: what really isJSON5.stringify's purpose — and what should it be?parse.npm install --save, etc. But we'll never have this ability as long as comments are stripped, whichJSON5.parsedoes. So if we really want "parse the JSON5 document in its full original syntax",JSON5.parseis not it — soJSON5.stringifyshouldn't be considered the counterpart to it. (Another angle: as long asJSON5.stringifytakes data, e.g. a JS object, and not a syntax tree, it doesn't serve this purpose.)JSON5.stringifyreally trying to solve?Thanks for the discussion again! Looking forward to responses.
XGHeaven commentedon Mar 14, 2016
If add variables support, I pretty to write
jsinstead ofjson5.I don't like to add this support.
piranna commentedon Mar 14, 2016
Totally agree.
If that's the case, then comments should be preserved. Don't know about whitespaces, but probably they should be preserved too, but this will make the parser too much more difficult...
That would make sense if we export the content instead of left it as a statement... besides that, I don't think it's too much of a problem to have constants (not variables) in json5.
jordanbtucker commentedon Mar 14, 2016
TBH, I didn't expect this to get implemented, but I thought it would be good to have a conversation about it. I'm in full agreement with @aseemk on this.