You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of version 2.16.1, infinite values of float and double are serialized in a way that is incompatible with the XML Schema definition and JAXB. Specifically, jackson-dataformat-xml serializes these values as the strings Infinity or -Infinity. XML Schema, however, says they should be serialized as INF or -INF, and that is what JAXB does.
--- Jackson serialization ---
<ExampleObject><x>Infinity</x><y>-Infinity</y><z>NaN</z><fx>Infinity</fx><fy>-Infinity</fy><fz>NaN</fz></ExampleObject>
--- Jackson deserialization ---
x=Infinity y=-Infinity z=NaN fx=Infinity fy=-Infinity fz=NaN
--- JAXB serialization ---
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<example>
<x>INF</x>
<y>-INF</y>
<z>NaN</z>
<fx>INF</fx>
<fy>-INF</fy>
<fz>NaN</fz>
</example>
--- JAXB deserialization ---
x=Infinity y=-Infinity z=NaN fx=Infinity fy=-Infinity fz=NaN
--- serialized with JAXB, deserialized with Jackson ---
x=Infinity y=-Infinity z=NaN fx=Infinity fy=-Infinity fz=NaN
--- serialized with Jackson, deserialized with JAXB ---
x=0.000000 y=0.000000 z=NaN fx=0.000000 fy=0.000000 fz=NaN
As the example program's output shows, Jackson understands both its own format and the XML Schema format for floating-point infinity. JAXB, however, understands only the XML Schema format, and fails to parse Jackson's format.
The problem seems to be that jackson-dataformat-xml calls TypedXMLStreamWriter methods to serialize floating-point values, which ultimately uses NumberUtil.write{Float,Double} from StAX2, which in turn uses java.lang.String.valueOf to serialize the number, without any special handling of infinity.
Deserialization of XML Schema-formatted numbers seems to work correctly. Only serialization has an issue.
This issue only affects positive and negative infinity. java.lang.String.valueOf differs from XML Schema only in how it represents infinity; it uses the same format as XML Schema for NaN and finite values.
The text was updated successfully, but these errors were encountered:
Jackson XML module is neither JAXB implementation, nor make any use of XML Schema.
So in that sense expected behavior is not necessarily same.
Having said that, if anyone has time to come up with a PR I'd be happy to help get that merged -- the only (?) requirement would be to have new ToXMLGenerator.Feature for enabling different serialization: this is needed for backwards compatibility.
ahcodedthat
added a commit
to ahcodedthat/jackson-dataformat-xml
that referenced
this issue
Mar 8, 2024
As of version 2.16.1, infinite values of
float
anddouble
are serialized in a way that is incompatible with the XML Schema definition and JAXB. Specifically, jackson-dataformat-xml serializes these values as the stringsInfinity
or-Infinity
. XML Schema, however, says they should be serialized asINF
or-INF
, and that is what JAXB does.Example program (click to show)
Maven POM for example program (click to show)
Output from example program (click to show)
As the example program's output shows, Jackson understands both its own format and the XML Schema format for floating-point infinity. JAXB, however, understands only the XML Schema format, and fails to parse Jackson's format.
The problem seems to be that jackson-dataformat-xml calls
TypedXMLStreamWriter
methods to serialize floating-point values, which ultimately usesNumberUtil.write{Float,Double}
from StAX2, which in turn usesjava.lang.String.valueOf
to serialize the number, without any special handling of infinity.Deserialization of XML Schema-formatted numbers seems to work correctly. Only serialization has an issue.
This issue only affects positive and negative infinity.
java.lang.String.valueOf
differs from XML Schema only in how it represents infinity; it uses the same format as XML Schema for NaN and finite values.The text was updated successfully, but these errors were encountered: