Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON serialized Property Value containing multiple spaces get "squashed" #583

Open
andrew-m-leonard opened this issue Dec 23, 2024 · 0 comments

Comments

@andrew-m-leonard
Copy link
Contributor

andrew-m-leonard commented Dec 23, 2024

If a Property Value containing multiple consecutive spaces, eg.

 "A property value         containing multiple spaces!"

is serialized with JSON, the spaces get squashed, should they be preserved??

XML Serialization is correctly preserving the spaces.

Test Case:

import org.cyclonedx.exception.GeneratorException;
import org.cyclonedx.generators.json.BomJsonGenerator;
import org.cyclonedx.generators.xml.BomXmlGenerator;
import org.cyclonedx.model.Bom;
import org.cyclonedx.model.Component;
import org.cyclonedx.model.Property;
import org.cyclonedx.model.Metadata;
import org.cyclonedx.parsers.JsonParser;
import org.cyclonedx.parsers.XmlParser;
import org.cyclonedx.Version;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.List;
import java.util.LinkedList;
import java.util.UUID;

public final class Issue583 {

    public static void main(final String[] args) {
        try {
            Bom bom = new Bom();
            bom.setSerialNumber("urn:uuid:" + UUID.randomUUID());

            // Component test with Property containing multiple spaces
            Component comp1 = new Component();
            comp1.setType(Component.Type.APPLICATION);
            comp1.setName("COMP 1");
            comp1.setVersion("v1");

            Property prop1 = new Property();
            prop1.setName("PROP1");
            prop1.setValue("A property value       containing multiple spaces!");
            comp1.addProperty(prop1);

            bom.addComponent(comp1);

            // Serialize...
            writeJSONfile(bom, "Issue583_SBOM.json");
            writeXMLfile(bom, "Issue583_SBOM.xml");

            // Deserialize...
            Bom bomJson = readJSONfile("Issue583_SBOM.json");
            Bom bomXml  = readXMLfile("Issue583_SBOM.xml");

            // Check json and xml Property value is the same?
            String jsonValue = bomJson.getComponents().get(0).getProperties().get(0).getValue();
            String xmlValue  = bomXml.getComponents().get(0).getProperties().get(0).getValue();

            System.out.println("JSON Property value = "+jsonValue);
            System.out.println("XML Property value  = "+xmlValue);

            if (!jsonValue.equals(xmlValue)) {
              System.out.println("ERROR: JSON != XML Property value");
              System.exit(1);
            } else {
              System.out.println("SUCCESS: JSON == XML Property value");
            }
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    static String generateBomJson(final Bom bom) throws GeneratorException {
        BomJsonGenerator bomGen = new BomJsonGenerator(bom, Version.VERSION_16);
        String json = bomGen.toJsonString();
        return json;
    }

    static String generateBomXml(final Bom bom) throws GeneratorException {
        BomXmlGenerator bomGen = new BomXmlGenerator(bom, Version.VERSION_16);
        String xml = bomGen.toXmlString();
        return xml;
    }

    // Writes the BOM object to the specified file.
    static void writeJSONfile(final Bom bom, final String fileName) {
        FileWriter file;
        try {
            String json = generateBomJson(bom);

            file = new FileWriter(fileName);
            file.write(json);
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    // Writes the BOM object to the specified XML file.
    static void writeXMLfile(final Bom bom, final String fileName) {
        FileWriter file;
        try {
            String xml = generateBomXml(bom);

            file = new FileWriter(fileName);
            file.write(xml);
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    // Returns a parsed BOM object from the specified file.
    static Bom readJSONfile(final String fileName) {
        Bom bom = null;
        try {
            FileReader reader = new FileReader(fileName);
            JsonParser parser = new JsonParser();
            bom = parser.parse(reader);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
           return bom;
        }
    }

    // Returns a parsed BOM object from the specified file.
    static Bom readXMLfile(final String fileName) {
        Bom bom = null;
        try {
            FileReader reader = new FileReader(fileName);
            XmlParser parser = new XmlParser();
            bom = parser.parse(reader);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
           return bom;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant