Try to convert a string into a number, boolean, or null. If the string can't be converted, return the string. @param string A String. @return A simple JSON value.
(String string)
| 1531 | * @return A simple JSON value. |
| 1532 | */ |
| 1533 | static protected Object stringToValue(String string) { |
| 1534 | Double d; |
| 1535 | if (string.equals("")) { |
| 1536 | return string; |
| 1537 | } |
| 1538 | if (string.equalsIgnoreCase("true")) { |
| 1539 | return Boolean.TRUE; |
| 1540 | } |
| 1541 | if (string.equalsIgnoreCase("false")) { |
| 1542 | return Boolean.FALSE; |
| 1543 | } |
| 1544 | if (string.equalsIgnoreCase("null")) { |
| 1545 | return JSONObject.NULL; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * If it might be a number, try converting it. |
| 1550 | * If a number cannot be produced, then the value will just |
| 1551 | * be a string. Note that the plus and implied string |
| 1552 | * conventions are non-standard. A JSON parser may accept |
| 1553 | * non-JSON forms as long as it accepts all correct JSON forms. |
| 1554 | */ |
| 1555 | |
| 1556 | char b = string.charAt(0); |
| 1557 | if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') { |
| 1558 | try { |
| 1559 | if (string.indexOf('.') > -1 || |
| 1560 | string.indexOf('e') > -1 || string.indexOf('E') > -1) { |
| 1561 | d = Double.valueOf(string); |
| 1562 | if (!d.isInfinite() && !d.isNaN()) { |
| 1563 | return d; |
| 1564 | } |
| 1565 | } else { |
| 1566 | Long myLong = Long.valueOf(string); |
| 1567 | if (myLong.longValue() == myLong.intValue()) { |
| 1568 | return Integer.valueOf(myLong.intValue()); |
| 1569 | } else { |
| 1570 | return myLong; |
| 1571 | } |
| 1572 | } |
| 1573 | } catch (Exception ignore) { |
| 1574 | } |
| 1575 | } |
| 1576 | return string; |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | /** |