Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(json): add option to write json in multiline mode #213

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/9-tvtojson/exampletvtojson.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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())
})
}
}