aboutsummaryrefslogtreecommitdiff
path: root/json/parser.go
diff options
context:
space:
mode:
Diffstat (limited to 'json/parser.go')
-rw-r--r--json/parser.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/json/parser.go b/json/parser.go
new file mode 100644
index 0000000..ee7915d
--- /dev/null
+++ b/json/parser.go
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+package spdx_json
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+
+ "github.com/spdx/tools-golang/spdx/v2_2"
+ "github.com/spdx/tools-golang/spdx/v2_3"
+)
+
+// Load2_2 takes in an io.Reader and returns an SPDX document.
+func Load2_2(content io.Reader) (*v2_2.Document, error) {
+ // convert io.Reader to a slice of bytes and call the parser
+ buf := new(bytes.Buffer)
+ _, err := buf.ReadFrom(content)
+ if err != nil {
+ return nil, err
+ }
+
+ var doc v2_2.Document
+ err = json.Unmarshal(buf.Bytes(), &doc)
+ if err != nil {
+ return nil, err
+ }
+
+ return &doc, nil
+}
+
+// Load2_3 takes in an io.Reader and returns an SPDX document.
+func Load2_3(content io.Reader) (*v2_3.Document, error) {
+ // convert io.Reader to a slice of bytes and call the parser
+ buf := new(bytes.Buffer)
+ _, err := buf.ReadFrom(content)
+ if err != nil {
+ return nil, err
+ }
+
+ var doc v2_3.Document
+ err = json.Unmarshal(buf.Bytes(), &doc)
+ if err != nil {
+ return nil, err
+ }
+
+ return &doc, nil
+}