aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUjjwal Agarwal <ujjwalcoding012@gmail.com>2021-08-06 18:15:01 +0530
committerUjjwal Agarwal <ujjwalcoding012@gmail.com>2021-08-06 18:15:01 +0530
commit1bc87f7cbc680e409a1ec51fdfdd93b5d2b49701 (patch)
tree250966ff349b1e1dab25505774ccddc87a0d6d45 /examples
parent1a9690feffaf2257985570998037cefd196e21aa (diff)
downloadspdx-tools-1bc87f7cbc680e409a1ec51fdfdd93b5d2b49701.tar.gz
Examples : add example of jsonloader and remove existing conversion examples
- bug fixes in json saver done as well - test covereage of jsonparser increased Signed-off-by: Ujjwal Agarwal <ujjwalcoding012@gmail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/10-jsonloader/example_json_loader.go55
-rw-r--r--examples/8-jsontotv/examplejsontotv.go (renamed from examples/8-jsonloader/examplejsontotv.go)0
-rw-r--r--examples/9-tvtojson/exampletvtojson.go (renamed from examples/9-jsonsaver/exampletvtojson.go)0
-rw-r--r--examples/README.md10
4 files changed, 63 insertions, 2 deletions
diff --git a/examples/10-jsonloader/example_json_loader.go b/examples/10-jsonloader/example_json_loader.go
new file mode 100644
index 0000000..96f47fd
--- /dev/null
+++ b/examples/10-jsonloader/example_json_loader.go
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+// Example for: *jsonparser2v2*
+
+// This example demonstrates loading an SPDX json from disk into memory,
+// and then logging out some attributes to the console .
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/spdx/tools-golang/jsonloader"
+)
+
+func main() {
+
+ // check that we've received the right number of arguments
+ args := os.Args
+ if len(args) != 3 {
+ fmt.Printf("Usage: %v <spdx-file-in> <spdx-file-out>\n", args[0])
+ fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n")
+ fmt.Printf(" save it out to <spdx-file-out>.\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 json file, version 2.2
+ doc, err := jsonloader.Load2_2(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])
+
+ fmt.Println(strings.Repeat("=", 80))
+ fmt.Println("Some Attributes of the Document:")
+ fmt.Printf("Document Name: %s\n", doc.CreationInfo.DocumentName)
+ fmt.Printf("DataLicense: %s\n", doc.CreationInfo.DataLicense)
+ fmt.Printf("Document NameSpace: %s\n", doc.CreationInfo.DocumentNamespace)
+ fmt.Printf("SPDX Document Version: %s\n", doc.CreationInfo.SPDXVersion)
+ fmt.Println(strings.Repeat("=", 80))
+}
diff --git a/examples/8-jsonloader/examplejsontotv.go b/examples/8-jsontotv/examplejsontotv.go
index ede044e..ede044e 100644
--- a/examples/8-jsonloader/examplejsontotv.go
+++ b/examples/8-jsontotv/examplejsontotv.go
diff --git a/examples/9-jsonsaver/exampletvtojson.go b/examples/9-tvtojson/exampletvtojson.go
index 2c6ec7e..2c6ec7e 100644
--- a/examples/9-jsonsaver/exampletvtojson.go
+++ b/examples/9-tvtojson/exampletvtojson.go
diff --git a/examples/README.md b/examples/README.md
index 5009adc..bd4b3d3 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -64,17 +64,23 @@ the same identifier in both documents.
This example demonstrates loading an SPDX rdf file from disk into memory
and then printing the corresponding spdx struct for the document.
-## 8-jsonloader
+## 8-jsontotv
*jsonloader*, *tvsaver*
This example demonstrates loading an SPDX json from disk into memory
and then re-saving it to a different file on disk in tag-value format.
-## 9-jsonsaver
+## 9-tvtojson
*jsonsaver*, *tvloader*
This example demonstrates loading an SPDX tag-value from disk into memory
and then re-saving it to a different file on disk in json format.
+## 10-jsonloader
+
+*jsonloader*
+
+This example demonstrates loading an SPDX json from disk into memory
+and then logging some of the attributes to the console.