spdx_tools.spdx.writer.json.json_writer

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4import json
 5
 6from beartype.typing import IO
 7
 8from spdx_tools.spdx.jsonschema.document_converter import DocumentConverter
 9from spdx_tools.spdx.model import Document
10from spdx_tools.spdx.writer.write_utils import convert, validate_and_deduplicate
11
12
13def write_document_to_stream(
14    document: Document,
15    stream: IO[str],
16    validate: bool = True,
17    converter: DocumentConverter = None,
18    drop_duplicates: bool = True,
19):
20    """
21    Serializes the provided document to json and writes it to a file with the provided name. Unless validate is set
22    to False, validates the document before serialization. Unless a DocumentConverter instance is provided,
23    a new one is created.
24    """
25    document = validate_and_deduplicate(document, validate, drop_duplicates)
26    document_dict = convert(document, converter)
27    json.dump(document_dict, stream, indent=4)
28
29
30def write_document_to_file(
31    document: Document,
32    file_name: str,
33    validate: bool = True,
34    converter: DocumentConverter = None,
35    drop_duplicates: bool = True,
36):
37    with open(file_name, "w", encoding="utf-8") as out:
38        write_document_to_stream(document, out, validate, converter, drop_duplicates)
def write_document_to_stream( document: spdx_tools.spdx.model.document.Document, stream: IO[str], validate: bool = True, converter: spdx_tools.spdx.jsonschema.document_converter.DocumentConverter = None, drop_duplicates: bool = True):
14def write_document_to_stream(
15    document: Document,
16    stream: IO[str],
17    validate: bool = True,
18    converter: DocumentConverter = None,
19    drop_duplicates: bool = True,
20):
21    """
22    Serializes the provided document to json and writes it to a file with the provided name. Unless validate is set
23    to False, validates the document before serialization. Unless a DocumentConverter instance is provided,
24    a new one is created.
25    """
26    document = validate_and_deduplicate(document, validate, drop_duplicates)
27    document_dict = convert(document, converter)
28    json.dump(document_dict, stream, indent=4)

Serializes the provided document to json and writes it to a file with the provided name. Unless validate is set to False, validates the document before serialization. Unless a DocumentConverter instance is provided, a new one is created.

def write_document_to_file( document: spdx_tools.spdx.model.document.Document, file_name: str, validate: bool = True, converter: spdx_tools.spdx.jsonschema.document_converter.DocumentConverter = None, drop_duplicates: bool = True):
31def write_document_to_file(
32    document: Document,
33    file_name: str,
34    validate: bool = True,
35    converter: DocumentConverter = None,
36    drop_duplicates: bool = True,
37):
38    with open(file_name, "w", encoding="utf-8") as out:
39        write_document_to_stream(document, out, validate, converter, drop_duplicates)