gpt4 book ai didi

json - JSON 到 XML 的 XSLT 3.0 转换 — 数字数据类型

转载 作者:行者123 更新时间:2023-12-01 17:01:17 24 4
gpt4 key购买 nike

摘要

  • XSLT 3.0 + XPath/XQuery 3.1 添加了对 JSON 的一些支持
  • 遗憾的是,JSON 数字类型作为 IEEE double 进行处理,导致数据会损失数字精度
  • 我正在考虑编写一组基于 Java BigDecimal 而不是 IEEE double 的自定义函数

问:为了支持超出 IEEE double 提供的数值精度,我考虑克隆 saxon 9.8 HE 中的 JSON 支持并构建一组使用 BigDecimal 而不是 IEEE double 的自定义函数是否合理?

详情

我需要对 JSON 数据执行多次转换。XSLT 3.0 + XPath 3.1 + XQuery 3.1 通过 json-to-xml + parse-json 对 JSON 提供一些支持。

https://www.w3.org/TR/xpath-functions-31/#json-functions https://www.saxonica.com/papers/xmlprague-2016mhk.pdf

我遇到了与数字数据类型处理相关的重大障碍。我的 JSON 数据包含超出 IEEE 双浮点精度的数值。在 Java 中,我的数值需要使用 BigDecimal 进行处理。

https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping

Information may however be lost if (a) JSON numbers are not exactly representable as double-precision floating point ...

此外,我还查看了 ./ma/json/JsonParser.java 的 saxonica 9.8 HE 引用实现源代码,并确认私有(private)方法 parseNumericLiteral() 返回一个原始 double 值。

我正在考虑克隆 saxon 9.8 HE JSON 支持代码,并将其用作一组自定义函数的基础,这些函数使用 Java BigDecimal 而不是 double,以便通过转换保留数字精度...

问:为了支持超出 IEEE double 提供的数值精度,我考虑克隆 saxon 9.8 HE 中的 JSON 支持并构建一组使用 BigDecimal 而不是 IEEE double 的自定义函数是否合理?

问:您是否知道我可能会遇到任何不可预见的问题?

XML 数据模型将十进制数定义为具有任意有限精度。 https://www.w3.org/TR/xmlschema-2/#decimal

JSON 数据模型将数字定义为具有任意有限精度。 https://www.rfc-editor.org/rfc/rfc7159#page-6

毫不奇怪,两者都警告具有扩展精度的数值存在潜在的互操作性问题。

问:将 XPath/XQuery 中的 JSON 数字类型显式定义为 IEEE double 的理由是什么?

结束

最佳答案

RFC 是这么说的:

This specification allows implementations to set limits on the range
and precision of numbers accepted. Since software that implements
IEEE 754-2008 binary64 (double precision) numbers [IEEE754] is
generally available and widely used, good interoperability can be
achieved by implementations that expect no more precision or range
than these provide, in the sense that implementations will
approximate JSON numbers within the expected precision. A JSON
number such as 1E400 or 3.141592653589793238462643383279 may indicate potential interoperability problems, since it suggests that the
software that created it expects receiving software to have greater
capabilities for numeric magnitude and precision than is widely
available.

在我看来,这是一个非常明确的警告:它表示虽然 JSON 语法允许数值的任意精度,但您不能依赖 JSON 使用者来保留该精度,因此如果您想如果要传达高精度数值,最好将其作为字符串传达。

需要仔细阅读fn:json-to-xmlfn:xml-to-json的规则:

The fn:json-to-xml function creates an element whose string value is lexically the same as the JSON representation of the number. The fn:xml-to-json function generates a JSON representation that is the result of casting the (typed or untyped) value of the node to xs:double and then casting the result to xs:string. Leading and trailing whitespace is accepted. Since JSON does not impose limits on the range or precision of numbers, these rules mean that conversion from JSON to XML will always succeed, and will retain full precision in the lexical representation unless the data model implementation is one that reconstructs the string value from the typed value. In the reverse direction, conversion from XML to JSON may fail if the value is infinity or NaN, or if the string value is such that casting to xs:double produces positive or negative infinity.

虽然这些话可能是我写的,但我不确定我是否记得做出这样决定的确切理由,但这确实表明这件事经过了仔细的考虑。我怀疑这个想法是,当您使用 JSON 时,您应该尝试保留输入中存在的所有信息,但是当您生成 JSON 时,您应该尝试生成所有消费者都可以接受的内容。 (著名的格言是:在你所接受的事物上保持自由,在你所创造的事物上保持保守。)

您对撒克逊来源的分析不太正确。你说:

the private method parseNumericLiteral() returns a primitive double.

这确实是事实;但原始的词法表示形式被保留,并且当解析器将值传递给 JsonReceiver 时,它会传递 Java double 和字符串表示形式,因此 JsonReceiver 可以访问两者(这是正确实现 fn:json-to-xml 所必需的)。

关于json - JSON 到 XML 的 XSLT 3.0 转换 — 数字数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49521311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com