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.

Can't deserialise an empty tag into an instance with no fields

See original GitHub issue

As noted in #89:

  • jackson deserializes <x></x> and <x/> for leaf-nodes as NULL…
  • JAXB deserializes <x></x> or <x/> for leaf-nodes not as NULL-values but provides default-values (an integer will be 0 by default).

This is a big problem for me. I need to deserialize a third-party schema which represents some state using the presence of a tag, in some cases with no children or attributes

   <myObject>
       <someStructuredData>...</someStructuredData>
       <flag1/>    
       <flag3/>
   </myObject>

This maps naturally to fields that can be null or populated with objects that are dumb, but typed:

   class myObject{
       SomeStructuredData someStructuredData;
       Flag1 flag1;
       Flag2 flag2;
       Flag3 flag3;
   }

After deserializing the example above I want flags 1 and 3 to be populated, and flag 2 to be null.

But I can find no way to get Jackson-xml to deserialise empty tags to anything but null. Is it truly not supported, or am I just missing something? Discerning meaningfully between missing and empty tags is a perfectly valid use of xml, and should be supported in some way or other.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
cowtowncodercommented, May 4, 2018

Jackson 2.9 added one new feature for @JsonSetter, for null handling:

https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff

(see “Null replacement/error/skipping (on deserialization)”)

which is RFE https://github.com/FasterXML/jackson-databind/issues/1402

wherein annotating property as:

@JsonSetter(nulls=Nulls.AS_EMPTY)

should result in null being coerced into “empty” value of the type. For POJOs this should be newly constructed instance (and if so, requires no-arg constructor, NOT any other kind of creator).

Problem otherwise is that due to separate of streaming parser, databinding, lower level must decide in how to expose value, and as things are there is not enough information to be able to expose it either as null OR as START_OBJECT/END_OBJECT pair (since parser has no knowledge of what expected target at high level might be).

I will add tests in XML module to ensure that this approach works (or, if not, that it needs to be fixed).

0reactions
cowtowncodercommented, May 4, 2018

@markaren yay!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Xml Deserialization Fails on Empty Element - Stack Overflow
This looks like a symptom of not serializing the XML correctly in the first case, which is most likely done with .NET 1.0...
Read more >
Using Optional with Jackson - Baeldung
In this case, isPresent() is a public getter on the Optional class. This means it will be serialized with a value of true...
Read more >
XmlSerializer Class (System.Xml.Serialization) | Microsoft Learn
Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.
Read more >
Definitive Guide to Jackson ObjectMapper - Serialize and ...
Whenever there's a mismatch between the names of properties/fields in a JSON String and a POJO - you can deal with the mismatch...
Read more >
Deserialize old XML after field removal - Google Groups
but I can't believe it. ... that was serialized as part of a now ignored element/subtree, that ... So... is there really no...
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