Skip to content

Support variables #112

Closed
Closed

Description

@piranna

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.

Activity

  1. jordanbtucker commented on Mar 12, 2016

    @jordanbtucker
    Member

    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:

    var e = 2.72;
    var pi = 355/113; // using the new operator syntax in #111
    var contact = '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.

    var options = {
      quote_keys: false, // don't put quotes around keys unless required
      quote: "'", // use single quotes
      constants: {
        e: 2.72,
        pi: 355/113, // using the new operator syntax in #111
        contact: 'Obi-Wan Kenobi <[email protected]>',
      },
    };
    
    var doc = {
      e: JSON5.useConstant('e'),
      pi: JSON5.useConstant('pi'),
      authors: [
        contact: JSON5.useConstant('contact'),
      ],
    };
    
    var json5 = 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?

  2. piranna commented on Mar 12, 2016

    @piranna
    Author

    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...

  3. jordanbtucker commented on Mar 13, 2016

    @jordanbtucker
    Member

    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.

  4. piranna commented on Mar 13, 2016

    @piranna
    Author

    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.

    Ok, I agree with that.

  5. jordanbtucker commented on Mar 14, 2016

    @jordanbtucker
    Member

    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...

    Yes, but that changed with #32.

  6. aseemk commented on Mar 14, 2016

    @aseemk
    Member

    I have to admit I'm pretty hesitant about this feature.

    • JSON(5) is already a format for describing values.
    • There's something elegant about an entire JSON(5) document being a single expression.
    • Do people really need this? Are we really solving a common problem people have? Or are we just considering adding this feature because we can?
  7. aseemk commented on Mar 14, 2016

    @aseemk
    Member

    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.

  8. aseemk commented on Mar 14, 2016

    @aseemk
    Member

    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.

  9. XGHeaven commented on Mar 14, 2016

    @XGHeaven

    If add variables support, I pretty to write js instead of json5.
    I don't like to add this support.

  10. piranna commented on Mar 14, 2016

    @piranna
    Author

    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.

  11. jordanbtucker commented on Mar 14, 2016

    @jordanbtucker
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions