Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

RJSON #13

Closed
sebmck opened this issue Feb 20, 2020 · 17 comments
Closed

RJSON #13

sebmck opened this issue Feb 20, 2020 · 17 comments
Labels
A-Documentation Area: documentation

Comments

@sebmck
Copy link
Contributor

sebmck commented Feb 20, 2020

Rome contains a custom JSON parser at packages/@romejs/codec-json. The primary motivation for building this was JSON.parse does not emit detailed information for syntax errors. So I decided to build a vanilla JSON parser with detailed errors that emits Rome diagnostics (#3).

Once I had this built, it was quite easy to add JSON extensions. So I did. They are only enabled when a filename is passed in that ends in .rjson, so the parser can parse with and without.

These extensions do not add additional data types and are purely syntactic. When using extensions with a regular JSON file, the error messages will specifically mention it.

The features of RJSON are as followed.

Unquoted keys

If a property key is a valid identifier then the quotes can be omitted, just like in regular JavaScript.

{
  unquotedKey: true
}

Optional commas

Commas are not required to separate elements of an array or object properties.

Example

[
  1
  2
  3
]
{
  a: 1
  b: 2
  c: 3
}

Numeric separators

With the addition of numeric separators in JavaScript, support has been added in RJSON.

Example

5_000

Comments

Standard JS comments are supported. Both line and block comments.

The comment support also allows the manipulation of an RJSON file, while retaining comments.

Example

{
  // Line comment
  /* Block comment */
  foo: "bar"
}

Implicit top level object

Curly braces at the top level aren't required if it's just a list of properties.

Empty input is treated as an empty implicit object.

Example

foo: "bar"
"bar": "foo"
@sebmck sebmck added the A-Documentation Area: documentation label Feb 20, 2020
@sebmck sebmck mentioned this issue Feb 20, 2020
@Haroenv
Copy link
Contributor

Haroenv commented Feb 27, 2020

What is the reason you added no-comma support? Three others are js features, so that makes sense, but I haven’t seen commaless before. Is it to be closer to yaml/rc?

@sebmck
Copy link
Contributor Author

sebmck commented Feb 27, 2020

I added optional commas because they can unambiguously be added. I started with just omitting all the syntax I could while still feeling JSONish. Colons could also theoretically be made optional but I feel like that's stepping over "the line".

@ishpartko
Copy link

ishpartko commented Feb 27, 2020

I added optional commas because they can unambiguously be added. I started with just omitting all the syntax I could while still feeling JSONish. Colons could also theoretically be made optional but I feel like that's stepping over "the line".

Why do you need to create another data format when there are formats already familiar to the people? Like .js/.ts/.json/.yaml

It's good when people read about this new format, but usually it's easier for people who work a lot to use the familiar syntaxes.

And it will be easier for code editors, they are more comfortable dealing with familiar syntaxes than with "erroneous json"/ "plain text" in their opinion.

What do you think, @sebmck ?

@sebmck
Copy link
Contributor Author

sebmck commented Feb 28, 2020

You can already use .json. There does not exist a JS parser and stringifier today for YAML or another format that has the ability to retain comments, which I see as the sole motivation for having a custom JSON format.

@talentlessguy
Copy link

json5 support for configs would be cool too

https://json5.org/

@sebmck
Copy link
Contributor Author

sebmck commented Feb 29, 2020

JSON5 does not allow for the reserialization of files and retaining comments. Are you proposing that it be reimplemented?

@talentlessguy
Copy link

@sebmck

maybe I got you wrong but:

json5 allows making comments

also what does "reserialization" mean?

@talentlessguy
Copy link

talentlessguy commented Mar 1, 2020

sorry, for getting away from the issue topic, but

another cool format that I just remembered, Toml

npm package | toml repo

I suggest TOML because unlike yml or json it is actually readable and simple. Example

[dev_server]
port = 3000
host = '127.0.0.1'

@stephenwf
Copy link

@talentlessguy This I believe: https://codesandbox.io/s/recursing-star-33xfn when you parse you only have the option of getting back an object representation. consumeJSONExtra in RJSON implementation keeps the comments.

@sebmck
Copy link
Contributor Author

sebmck commented Mar 1, 2020

@talentlessguy

also what does "reserialization" mean?

Reading a file, changing something, and then reserializing it back and retaining the original comments. Exactly what @stephenwf said.

@sebmck
Copy link
Contributor Author

sebmck commented Mar 1, 2020

@talentlessguy Do you know of any JavaScript tooling that uses TOML? Previously Rome did actually use TOML but after reading https://hitchdev.com/strictyaml/why-not/toml/ and seeing it's extremely limited usage in the JavaScript community I pivoted. I know it sounds contradictory since RJSON is some unique-only-for-Rome JSON, but I would consider syntax omission to not be as big of a deal.

@sebmck
Copy link
Contributor Author

sebmck commented Mar 1, 2020

RJSON adds the same syntax extensions as JSON5 except RJSON allows omitting syntax and you cannot use single quotes.

@sebmck
Copy link
Contributor Author

sebmck commented Mar 1, 2020

This article resonated strongly and motiviated some of the syntax omission such as top-level curlies etc https://www.lucidchart.com/techblog/2018/07/16/why-json-isnt-a-good-configuration-language/

@talentlessguy
Copy link

@talentlessguy Do you know of any JavaScript tooling that uses TOML? Previously Rome did actually use TOML but after reading https://hitchdev.com/strictyaml/why-not/toml/ and seeing it's extremely limited usage in the JavaScript community I pivoted. I know it sounds contradictory since RJSON is some unique-only-for-Rome JSON, but I would consider syntax omission to not be as big of a deal.

oh I see. As I understood Rome config will be very nested and big, so TOML won't work :/. So we have to use something json-like

@talentlessguy
Copy link

@stephenwf @sebmck

hm ok I see that json5 doesn't save comments. But why Rome should save comments in the configuration? Shouldn't they be erased after config consumption?

@sebmck
Copy link
Contributor Author

sebmck commented Mar 1, 2020

Rome has the ability to modify the config and then save it. This allows us to have commands like rome config enable-category lint and have it load, modify, and save while retaining comments.

@MrHIDEn
Copy link

MrHIDEn commented Mar 27, 2020

This article resonated strongly and motiviated some of the syntax omission such as top-level curlies etc https://www.lucidchart.com/techblog/2018/07/16/why-json-isnt-a-good-configuration-language/

In that article you mentioned there is HJSON format, very similar to yours RJSON. I agree with you with the idea of RJSON. I believe it is too late for that format but it is interesting thing.

@sebmck sebmck closed this as completed Apr 30, 2020
sebmck pushed a commit that referenced this issue May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Documentation Area: documentation
Projects
None yet
Development

No branches or pull requests

6 participants