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

BomJsonGenerator.toJsonString() unexpectedly replaces all new lines in Strings with spaces #394

Closed
benken-parasoft opened this issue Apr 12, 2024 · 4 comments
Labels
duplicate This issue or pull request already exists

Comments

@benken-parasoft
Copy link

benken-parasoft commented Apr 12, 2024

If I serialize a org.cyclonedx.model.Bom to JSON using BomJsonGenerator.toJsonString() then I unexpectedly lose newlines in various strings, like license and copyright text where new lines are especially important.

I tracked down the problem to org.cyclonedx.util.serializer.TrimStringSerializer. This serializer is not just trimming but is also replacing carriage return, line feed, and tab characters with a space. These should be preserved in the JSON. Jackson will correctly escape such characters, like encoding newlines as '\n'.

In contrast, BomXmlGenerator is fine. The XML version has newlines correctly preserved in the XML.

I am currently working around the issue as follows:

    private static void removeTrimStringSerializer(BomJsonGenerator jsonGenerator) {
        ObjectMapper mapper = ((AbstractBomJsonGenerator) jsonGenerator).getMapper();
        SerializerFactory newSerializerFactory = BeanSerializerFactory.instance;
        SerializationConfig serializationConfig = mapper.getSerializationConfig();
        JavaType javaType = serializationConfig.constructType(String.class);
        BeanDescription beanDesc = serializationConfig.introspect(javaType);
        SerializerFactoryConfig serializerFactoryConfig = ((BasicSerializerFactory) mapper.getSerializerFactory()).getFactoryConfig();
        for (Serializers serializers : serializerFactoryConfig.serializers()) {
            JsonSerializer<?> stringSerializer = serializers.findSerializer(serializationConfig, javaType, beanDesc);
            if (!(stringSerializer instanceof TrimStringSerializer)) {
                newSerializerFactory = newSerializerFactory.withAdditionalSerializers(serializers);
            }
        }
        mapper.setSerializerFactory(newSerializerFactory);
    }
@nscuro nscuro added bug Something isn't working duplicate This issue or pull request already exists and removed bug Something isn't working labels Jun 18, 2024
@nscuro
Copy link
Member

nscuro commented Jun 18, 2024

Appears to be a duplicate of #394 (Edit: #135).

@nscuro nscuro closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2024
@benken-parasoft
Copy link
Author

benken-parasoft commented Jun 18, 2024

Appears to be a duplicate of #394

A duplicate of itself?
This is a real issue. Copyright and license text can be large with a lot of newlines. BomJsonGenerator strips them out! In contrast, BomXmlGenerator is fine and preserves everything.

@nscuro
Copy link
Member

nscuro commented Jun 18, 2024

Too much copy-pasting. Meant to link to #135.

@benken-parasoft
Copy link
Author

Too much copy-pasting. Meant to link to #135.

Thanks! I'll follow this one instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants