title: XSLT 3.0 transformation of JSON to XML -- numeric data types
body: <p><strong>SUMMARY</strong></p>
  <ul>
  <li>some support for JSON was added to XSLT 3.0 + XPath/XQuery 3.1</li>
  <li>unfortunately, JSON number types are handled as IEEE double, subjecting the data to loss of numeric precision</li>
  <li>I am considering writing a set of custom functions based on Java BigDecimal instead of IEEE double</li>
  </ul>
  <p>Q: In order to support numeric precision beyond that offered by IEEE double, is it reasonable for me to consider cloning the JSON support in saxon 9.8 HE and building a set of customized functions which use BigDecimal instead of IEEE double?</p>
  <p><strong>DETAIL</strong></p>
  <p>I need to perform a number of transformations of JSON data.
  XSLT 3.0 + XPath 3.1 + XQuery 3.1 have some support for JSON through json-to-xml + parse-json.</p>
  <p><a href="https://www.w3.org/TR/xpath-functions-31/#json-functions" rel="nofollow noreferrer">https://www.w3.org/TR/xpath-functions-31/#json-functions</a>
  <a href="https://www.saxonica.com/papers/xmlprague-2016mhk.pdf" rel="nofollow noreferrer">https://www.saxonica.com/papers/xmlprague-2016mhk.pdf</a></p>
  <p>I have hit a significant snag related to treatment of numeric data types.
  My JSON data includes numeric values that exceed the precision of IEEE double-floats. In Java, my numeric values need to be processed using BigDecimal.</p>
  <p><a href="https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping" rel="nofollow noreferrer">https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping</a>
  states</p>
  <blockquote>
  <p>Information may however be lost if (a) JSON numbers are not exactly representable as double-precision floating point ...</p>
  </blockquote>
  <p>In addition, I have taken a look at the saxonica 9.8 HE reference implementation source for ./ma/json/JsonParser.java and confirm that the private method parseNumericLiteral() returns a primitive double.</p>
  <p>I am considering cloning the saxon 9.8 HE JSON support code and using this as the basis for a set of customized functions which uses Java BigDecimal instead of double in order to retain numeric precision through the transformations ...</p>
  <p>Q: In order to support numeric precision beyond that offered by IEEE double, is it reasonable for me to consider cloning the JSON support in saxon 9.8 HE and building a set of customized functions which use BigDecimal instead of IEEE double?</p>
  <p>Q: Are you aware of any unforeseen issues which I may encounter?</p>
  <p>The XML data model defines decimal numbers as having any finite precision.
  <a href="https://www.w3.org/TR/xmlschema-2/#decimal" rel="nofollow noreferrer">https://www.w3.org/TR/xmlschema-2/#decimal</a></p>
  <p>The JSON data model defines numbers as having any finite precision.
  <a href="https://www.rfc-editor.org/rfc/rfc7159#page-6" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc7159#page-6</a></p>
  <p>Not surprisingly, both warn of potential interoperability issues with numeric values with extended precision.</p>
  <p>Q: What was the rationale for explicitly defining the JSON number type in XPath/XQuery as IEEE double?</p>
  <p>THE END</p>