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.

Deserializing @JacksonXmlText

See original GitHub issue

Whilst there is no problem serializing a field annotated with @JacksonXmlText, deserialization always chokes up. For example (in Scala, but should be clear enough):

case class Phone (
  @JacksonXmlProperty(isAttribute = true, localName = "type") phoneType: String,
  @JacksonXmlText phoneNumber: String
)

Serializes Phone("cell","000-000-0000") into <phone type="cell">000-000-0000</phone>, but deserializing the same thing will fail with

JsonMappingException: Could not find creator property with name ''

Is there a reason this can’t/shouldn’t be supported, at least in restricted situations like the above? It seems like this falls under the “XML written using this module must be readable using module as well”.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
iteratorukcommented, Aug 3, 2018

My current workaround is to use a custom deserializer:

@JsonDeserialize(using = classOf[AttributeAndTextTypeDeserializer])
case class MyElement(@JacksonXmlProperty(localName = "anAttribute", isAttribute = true)
                     anAttribute: Option[String] = None,

                     @JacksonXmlText
                     theValue: Option[String] = None)

class AttributeAndTextTypeDeserializer extends StdDeserializer[MyElement](classOf[MyElement]) {

  override def deserialize(p: JsonParser, ctx: DeserializationContext): MyElement = {
    val n: JsonNode = p.getCodec.readTree(p)
    MyElement(nonEmptyOrNone(n.get("anAttribute").asText("")), nonEmptyOrNone(n.get("").asText("")))
  }

  private def nonEmptyOrNone(str: String): Option[String] = if (str.trim.isEmpty) None else Some(str)

}
0reactions
Simon3commented, Nov 9, 2021

Actually, forget about it. My problem is related to Kotlin data classes. Works fine with normal classes. Seems to be a known issue in jackson-module-kotlin: https://github.com/FasterXML/jackson-module-kotlin/issues/138

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson xml module: deserializing immutable type with a ...
Jackson xml module: deserializing immutable type with a @JacksonXmlText property ... I want to serialize an immutable type both as json and as...
Read more >
JacksonXmlText (Jackson-dataformat-XML 2.1.1 API)
It will result in value of the property be serialized without element wrapper, as long as there are no element-wrapped other properties (attribute-valued ......
Read more >
Solving the XML Problem with Jackson - Stackify
Next, let's have a look at the @JacksonXmlText annotation. ... The opposite of this is also possible – deserializing a Java bean from...
Read more >
[jackson-user] XML Deserialization of @JacksonXmlText in Scala ...
Hi -- I am wondering if anyone has experienced the following issue with XML deserialization to Scala case classes annotated with @JacksonXmlText:
Read more >
XML Serialization and Deserialization with Jackson - Baeldung
This short tutorial shows how the Jackson library can be used to serialize Java object to XML and deserialize them back to objects....
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