diff --git a/pygrype/core/decorators/to_json.py b/pygrype/core/decorators/to_json.py new file mode 100644 index 0000000..36c7424 --- /dev/null +++ b/pygrype/core/decorators/to_json.py @@ -0,0 +1,11 @@ +import json +from dataclasses import asdict +from typing import Type, TypeVar + +T = TypeVar('T') + +def to_json(cls: Type[T]) -> Type[T]: + def to_json_func(self: T): + return json.dumps(asdict(self)) + cls.to_json = to_json_func + return cls diff --git a/pygrype/core/grype_version.py b/pygrype/core/grype_version.py index 7b73605..d08d6f4 100644 --- a/pygrype/core/grype_version.py +++ b/pygrype/core/grype_version.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class GrypeVersion: version: str syft_version: str diff --git a/pygrype/core/list/db_meta_data.py b/pygrype/core/list/db_meta_data.py index 890ebe5..86123a6 100644 --- a/pygrype/core/list/db_meta_data.py +++ b/pygrype/core/list/db_meta_data.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class DBMetaData: built: str version: int diff --git a/pygrype/core/scan/artifact.py b/pygrype/core/scan/artifact.py index 9782ab0..992d747 100644 --- a/pygrype/core/scan/artifact.py +++ b/pygrype/core/scan/artifact.py @@ -1,11 +1,13 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.location import Location from pygrype.core.scan.upstream import Upstream @dataclass +@to_json class Artifact: id: str name: str diff --git a/pygrype/core/scan/cvss.py b/pygrype/core/scan/cvss.py index 2bf2852..84b1f4d 100644 --- a/pygrype/core/scan/cvss.py +++ b/pygrype/core/scan/cvss.py @@ -1,10 +1,12 @@ from dataclasses import dataclass from typing import Optional +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.cvss_metrics import CVSSMetrics @dataclass +@to_json class CVSS: source: Optional[str] type: Optional[str] diff --git a/pygrype/core/scan/cvss_metrics.py b/pygrype/core/scan/cvss_metrics.py index cf025e0..8e1fb98 100644 --- a/pygrype/core/scan/cvss_metrics.py +++ b/pygrype/core/scan/cvss_metrics.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class CVSSMetrics: baseScore: float exploitabilityScore: float diff --git a/pygrype/core/scan/distro.py b/pygrype/core/scan/distro.py index 907c8b6..de95770 100644 --- a/pygrype/core/scan/distro.py +++ b/pygrype/core/scan/distro.py @@ -1,8 +1,10 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Distro: name: str version: str diff --git a/pygrype/core/scan/layer.py b/pygrype/core/scan/layer.py index 62ee7df..b3ee3d3 100644 --- a/pygrype/core/scan/layer.py +++ b/pygrype/core/scan/layer.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Layer: mediaType: str digest: str diff --git a/pygrype/core/scan/location.py b/pygrype/core/scan/location.py index 75245d4..ec4b93b 100644 --- a/pygrype/core/scan/location.py +++ b/pygrype/core/scan/location.py @@ -1,7 +1,10 @@ from dataclasses import dataclass from typing import Optional +from pygrype.core.decorators.to_json import to_json + @dataclass +@to_json class Location: path: str layerID: Optional[str] = None diff --git a/pygrype/core/scan/match.py b/pygrype/core/scan/match.py index e8f4581..9584f85 100644 --- a/pygrype/core/scan/match.py +++ b/pygrype/core/scan/match.py @@ -1,12 +1,14 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.artifact import Artifact from pygrype.core.scan.match_details import MatchDetails from pygrype.core.scan.vulnerability import Vulnerability @dataclass +@to_json class Match: vulnerability: Vulnerability relatedVulnerabilities: List[Vulnerability] diff --git a/pygrype/core/scan/match_details.py b/pygrype/core/scan/match_details.py index e36c2db..386ca21 100644 --- a/pygrype/core/scan/match_details.py +++ b/pygrype/core/scan/match_details.py @@ -1,10 +1,12 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.match_details_found import MatchDetailsFound from pygrype.core.scan.match_details_searched_by import MatchDetailsSearchedBy @dataclass +@to_json class MatchDetails: type: str matcher: str diff --git a/pygrype/core/scan/match_details_found.py b/pygrype/core/scan/match_details_found.py index 93b9b12..5bff53a 100644 --- a/pygrype/core/scan/match_details_found.py +++ b/pygrype/core/scan/match_details_found.py @@ -1,8 +1,10 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class MatchDetailsFound: vulnerabilityID: str versionConstraint: str diff --git a/pygrype/core/scan/match_details_searched_by.py b/pygrype/core/scan/match_details_searched_by.py index 906c780..d8162ad 100644 --- a/pygrype/core/scan/match_details_searched_by.py +++ b/pygrype/core/scan/match_details_searched_by.py @@ -1,11 +1,12 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.package import Package from pygrype.core.scan.searched_by_distro import Distro - @dataclass +@to_json class MatchDetailsSearchedBy: namespace: str cpes: Optional[List[str]] diff --git a/pygrype/core/scan/package.py b/pygrype/core/scan/package.py index c4b92f1..cc3dc7d 100644 --- a/pygrype/core/scan/package.py +++ b/pygrype/core/scan/package.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Package: name: str version: str diff --git a/pygrype/core/scan/scan.py b/pygrype/core/scan/scan.py index 2f8ba3d..37c8a74 100644 --- a/pygrype/core/scan/scan.py +++ b/pygrype/core/scan/scan.py @@ -1,12 +1,13 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.distro import Distro from pygrype.core.scan.match import Match from pygrype.core.scan.scan_source import ScanSource - @dataclass +@to_json class Scan: matches: List[Match] source: ScanSource diff --git a/pygrype/core/scan/scan_source.py b/pygrype/core/scan/scan_source.py index 3a85e48..433fefb 100644 --- a/pygrype/core/scan/scan_source.py +++ b/pygrype/core/scan/scan_source.py @@ -1,10 +1,12 @@ from dataclasses import dataclass from typing import Union +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.target import Target @dataclass +@to_json class ScanSource: type: str target: Union[Target, str] diff --git a/pygrype/core/scan/searched_by_distro.py b/pygrype/core/scan/searched_by_distro.py index 72984d0..23f78d9 100644 --- a/pygrype/core/scan/searched_by_distro.py +++ b/pygrype/core/scan/searched_by_distro.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Distro: type: str version: str diff --git a/pygrype/core/scan/target.py b/pygrype/core/scan/target.py index b53b747..0a1fbce 100644 --- a/pygrype/core/scan/target.py +++ b/pygrype/core/scan/target.py @@ -1,10 +1,11 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.layer import Layer - @dataclass +@to_json class Target: userInput: str imageID: str diff --git a/pygrype/core/scan/upstream.py b/pygrype/core/scan/upstream.py index ca88c45..6350bd2 100644 --- a/pygrype/core/scan/upstream.py +++ b/pygrype/core/scan/upstream.py @@ -1,6 +1,8 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Upstream: name: str diff --git a/pygrype/core/scan/vulnerability.py b/pygrype/core/scan/vulnerability.py index 6969f3e..b64a72c 100644 --- a/pygrype/core/scan/vulnerability.py +++ b/pygrype/core/scan/vulnerability.py @@ -1,11 +1,13 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.cvss import CVSS from pygrype.core.scan.vulnerability_fix import VulnerabilityFix @dataclass +@to_json class Vulnerability: id: str description: Optional[str] diff --git a/pygrype/core/scan/vulnerability_fix.py b/pygrype/core/scan/vulnerability_fix.py index 5e7acd8..d48b128 100644 --- a/pygrype/core/scan/vulnerability_fix.py +++ b/pygrype/core/scan/vulnerability_fix.py @@ -1,8 +1,10 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class VulnerabilityFix: versions: List[str] state: str