Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from spdx/json
Merge JSON saving into main
- Loading branch information
Showing
36 changed files
with
2,553 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
SPDX-License-Identifier: CC-BY-4.0 | ||
|
||
## Working | ||
|
||
A UnmarshallJSON function on the spdx.Document2_2 struct is defined so that when the JSON is unmarshalled in it the function is called and we can implement the process in a custom way . Then a new map[string]interface{} is deifined which temporarily holds the unmarshalled json . The map is then parsed into the spdx.Document2_2 using functions defined for it’s different sections . | ||
|
||
JSON → map[string]interface{} → spdx.Document2_2 | ||
|
||
## Some Key Points | ||
|
||
- The packages have a property "hasFiles" defined in the schema which is an array of the SPDX Identifiers of the files of that pacakge . The parses first parses all the files into the Unpackaged files map of the document and then when it parses the packages , it removes the respective files from the unpackaged files map and places it inside the files map of that package . | ||
|
||
- The snippets have a property "snippetFromFile" which has the SPDX identiifer of the file to which the snippet is related . Thus the snippets require the files to be parsed before them . Then the snippets are parsed one by one and inserted into the respective files using this property . | ||
|
||
|
||
The json file loader in `package jsonloader` makes the following assumptions: | ||
|
||
|
||
### Order of appearance of the properties | ||
* The parser does not make any pre-assumptions based on the order in which the properties appear . | ||
|
||
|
||
### Annotations | ||
* The json spdx schema does not define the SPDX Identifier property for the annotation object . The parser assumes the spdx Identifier of the parent property of the currently being parsed annotation array to be the SPDX Identifer for all the annotation objects of that array. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
SPDX-License-Identifier: CC-BY-4.0 | ||
|
||
## Working | ||
|
||
The spdx document is converted to map[string]interface{} and then the entire map is converted to json using a single json Marshall function call . The saver uses a tempoarary storage to store all the files (Paackaged and Unpackaged) together in a single data structure in order to comply with the json schema defined by spdx . | ||
|
||
spdx.Document2_2 → map[string]interface{} → JSON | ||
|
||
## Some Key Points | ||
|
||
- The packages have a property "hasFiles" defined in the schema which is an array of the SPDX Identifiers of the files of that pacakge . The saver iterates through the files of a package and inserted all the SPDX Identifiers of the files in the "hasFiles" array . In addition it adds the file to a temporary storage map to store all the files of the entire document at a single place . | ||
|
||
- The files require the packages to be saved before them in order to ensure that the packaged files are added to the temporary storage before the files are saved . | ||
|
||
- The snippets are saved after the files and a property "snippetFromFile" identifies the file of the snippets. | ||
|
||
The json file loader in `package jsonsaver` makes the following assumptions: | ||
|
||
|
||
### Order of appearance of the properties | ||
* The saver does not make any pre-assumptions based on the order in which the properties are saved . | ||
|
||
|
||
### Annotations | ||
* The json spdx schema does not define the SPDX Identifier property for the annotation object . The saver inserts the annotation inside the element who spdx identifier mathches the annotation SPDX identifier . | ||
|
||
### Indentation | ||
* The jsonsaver uses the marshall indent function with "" as he prefix and "\t" as the indent character , passed as funtion parameters . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | ||
|
||
// Example for: *tvloader*, *jsonsaver* | ||
|
||
// This example demonstrates loading an SPDX tag-value file from disk into memory, | ||
// and re-saving it to a different json file on disk. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spdx/tools-golang/jsonsaver" | ||
"github.com/spdx/tools-golang/tvloader" | ||
) | ||
|
||
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 tag-value file, version 2.2 | ||
doc, err := tvloader.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]) | ||
|
||
// 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 an SPDX json file, version 2.2 | ||
err = jsonsaver.Save2_2(doc, w) | ||
if err != nil { | ||
fmt.Printf("Error while saving %v: %v", fileOut, err) | ||
return | ||
} | ||
|
||
// it worked | ||
fmt.Printf("Successfully saved %s\n", fileOut) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.