@@ -75,7 +75,7 @@ type Package struct {
75
75
// 7.14: All Licenses Info from Files: SPDX License Expression, "NONE" or "NOASSERTION"
76
76
// Cardinality: mandatory, one or many if filesAnalyzed is true / omitted;
77
77
// zero (must be omitted) if filesAnalyzed is false
78
- PackageLicenseInfoFromFiles []string `json:"licenseInfoFromFiles"`
78
+ PackageLicenseInfoFromFiles []string `json:"licenseInfoFromFiles,omitempty "`
79
79
80
80
// 7.15: Declared License: SPDX License Expression, "NONE" or "NOASSERTION"
81
81
// Cardinality: mandatory, one
@@ -123,6 +123,32 @@ type Package struct {
123
123
hasFiles []common.DocElementID
124
124
}
125
125
126
+ func (p Package ) MarshalJSON () ([]byte , error ) {
127
+ type pkg Package
128
+ p2 := pkg (p )
129
+
130
+ data , err := marshal .JSON (p2 )
131
+ if err != nil {
132
+ return nil , err
133
+ }
134
+
135
+ // remove empty packageVerificationCode entries -- required by SPDX 2.2 but
136
+ // omitempty has no effect since it is a non-comparable struct and not a pointer, so we
137
+ // manually check to determine if there is a valid value to output and omit the field if not
138
+ // see: https://spdx.github.io/spdx-spec/v2.2.2/package-information/#79-package-verification-code-field
139
+ if p .PackageVerificationCode .Value == "" && p .PackageVerificationCode .ExcludedFiles == nil {
140
+ var values map [string ]interface {}
141
+ err = json .Unmarshal (data , & values )
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+ delete (values , "packageVerificationCode" )
146
+ return marshal .JSON (values )
147
+ }
148
+
149
+ return data , nil
150
+ }
151
+
126
152
func (p * Package ) UnmarshalJSON (b []byte ) error {
127
153
type pkg Package
128
154
type extras struct {
@@ -154,6 +180,7 @@ func (p *Package) UnmarshalJSON(b []byte) error {
154
180
}
155
181
156
182
var _ json.Unmarshaler = (* Package )(nil )
183
+ var _ json.Marshaler = (* Package )(nil )
157
184
158
185
// PackageExternalReference is an External Reference to additional info
159
186
// about a Package, as defined in section 7.21 in version 2.2 of the spec.
0 commit comments