diff --git a/examples/14-tvtomultilinejson/exampletvtomultilinejson.go b/examples/14-tvtomultilinejson/exampletvtomultilinejson.go new file mode 100644 index 0000000..a545b48 --- /dev/null +++ b/examples/14-tvtomultilinejson/exampletvtomultilinejson.go @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +// Example for: *tagvalue*, *json* + +// This example demonstrates loading an SPDX tag-value file from disk into memory, +// and re-saving it to a different multiline json file on disk. +// Run project: go run exampletvtomultilinejson.go ../sample-docs/tv/hello.spdx example.json +package main + +import ( + "fmt" + "os" + + "github.com/spdx/tools-golang/json" + "github.com/spdx/tools-golang/tagvalue" +) + +func main() { + + // check that we've received the right number of arguments + args := os.Args + if len(args) != 3 { + fmt.Printf("Usage: %v \n", args[0]) + fmt.Printf(" Load SPDX tag-value file , and\n") + fmt.Printf(" save it out to multiline JSON .\n") + return + } + + // open the SPDX file + fileIn := args[1] + r, err := os.Open(fileIn) + if err != nil { + fmt.Printf("Error while opening %v for reading: %v", fileIn, err) + return + } + defer r.Close() + + // try to load the SPDX file's contents as a tag-value file + doc, err := tagvalue.Read(r) + if err != nil { + fmt.Printf("Error while parsing %v: %v", args[1], err) + return + } + + // if we got here, the file is now loaded into memory. + fmt.Printf("Successfully loaded %s\n", args[1]) + + // we can now save it back to disk, using jsonsaver. + + // create a new file for writing + fileOut := args[2] + w, err := os.Create(fileOut) + if err != nil { + fmt.Printf("Error while opening %v for writing: %v", fileOut, err) + return + } + defer w.Close() + + // try to save the document to disk as multiline JSON file + err = json.WriteMultiline(doc, w) + if err != nil { + fmt.Printf("Error while saving %v: %v", fileOut, err) + return + } + + // it worked + fmt.Printf("Successfully saved %s\n", fileOut) +} diff --git a/json/writer.go b/json/writer.go index 88563a5..a452d48 100644 --- a/json/writer.go +++ b/json/writer.go @@ -23,3 +23,18 @@ func Write(doc common.AnyDocument, w io.Writer) error { return nil } + +// WriteMultiline takes an SPDX Document and an io.Writer, and writes the document to the writer in JSON format with indents (multiline format). +func WriteMultiline(doc common.AnyDocument, w io.Writer) error { + buf, err := json.MarshalIndent(doc, "", " ") + if err != nil { + return err + } + + _, err = w.Write(buf) + if err != nil { + return err + } + + return nil +} diff --git a/spdx/v2/v2_2/json/json_test.go b/spdx/v2/v2_2/json/json_test.go index b2411a8..a4ce774 100644 --- a/spdx/v2/v2_2/json/json_test.go +++ b/spdx/v2/v2_2/json/json_test.go @@ -92,6 +92,30 @@ func Test_Write(t *testing.T) { } } +func Test_MultilineWrite(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + + if err := json.WriteMultiline(&want, w); err != nil { + t.Errorf("Write() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote + var got spdx.Document + err := json.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) { + t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}))) + return + } +} + func Test_ShorthandFields(t *testing.T) { contents := `{ "spdxVersion": "SPDX-2.2", diff --git a/spdx/v2/v2_3/json/json_test.go b/spdx/v2/v2_3/json/json_test.go index 6bed4ea..8d3b915 100644 --- a/spdx/v2/v2_3/json/json_test.go +++ b/spdx/v2/v2_3/json/json_test.go @@ -81,6 +81,29 @@ func Test_Write(t *testing.T) { return } } +func Test_MultilineWrite(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + + if err := json.WriteMultiline(&want, w); err != nil { + t.Errorf("Write() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote + var got spdx.Document + err := json.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) { + t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}))) + return + } +} func Test_ShorthandFields(t *testing.T) { contents := `{