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

Open
benken-parasoft opened this issue Apr 12, 2024 · 0 comments

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);
    }
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