diff options
author | DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com> | 2023-05-17 19:19:07 +0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-17 09:19:07 -0400 |
commit | 564d598c05824d66f071057614699dee2a427c1e (patch) | |
tree | 08a55887182f3160079d8a6da2a822c33315a82e | |
parent | 13245e003bc18836be57c429f5f672de662dfd0a (diff) | |
download | spdx-tools-564d598c05824d66f071057614699dee2a427c1e.tar.gz |
feat(json): add option to write json in multiline mode (#213)
Signed-off-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
-rw-r--r-- | examples/9-tvtojson/exampletvtojson.go | 10 | ||||
-rw-r--r-- | json/writer.go | 26 | ||||
-rw-r--r-- | json/writer_test.go | 82 |
3 files changed, 108 insertions, 10 deletions
diff --git a/examples/9-tvtojson/exampletvtojson.go b/examples/9-tvtojson/exampletvtojson.go index 5e62561..76ee808 100644 --- a/examples/9-tvtojson/exampletvtojson.go +++ b/examples/9-tvtojson/exampletvtojson.go @@ -56,8 +56,16 @@ func main() { } defer w.Close() + var opt []json.WriteOption + // you can use WriteOption to change JSON format + // uncomment the following code to test it + /* + opt = append(opt, json.Indent(" ")) // to create multiline json + opt = append(opt, json.EscapeHTML(true)) // to escape HTML characters + */ + // try to save the document to disk as JSON file - err = json.Write(doc, w) + err = json.Write(doc, w, opt...) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/json/writer.go b/json/writer.go index 88563a5..a944dcc 100644 --- a/json/writer.go +++ b/json/writer.go @@ -9,17 +9,25 @@ import ( "github.com/spdx/tools-golang/spdx/common" ) -// Write takes an SPDX Document and an io.Writer, and writes the document to the writer in JSON format. -func Write(doc common.AnyDocument, w io.Writer) error { - buf, err := json.Marshal(doc) - if err != nil { - return err +type WriteOption func(*json.Encoder) + +func Indent(indent string) WriteOption { + return func(e *json.Encoder) { + e.SetIndent("", indent) } +} - _, err = w.Write(buf) - if err != nil { - return err +func EscapeHTML(escape bool) WriteOption { + return func(e *json.Encoder) { + e.SetEscapeHTML(escape) } +} - return nil +// Write takes an SPDX Document and an io.Writer, and writes the document to the writer in JSON format. +func Write(doc common.AnyDocument, w io.Writer, opts ...WriteOption) error { + e := json.NewEncoder(w) + for _, opt := range opts { + opt(e) + } + return e.Encode(doc) } diff --git a/json/writer_test.go b/json/writer_test.go new file mode 100644 index 0000000..f00f156 --- /dev/null +++ b/json/writer_test.go @@ -0,0 +1,82 @@ +package json_test + +import ( + "bytes" + "github.com/spdx/tools-golang/json" + "github.com/spdx/tools-golang/spdx/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_Write(t *testing.T) { + tests := []struct { + name string + doc common.AnyDocument + option []json.WriteOption + want string + }{ + { + name: "happy path", + doc: spdx.Document{ + SPDXVersion: "2.3", + DocumentName: "test_doc", + }, + want: `{"spdxVersion":"2.3","dataLicense":"","SPDXID":"SPDXRef-","name":"test_doc","documentNamespace":"","creationInfo":null} +`, + }, + { + name: "happy path with Indent option", + doc: spdx.Document{ + SPDXVersion: "2.3", + DocumentName: "test_doc", + }, + option: []json.WriteOption{json.Indent(" ")}, + want: `{ + "spdxVersion": "2.3", + "dataLicense": "", + "SPDXID": "SPDXRef-", + "name": "test_doc", + "documentNamespace": "", + "creationInfo": null +} +`, + }, + { + name: "happy path with EscapeHTML==true option", + doc: spdx.Document{ + SPDXVersion: "2.3", + DocumentName: "test_doc_>", + }, + option: []json.WriteOption{json.EscapeHTML(true)}, + want: "{\"spdxVersion\":\"2.3\",\"dataLicense\":\"\",\"SPDXID\":\"SPDXRef-\",\"name\":\"test_doc_\\u003e\",\"documentNamespace\":\"\",\"creationInfo\":null}\n", + }, + { + name: "happy path with EscapeHTML==false option", + doc: spdx.Document{ + SPDXVersion: "2.3", + DocumentName: "test_doc_>", + }, + option: []json.WriteOption{json.EscapeHTML(false)}, + want: "{\"spdxVersion\":\"2.3\",\"dataLicense\":\"\",\"SPDXID\":\"SPDXRef-\",\"name\":\"test_doc_>\",\"documentNamespace\":\"\",\"creationInfo\":null}\n", + }, + { + name: "happy path with EscapeHTML==false option", + doc: spdx.Document{ + SPDXVersion: "2.3", + DocumentName: "test_doc_>", + }, + option: []json.WriteOption{json.EscapeHTML(false)}, + want: "{\"spdxVersion\":\"2.3\",\"dataLicense\":\"\",\"SPDXID\":\"SPDXRef-\",\"name\":\"test_doc_>\",\"documentNamespace\":\"\",\"creationInfo\":null}\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + buf := new(bytes.Buffer) + err := json.Write(tt.doc, buf, tt.option...) + assert.NoError(t, err) + assert.Equal(t, tt.want, buf.String()) + }) + } +} |