Skip to content
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

phone numbers that start with one 0 get treated as int #49

Closed
pcasaes opened this issue Mar 23, 2012 · 4 comments
Closed

phone numbers that start with one 0 get treated as int #49

pcasaes opened this issue Mar 23, 2012 · 4 comments

Comments

@pcasaes
Copy link

pcasaes commented Mar 23, 2012

Hello,

Here in brazil we have phone numbers that start with 0800 and they get treated as ints thereby losing the first 0.

Tracked down the error to this bit of code in XML.java

    try {
        char initial = string.charAt(0);
        boolean negative = false;
        if (initial == '-') {
            initial = string.charAt(1);
            negative = true;
        }
        if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
            return string;
        }
        if ((initial >= '0' && initial <= '9')) {
            if (string.indexOf('.') >= 0) {
                return Double.valueOf(string);
            } else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {
                Long myLong = new Long(string);
                if (myLong.longValue() == myLong.intValue()) {
                    return new Integer(myLong.intValue());
                } else {
                    return myLong;
                }
            }
        }
    }  catch (Exception ignore) {
    }

It seems to want to convert any value 0XNNNN... (where X isn't 0 and N is any digit) as an int.
Any particular reason for this?

@douglascrockford
Copy link
Contributor

Phone numbers should be represented as strings. Wrap them in quotes.

@pcasaes
Copy link
Author

pcasaes commented Mar 23, 2012

Hello,

The method doesn't convert 00222 into an int. If it the second character is a 0 is keeps it as a string.
Is this behaviour wrong? If not why not?

@douglascrockford
Copy link
Contributor

Integers in JSON may not start with a leading zero. See http://json.org/

@pcasaes
Copy link
Author

pcasaes commented Mar 23, 2012

Ah! now I understand.
I was focusing to much on ints that I forgot doubles.

I do believe the code is not following that specification correctly though.

changed this bit of code on XML.java, line 330:
if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
return string;
}
to
if (initial == '0' && string.charAt(negative ? 2 : 1) != '.') {
return string;
}

tested it on:
-123
123
-0.123
0.123

worked perfectly. thanks.

BGehrels pushed a commit to BGehrels/JSON-java that referenced this issue Apr 29, 2020
Add test that an invalid escape sequence results in a JSONException a…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants