question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

I have been cross testing various json parsers looking for those that expose the lexical of json numbers and not only their bound java.lang.Number. Because of the lazy parsing done by gson with LazilyParsedNumber, that keeps the lexical, all my roundtrip tests pass apart one: the lexical -0 that is treated as it were 0

I read some threads about negative zero: https://www.ietf.org/mail-archive/web/json/current/msg03668.html https://www.ietf.org/mail-archive/web/json/current/msg01520.html https://www.ietf.org/mail-archive/web/json/current/msg01523.html https://www.ietf.org/mail-archive/web/json/current/msg01525.html

I created this issue thinking that -0 is a float, the same as -0.0, since a signed zero makes sense only in floating point numbers and also because in Java only Double/Float preserve sign of zero. This would have the implication that -0 could not be validated by jsonschema type integer , and that a jsonschema implementation would have the need to know if a -0 is present in json data, but probably this is not the case.

After I started to (re)consider that -0 could be an integer, only that seems that in no programming language there is an integer that preserves sign for zero.

In any case, differentiating between 0 and -0 at lexical level would allow a client of gson to be able to refuse the value -0.

Gson could easily support differentiating between 0 and -0: in code -0 is treated as an integer (PEEKED_LONG) in JsonReader so its value is stored in a Java long that cannot represent negative zero. I noted that -0.0 roundtrips correctly because is treated as a PEEKED_NUMBER that is kept as a Java String. So the case of -0 could be trapped and treated as -0.0, as a PEEKED_NUMBER, in this way the toString() method of LazilyParsedNumber will return -0 and gson will be able to roundtrip any valid number value found in source, only clients using Number.toString() will notice any difference.

My proposal is to change this code from

      if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative)) {

to

      if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value!=0 || false==negative) && (value != Long.MIN_VALUE || negative)) {

Thanks, Michele

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
inder123commented, Apr 20, 2017

Thanks. Yes, TDD for bug fixes is awesome.

0reactions
eleumikcommented, Apr 20, 2017

yes, I wrote first the test. I double checked now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Signed zero - Wikipedia
The notation "−0" may be used informally to denote a negative number that has been rounded to zero. The concept of negative zero...
Read more >
Does negative zero exist? [closed] - Math Stack Exchange
There is a negative 0, it just happens to be equal to the normal zero. For each real number a, we have a...
Read more >
Does negative zero exist? - Quora
Yes it does. It just happens to be equal to zero. As the positive and negative versions of zero equals each other, it's...
Read more >
negative zero - Wiktionary
(computing) A signed magnitude representation of zero in which the sign bit indicates a negative number. One must take care in handling negative...
Read more >
Negative Zero (@thenegativezeromusic) • Instagram photos ...
129 Followers, 196 Following, 19 Posts - See Instagram photos and videos from Negative Zero (@thenegativezeromusic)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found