kachick / times_kachick

`#times_kachick channel in chat` as a public repository. Personal Note and TODOs
https://github.com/kachick/times_kachick/issues?q=is%3Aissue+is%3Aclosed
6 stars 0 forks source link

2022-07-17 - JSON5は使わない方が良いのかなー? #181

Closed kachick closed 2 years ago

kachick commented 2 years ago

Renovate の config を書くことが多くなってきたんだけど、各ソースのURLと正規表現やらrenovateのオプションやら一般的じゃないことに対する物の集合体なのでコメントを付けたい。しかし renovate は config 管理が JSON なのでどうかなーというと、一応 JSON5 に対応している。自分の理解だと JSONC は JSON + Comment で、 JSON5 はその superset という認識。 別にコメント以上に使いたい機能は無いから JSONC で良いんだけど、これ拡張子とかで表現しないから対応してるのかしてないのかが読み込ませてみるまでわからないし、 vscode でもエラー表示になったりする。 なのではっきり拡張子で表現することにもなっている?してもよい? JSON5 で良いのかなーと思ったんだけど、config というジャンルなのにツールセットが出揃ってない感で食指が伸びない

vscode 拡張に関してはフォークしてリリースしちゃえば良い気もしつつ、そんなに気合入れてJSON5と関わっていきたいわけでも無いのでうーむむむとなる。 拡張子をjson5にして実質 JSONC に留めた書き方するというのはちょっと・・・となる。

https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.3 みたいに $comment key に巨大なコメント書こっかなーとおもったけど、この仕様に対応しているかどうかを考えるのが嫌だ・・・

https://github.com/renovatebot/renovate/blob/2b43f273bf0737ccf29b27cf55ecec69403bfdf1/lib/workers/global/config/parse/file.ts#L18-L20 見る限り json だろうと json5 だろうと json5 前提でパースしてるっぽいので、拡張子を json にして syntax は JSONC に留める事で事が荒立たず収まるという tsconfig やらなんやらでよく見る感じに寄せちゃうのが落とし所なのかな。(スッキリはしない。というか本当に superset という認識で良いのかな・・・)

renovate 本家回答としては2年前の段階で descriptionに全部書け で、先月の時点で JSON5使えになってた。

kachick commented 2 years ago

結局、renovate に関しては JSON5 を使う事にした。 vscode の plugin は カラーリングを標準のと合わせてくれる fork の https://github.com/katjanakosic/vscode-json5 を取り敢えず使った。 dprint.json の設定で単にjson5を含めると prettier 側に fallback する。そしてkey の quote を勝手に削って来るし、残す設定に変えても他を single quote にしてるとここも singlequote になったりと相変わらず融通が利かない。そもそも JSONC alt ととしてしか使う予定がないので、 dprint-plugin-json に無理やりお願いした。

https://github.com/kachick/renovate-config-asdf/blob/c4b329a0fa65f24ae80294108d3860e4e866472b/dprint.json#L2-L4

こんな感じで associations を使うと dprint さんに無理やりお願いを聞いてもらえる。

後は renovate 側の config で大分ハマった。 subdir を切ると preset 表現が使えないというのと、更に拡張子が json5 はデフォルトで fetch しないらしいので 拡張子がjsonの時は拡張子を付けない、json5の時は付けて指定するというややこしい感じだった。

https://github.com/kachick/renovate-config-asdf/blob/c4b329a0fa65f24ae80294108d3860e4e866472b/default.json

結論こう。

kachick commented 2 years ago

https://crystal-lang.org/api/1.5.0/JSON.html

crystal の json ライブラリが便利そうだけど JSONC 対応してなさそうなので Ruby もそうだろうなーと思ったら11年前にはすでに対応してたんだ・・・API返すtestの中で見る時ぐらいしかRubyでパースする事なかったから知らんかった。

https://github.com/flori/json/issues/111 https://www.ietf.org/rfc/rfc4627.txt

July 2006 A JSON parser MAY accept non-JSON forms or extensions.

https://www.ietf.org/rfc/rfc7159.txt

March 2014 A JSON parser MAY accept non-JSON forms or extensions.

Oh

尚 crystal は対応していない感

https://play.crystal-lang.org/#/r/dgao

require "json"
pp JSON.parse("{ \"someKey\":\"someValue\", /* Some comment */ \"someOtherKey\":[\"some\",\"array\"]}")
Unhandled exception: Unexpected char '/' at line 1, column 26 (JSON::ParseException)
  from /usr/lib/crystal/json/lexer.cr:331:7 in 'raise'
  from /usr/lib/crystal/json/lexer.cr:327:5 in 'unexpected_char'
  from /usr/lib/crystal/json/lexer.cr:326:11 in 'unexpected_char'
  from /usr/lib/crystal/json/lexer.cr:243:10 in 'consume_number'
  from /usr/lib/crystal/json/lexer.cr:63:7 in 'next_token'
  from /usr/lib/crystal/json/lexer.cr:74:5 in 'next_token_expect_object_key'
  from /usr/lib/crystal/json/parser.cr:109:11 in 'next_token_expect_object_key'
  from /usr/lib/crystal/json/parser.cr:91:13 in 'parse_object'
  from /usr/lib/crystal/json/parser.cr:35:7 in 'parse_value'
  from /usr/lib/crystal/json/parser.cr:13:12 in 'parse'
  from /usr/lib/crystal/json.cr:135:5 in 'parse'
  from eval:2:4 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:115:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:101:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:127:3 in 'main'
  from /usr/lib/libc.so.6 in '??'
  from /usr/lib/libc.so.6 in '__libc_start_main'
  from ../sysdeps/x86_64/start.S:117 in '_start'
  from ???

https://play.crystal-lang.org/#/r/dgaq

require "json"
pp JSON.parse("{ \"someKey\":\"someValue\",  \"someOtherKey\":[\"some\",\"array\"]}")
{"someKey" => "someValue", "someOtherKey" => ["some", "array"]}
kachick commented 2 years ago

う、 vscode の json schema サポートしてくれるやつが json5 拡張子にすると動かなくなる・・・ https://json-schema-everywhere.github.io/json5

kachick commented 1 year ago

deno-1.34.2 で deno init したら deno.jsonc が作られた。