question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PrettyPrint XML in custom serializer doesn't seem to work?

See original GitHub issue

Hello there!

I am using jackson on a project and greatly enjoy it, so first off thanks.

However, I’m running in to an odd issue with Pretty Printing XML:

<?xml version="1.0" encoding="UTF-8"?><bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
  <components>
    <component type="framework" bom-ref="pkg:maven/org.example.acme/web-framework@1.0.0">
      <group>org.example.acme</group>
      <name>web-framework</name>
      <version>1.0.0</version>
      <purl>pkg:maven/org.example.acme/web-framework@1.0.0</purl>
    </component>
    <component type="library" bom-ref="pkg:maven/org.example.acme/persistence@3.1.0">
      <group>org.example.acme</group>
      <name>persistence</name>
      <version>3.1.0</version>
      <purl>pkg:maven/org.example.acme/persistence@3.1.0</purl>
    </component>
    <component type="library" bom-ref="pkg:maven/org.example.acme/common-util@3.0.0">
      <group>com.example.acme</group>
      <name>common-util</name>
      <version>3.0.0</version>
      <purl>pkg:maven/org.example.acme/common-util@3.0.0</purl>
    </component>
  </components><dg:dependencies xmlns:dg="http://cyclonedx.org/schema/ext/dependency-graph/1.0"><dg:dependency ref="pkg:maven/org.example.acme/web-framework@1.0.0"><dg:dependency ref="pkg:maven/org.example.acme/common-util@3.0.0"/><dg:dependency ref="pkg:maven/org.example.acme/common-persistence@3.0.0"/></dg:dependency><dg:dependency ref="pkg:maven/org.example.acme/persistence@3.1.0"><dg:dependency ref="pkg:maven/org.example.acme/common-util@3.0.0"/></dg:dependency><dg:dependency ref="pkg:maven/org.example.acme/common-util@3.0.0"/></dg:dependencies>
</bom>

The dg:dependencies section is all done using a custom serializer, for a number of reasons.

Inside of there, I generally construct things like so:

  @Override
  public void serialize(
      final List<Dependency> dependencies, final JsonGenerator generator, final SerializerProvider provider)
      throws IOException
  {
    if (generator instanceof ToXmlGenerator) {
      final ToXmlGenerator toXmlGenerator = (ToXmlGenerator) generator;
      final XMLStreamWriter staxWriter = toXmlGenerator.getStaxWriter();
      try {
        if (dependencies != null && !dependencies.isEmpty()) {
          if (useNamespace) {
            staxWriter.writeStartElement(NAMESPACE_PREFIX, "dependencies", NAMESPACE_URI);
          } else {
            staxWriter.writeStartElement("dependencies");
          }
          toXmlGenerator.writeStartArray();
          for (Dependency dependency : dependencies) {
            writeDependency(dependency, staxWriter);
          }
          toXmlGenerator.writeEndArray();
          staxWriter.writeEndElement();
        }
      }
      catch (XMLStreamException ex) {
       throw new IOException(ex);
      }

I’ve been using the staxWriter to output, because I need control over the namespace, and had some issues that I honestly can’t remember at this point, but staxWriter was the only way to get around them.

No matter what I do, I can’t seem to get the <dg:dependencies section to pretty print, but the rest of the document does. Am I holding this thing wrong 😃

The project and PR I am working on at the moment are here, if seeing more code is useful: https://github.com/CycloneDX/cyclonedx-core-java/pull/84

Cheers!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Mar 27, 2021

That’s the spirit! Looking forward to contributions!

Also very happy that you were able to make things work. Working with XML is not always easy (with or without Jackson) so getting to work right is a good feeling.

1reaction
cowtowncodercommented, Mar 26, 2021

Correct: the issue is that only calls via ToXmlGenerator will be pretty-printed: there is no standard Stax (or Stax2) way to indent XML content (this is a generally difficult problem for arbitrary XML content; without schema it is not possible to know definitely where whitespace is meaningful and where not). Theoretically you can simply use Stax XmlStreamWriter calls directly, but that may be problematic wrt synchronizing actual indentation depth and so on.

As to namespaces; XML format backend forces use of repairing writer so you should be able to force certain bindings for namespaces. So perhaps you can just call writeNamespace() to establish Namespace URI/prefix binding, if that is needed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pretty printing XML with Jackson library - Stack Overflow
I am trying to use Jackson library to serialize Java objects into XML by using JAXB annotations. However, I face an issue in...
Read more >
JSON and XML Serialization in ASP.NET Web API
Describes the JSON and XML formatters in ASP.NET Web API for ASP.NET 4.x.
Read more >
Definitive Guide to Jackson ObjectMapper - Serialize and ...
In this detailed guide - learn everything you need to know about ObjectMapper. Convert JSON to and from Java POJOs, implement custom ......
Read more >
Using an alternate JSON Serializer in ASP.NET Web API
Neither does DataContractSerializer for XML output for that matter. ... FWIW, the same is true for XmlSerializer which also doesn't work ...
Read more >
Choosing your Serializer — if you can - Apache Flink
It also allows optimizations in the serialization format as well as reducing ... Alternatively, you can also register custom serializers for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found