Skip to content

Commit

Permalink
feat(json): add option to write json in multiline mode (#213)
Browse files Browse the repository at this point in the history
Signed-off-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
  • Loading branch information
DmitriyLewen committed May 17, 2023
1 parent 13245e0 commit 564d598
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 10 deletions.
10 changes: 9 additions & 1 deletion examples/9-tvtojson/exampletvtojson.go
Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions json/writer.go
Expand Up @@ -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)
}
82 changes: 82 additions & 0 deletions 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())
})
}
}

0 comments on commit 564d598

Please sign in to comment.