writer.go 674 B

123456789101112131415161718192021222324252627282930313233
  1. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  2. package json
  3. import (
  4. "encoding/json"
  5. "io"
  6. "github.com/spdx/tools-golang/spdx/common"
  7. )
  8. type WriteOption func(*json.Encoder)
  9. func Indent(indent string) WriteOption {
  10. return func(e *json.Encoder) {
  11. e.SetIndent("", indent)
  12. }
  13. }
  14. func EscapeHTML(escape bool) WriteOption {
  15. return func(e *json.Encoder) {
  16. e.SetEscapeHTML(escape)
  17. }
  18. }
  19. // Write takes an SPDX Document and an io.Writer, and writes the document to the writer in JSON format.
  20. func Write(doc common.AnyDocument, w io.Writer, opts ...WriteOption) error {
  21. e := json.NewEncoder(w)
  22. for _, opt := range opts {
  23. opt(e)
  24. }
  25. return e.Encode(doc)
  26. }