ahrefs/atd

Make it easier to use hyphen-minus to separate words in JSON keys

IagoAbal opened this issue · 1 comments

That seems to be the standard naming convention for JSON so ideally it should be easy to follow that convention with ATD. However right now it is quite cumbersome to get this:

type t = {
  ~foo_bar <ocaml default="true"> <json name="foo-bar"> : bool;
...

Basically you need to write each identifier twice, one time using underscores and another time using hyper minus as separator.

It seems to me that it might be a more common practice with YAML than with JSON. It's clearly not standard practice with JSON. One reason could be that foo_bar doesn't require quotes in JavaScript but foo-bar does, making it more cumbersome to produce objects from JavaScript.

Since this is a convention that affects all the fields of objects in a given API, it would be best handled with a global option. ATD supports annotations at the beginning of the file. I suggest adding support for the following <json field_name_style="dashes"> annotation:

<json field_name_style="dashes">

type t = {
  a_b : int;
  c_D_e: int;
  f_g <json name="f_g">: int;
}

It would produce JSON data like this:

{
  "a-b": 42,
  "c-D-e": 17,
  "f_g": 13
}

While we're at it, we should also add options to make enums all-lowercase or all-uppercase since the default is to use the OCaml capitalized style Foo instead of the more common foo or FOO.