json.go 520 B

123456789101112131415161718192021
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. )
  7. // encodeJSON writes v as two-space indented JSON followed by a newline.
  8. // Every JSON-emitting command goes through here so their output shape stays
  9. // identical — consumers should not have to special-case one command.
  10. func encodeJSON(w io.Writer, v any) error {
  11. data, err := json.MarshalIndent(v, "", " ")
  12. if err != nil {
  13. return fmt.Errorf("encode json: %w", err)
  14. }
  15. if _, err := fmt.Fprintln(w, string(data)); err != nil {
  16. return err
  17. }
  18. return nil
  19. }