-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Why does jq automatically convert double values to int when possible? #356
Comments
jq doesn't convert doubles to ints. It parses numbers as doubles, and it Any options would have to be global in effect, which might not be good |
See #369. |
I was planning to use jq for a project, but this problem alone is enough to make me not use it. |
On Mon, Feb 23, 2015 at 6:06 PM, schoo105 notifications@github.com wrote:
I'm sorry jq doesn't do what you need. Do be careful with JSON though: the |
@schoo105 - For what it's worth, I agree that the problem you mention is both serious and unfortunate. In fact, in my view, rfc7159 is, with respect to numbers, a step backwards relative to the original (Crockford) specification. However, with respect to jq, I hope you will consider some possible workarounds. If you don't have to do arithmetic on the large numbers, could they be converted to strings first? If you do have to do arithmetic on large integers, there is a (not-yet-very-well-tested) "BigInt" library for jq at https://gist.github.com/pkoppstein/d06a123f30c033195841 |
https://tools.ietf.org/rfcdiff?difftype=--hwdiff&url1=rfc4627&url2=rfc7159
The syntax for numbers certainly didn't change. In fact not much changed,
and jq adheres to the range and precision described as most common.
I'm not saying that jq does what it because the spec permits it. I'm
saying that expecting numbers to be formatted I'm a subset of the
prescribed syntax is skiing for trouble: chances are that lots of JSON
encoders will fail to meet your expectations.
May I ask what JSON parsers insist on a fractional part bring present?
|
@nicowilliams wrote:
The original JSON specification of |
@pkoppstein That's neither here nor there, but just in case @opqpop or @schoo105 are confused, and just because this comes up from time to time, so we might as well hash it out, let's we should cover this in some detail. First, and for whatever it might be worth (a lot, IMO), RFC4627 long predated the ECMA attempt at a JSON standard. Second, both permit leaving out a zero fractional part of a number (e.g., Anyone complaining that jq's data model is not the one they prefer could glare at Douglas Crockford, the IETF, and/or the ECMA for that matter, but the horse left the barn many years ago, and long before jq happened on the scene. As for the text in RFC7159 that you object to, a) it is informative, not normative (i.e., it requires nothing of implementors), b) it wasn't added to to change JSON, it was added to reflect actual deployments. To be sure, an informative note could create harm if taken as normative, but this text is clearly informative and harmless, and it is useful: it tells encoders what subset of numbers are likely to interoperate. Incidentally, JSON implementations come in these flavors as to numbers: those that implement IEEE754 as their numeric value range, precision, and semantics (minus NaNs and infinities, because JSON does not permit them), those that implement some sort of bigreal, and those that implement something much more constrained than IEEE754, none being strictly a superset or subset of the others. Obviously implementations with bigreal interoperate the best as parsers, but implementations more constrained than IEEE754 interoperate better than all the others _as encoders. Since the IETF is in the business of making interoperable specifications, an interoperability note had to be made -- nothing else would have made it possible to publish any update to RFC4627. But this is not relevant to either @opqpop or @schoo105, since they just want a fractional part to always be included, and that's permitted (but not required) by the standard, and has nothing to do with IEEE754. We might like to write or accept a patch to add an option to always include a Admittedly, JSON number representation is not something I spend a lot of time thinking about: because by and large what jq does (and all the other C JSON implementations I've used do) interoperates. But I'd really like to know what parser(s) @schoo105 is using that don't parse numbers encoded by jq, so that we can consider the importance of this issue/request in that context, and so that we can go file issues/requests against whatever that parser is. |
Regarding your gist, @pkoppstein, that's really cool! Also, to be clear, I have no idea what @schoo105 needs here, only what @opqpop wrote. I'm not at all certain that what @schoo105 wants is infinite (or even just better than IEEE754) range and precision numbers. |
BTW, the reason that IEEE754 came to be of prominence in JSON implementations is that IEEE754 is the common denominator in ECMAScript implementations, some even going so far as to use NaN coding, where immediate values (numbers, pointers) are all encoded as C doubles, which are half the size of libjq's |
@nicowilliams wrote:
Same here, but I remain discombobulated largely by the discrepancy between some of the "marketing materials" at github.com/stedolan and the reality. For example,
and for non-integral decimals:
So my own beef is primarily threefold:
I must admit, however, that my discombobulation will be reduced to the point of insignificance if/when |
I just use Python and its |
I have
input_file
:I run this
My output:
I would like the output to be
How do I prevent jq from automatically converting my double values to an int?
The text was updated successfully, but these errors were encountered: