github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/spdxjson/encoder.go (about)

     1  package spdxjson
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  
     7  	"github.com/nextlinux/gosbom/gosbom/formats/common/spdxhelpers"
     8  	"github.com/nextlinux/gosbom/gosbom/sbom"
     9  	"github.com/spdx/tools-golang/convert"
    10  	"github.com/spdx/tools-golang/spdx/v2/v2_2"
    11  )
    12  
    13  func encoder2_3(output io.Writer, s sbom.SBOM) error {
    14  	doc := spdxhelpers.ToFormatModel(s)
    15  	return encodeJSON(output, doc)
    16  }
    17  
    18  func encoder2_2(output io.Writer, s sbom.SBOM) error {
    19  	doc := spdxhelpers.ToFormatModel(s)
    20  
    21  	var out v2_2.Document
    22  	err := convert.Document(doc, &out)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	return encodeJSON(output, out)
    28  }
    29  
    30  func encodeJSON(output io.Writer, doc interface{}) error {
    31  	enc := json.NewEncoder(output)
    32  	// prevent > and < from being escaped in the payload
    33  	enc.SetEscapeHTML(false)
    34  	enc.SetIndent("", " ")
    35  
    36  	return enc.Encode(doc)
    37  }