sq/cli/output/jsonw
Neil O'Toole a5c4f9d6bd
#263: Inspect --overview; improved output (#273)
* Refactor tablew md writer

* wip: cmd inspect -v

* cmd inspect output improved

* cmd inspect tests

* cmd inspect: help text
2023-06-21 23:48:58 -06:00
..
internal Use flag --compact instead of --pretty (#216) 2023-05-05 11:41:22 -06:00
configwriter.go jsonw & yamlw: sq config -v: print options even if empty 2023-05-07 02:15:02 -06:00
encode.go record.Record elements are now values, not pointers (#243) 2023-05-27 07:57:07 -06:00
errorwriter.go Use flag --compact instead of --pretty (#216) 2023-05-05 11:41:22 -06:00
jsonw_test.go record.Record elements are now values, not pointers (#243) 2023-05-27 07:57:07 -06:00
jsonw.go Use flag --compact instead of --pretty (#216) 2023-05-05 11:41:22 -06:00
metadatawriter.go #263: Inspect --overview; improved output (#273) 2023-06-21 23:48:58 -06:00
pingwriter.go Refactoring (#206) 2023-04-21 21:36:32 -06:00
README.md #199: More config/options work (#215) 2023-05-05 08:32:50 -06:00
recordwriter.go #261: jsonw: deadlock occurred when record was large, due to mutex on Flush() call (#262) 2023-06-19 10:55:38 -06:00
sourcewriter.go #199: More config/options work (#215) 2023-05-05 08:32:50 -06:00
versionwriter.go #263: sq version host info (#271) 2023-06-21 06:28:15 -06:00

Package jsonw

Package jsonw implements JSON output writers.

Note that there are three implementations of output.RecordWriter.

  • NewStdRecordWriter returns a writer that outputs in "standard" JSON.
  • NewArrayRecordWriter outputs each record on its own line as an element of a JSON array.
  • NewObjectRecordWriter outputs each record as a JSON object on its own line.

These RecordWriters correspond to the --json, --jsona, and --jsonl flags (where jsonl means "JSON Lines"). There are also other writer implementations, such as an output.ErrorWriter and an output.MetadataWriter.

Standard JSON --json:

[
  {
    "actor_id": 1,
    "first_name": "PENELOPE",
    "last_name": "GUINESS",
    "last_update": "2020-06-11T02:50:54Z"
  },
  {
    "actor_id": 2,
    "first_name": "NICK",
    "last_name": "WAHLBERG",
    "last_update": "2020-06-11T02:50:54Z"
  }
]

JSON Array --jsona:

[1, "PENELOPE", "GUINESS", "2020-06-11T02:50:54Z"]
[2, "NICK", "WAHLBERG", "2020-06-11T02:50:54Z"]

Object aka JSON Lines --jsonl:

{"actor_id": 1, "first_name": "PENELOPE", "last_name": "GUINESS", "last_update": "2020-06-11T02:50:54Z"}
{"actor_id": 2, "first_name": "NICK", "last_name": "WAHLBERG", "last_update": "2020-06-11T02:50:54Z"}

Notes

At the time of development there was not a JSON encoder library available that provided the functionality that sq required. These requirements:

  • Optional colorization
  • Optional pretty-printing (indentation, spacing)
  • Preservation of the order of record fields (columns).

For the RecordWriter implementations, given the known "flat" structure of a record, it was relatively straightforward to create custom writers for each type of JSON output.

For general-purpose JSON output (such as metadata output), it was necessary to modify an existing JSON library to provide colorization (and also on-the-fly indentation). After benchmarking, the segmentio.io encoder was selected as the base library. Rather than a separate forked project (which probably would not make sense to ever merge with its parent project), the modified encoder is found in jsonw/internal/jcolorenc.