jq icon indicating copy to clipboard operation
jq copied to clipboard

Define format in jq code

Open dbohdan opened this issue 9 years ago • 9 comments

The proposed change makes it easier to extend format with new formats (#1227). The reasoning is that it is often more straightforward to implement a serialization algorithm recursively in a high-level functional language than in C.

I've implemented serialization to Tcl dictionaries (#685) in pure jq code as an example. This version differs slightly from the one in #685 to work around forward slash (\) escaping issues in src/builtin.jq.

Perhaps the function totcl should be renamed _totcl. Edit: Done.

dbohdan avatar Sep 04 '16 21:09 dbohdan

Coverage Status

Coverage increased (+0.02%) to 85.35% when pulling 19f2e61c997347481d02ae73370cfe5204980d0c on dbohdan:format-in-jq into 0b8218515eabf1a967eba0dbcc7a0e5ae031a509 on stedolan:master.

coveralls avatar Sep 04 '16 21:09 coveralls

Coverage Status

Coverage increased (+0.02%) to 85.35% when pulling 7ec1d73351e1a45e5193db3f4bb0038623eeb111 on dbohdan:format-in-jq into 0b8218515eabf1a967eba0dbcc7a0e5ae031a509 on stedolan:master.

coveralls avatar Sep 05 '16 10:09 coveralls

Coverage Status

Coverage increased (+0.4%) to 85.769% when pulling a1c93d9e1c6b4171ac83efa66c4bd86929a4990a on dbohdan:format-in-jq into 0b8218515eabf1a967eba0dbcc7a0e5ae031a509 on stedolan:master.

coveralls avatar Sep 05 '16 10:09 coveralls

Hi. This is a wonderful contribution. I may leave totcl that way instead of as _totcl; not sure. I'll try to get to merging this this coming weekend. Thanks!

nicowilliams avatar Jan 23 '17 19:01 nicowilliams

Great!

dbohdan avatar Jan 23 '17 20:01 dbohdan

I've realized there is a corner case that can cause @tcl to produce invalid Tcl data. I'll make a test for it and produce a patch shortly.

dbohdan avatar Jan 23 '17 21:01 dbohdan

Done. Now @tcl quotes data the way Tcl itself does. I've added tests for some corner cases that the earlier implementation failed to process correctly. I didn't rename _totcl back to totcl to leave that up to you.

I forgot to say that I appreciate you complimenting this change. I am a fan of the purely functional language that jq has grown.

dbohdan avatar Jan 24 '17 07:01 dbohdan

Ping. Any updates on this?

dbohdan avatar Sep 16 '17 08:09 dbohdan

I'd love to see this in jq. I could see this formatter plugin being used to output YAML, hocon, dhall, and some others that are effectively supersets of JSON.

colindean avatar Nov 04 '21 21:11 colindean