-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON Parsing & Serialization: Numbers #33
Comments
On the same theme, I've just had a request from a customer for an option on xml-to-json to avoid exponential notation when outputting numbers. |
For fn:xml-to-json (see https://saxonica.plan.io/issues/4860) I propose to add an option number-formatter as (function(xs:string) as xs:string)? If present, this function is used to format any
outputs the value "as is", while
outputs the value with three decimal places, and
Note, I feel an urge to allow the last example to be written as
i.e. allowing a function to be expressed as pipeline of functions to be composed. |
For symmetry, I suggest that for parse-json we add a similar function as an option: number-parser: function(xs:string) as xs:anyAtomicType. If the input matches the JSON number production, it is passed as a string to the supplied number-parser function, and the output contains whatever this returns. |
I have implemented the following (in the specs, the test suite, and Saxon): (a) A new option in parse-json and json-doc: (b) A new option in xml-to-json: Note that json-to-xml leaves the representation of the number unchanged. |
Where exactly can I find this change? What kind of functions do I need to think of for this Although I'm a
...this is the current output for I was hoping the default new output would leave the output as is (including the decimals):
And like Benito I'm more in favor of a
|
Any comments? |
@Reino17 15 months later, if it still matters … Drafts of the QT4 specs can be found here: https://qt4cg.org/ |
It looks to me as if |
For symmetry, This issue was initially about the parsing of numbers, but it also includes hints to formatting numbers. I’ve revised the title and the first comment of the issue. |
I believe we can close this issue as it has effectively been implemented. For parsing JSON, we now have a number-parser option that can handle numbers having more digits than an xs:double will accommodate. Similarly xml-to-json has a number-formatter option. We haven't changed the serialization spec, which allows the serializer to choose any representation of a number that is legal JSON. That basically puts the responsibility for finding an output format that meets user requirements on the implementor. Saxon is now using exponential notation only for double values outside the range 1e-18 to 1e+18, which is conformant with both the 3.1 and 4.0 specs. I believe this meets user requirements but I wouldn't want to impose it as standard behaviour. |
Wouldn't it be intuitive to have an analogous serialization parameter? How does Saxon serialize NaN and INF? serialize(
{ "invalid": xs:double('NaN') },
{ 'method': 'json', 'number-formatter': string#1 }
) |
The CG agreed to close this issue without further action at meeting 070. |
To-dos:
number-formatter
to thejson
method in the serialization spec.Sometimes people put very large numbers in a JSON file, if they are parsed as double, the numbers are corrupted. Or they become confused when
1000000
from the input becomes1e6
in the output. Finally parsing a double is slower than parsing an integer.There could be an additional option for parse-json/json-doc
number-type
with possible values:double
: Parse all numbers asxs:double
decimal
: Parse all numbers asxs:decimal
string
: Return numbers asxs:string
(so1e6
stays"1e6"
and1000000
stays"1000000"
)auto
: Parse numbers containinge
asxs:double
, numbers containing.
asxs:decimal
, and numbers containing neither asxs:integer
The text was updated successfully, but these errors were encountered: