Force JacksonXML to deserialize raw XML as String

Viewed 503

I have XML request string coming in as a text content, but Jackson identifies it as a XML tags. I tried using JacksonXMLText but that doesn't help.

<Reference>
  <Request><?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34; ?><Quote 
  sess=&#34;JAKE_OSCAR_EU&#34; language=&#34;ENG&#34; Country=&#34;US&#34;> 
  </Quote> 
  </Request>
</Reference>

It throws me exception

com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.
1 Answers

I think what you need is to write a customer deserializer reading that part of the tree you want to read Raw. Here is one example implementation:

@JsonSerialize(using = RawObjectSerializer.class)
@JsonDeserialize(using = RawObjectDeserializer.class)
public class RawObject {

    public final String value;

    public RawObject(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

public class RawObjectDeserializer extends StdDeserializer {

public RawObjectDeserializer() {
    super(RawObject.class);
}

    @Override
    public RawObject deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        return new RawObject(parser.getCodec().readTree(parser).toString());
    }
}

https://dolzhenko.me/blog/2017-08-13-raw-jackson

Related