| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -118,41 +118,7 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) | |||
| 118 | 118 | x.back(); | |
| 119 | 119 | this.myArrayList.add(x.nextValue()); | |
| 120 | 120 | } | |
| 121 | - switch (x.nextClean()) { | ||
| 122 | - case 0: | ||
| 123 | - // array is unclosed. No ']' found, instead EOF | ||
| 124 | - throw x.syntaxError("Expected a ',' or ']'"); | ||
| 125 | - case ',': | ||
| 126 | - nextChar = x.nextClean(); | ||
| 127 | - if (nextChar == 0) { | ||
| 128 | - // array is unclosed. No ']' found, instead EOF | ||
| 129 | - throw x.syntaxError("Expected a ',' or ']'"); | ||
| 130 | - } | ||
| 131 | - if (nextChar == ']') { | ||
| 132 | - // trailing commas are not allowed in strict mode | ||
| 133 | - if (jsonParserConfiguration.isStrictMode()) { | ||
| 134 | - throw x.syntaxError("Strict mode error: Expected another array element"); | ||
| 135 | - } | ||
| 136 | - return; | ||
| 137 | - } | ||
| 138 | - if (nextChar == ',') { | ||
| 139 | - // consecutive commas are not allowed in strict mode | ||
| 140 | - if (jsonParserConfiguration.isStrictMode()) { | ||
| 141 | - throw x.syntaxError("Strict mode error: Expected a valid array element"); | ||
| 142 | - } | ||
| 143 | - return; | ||
| 144 | - } | ||
| 145 | - x.back(); | ||
| 146 | - break; | ||
| 147 | - case ']': | ||
| 148 | - if (isInitial && jsonParserConfiguration.isStrictMode() && | ||
| 149 | - x.nextClean() != 0) { | ||
| 150 | - throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text"); | ||
| 151 | - } | ||
| 152 | - return; | ||
| 153 | - default: | ||
| 154 | - throw x.syntaxError("Expected a ',' or ']'"); | ||
| 155 | - } | ||
| 121 | + if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return; | ||
| 156 | 122 | } | |
| 157 | 123 | } else { | |
| 158 | 124 | if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) { | |
@@ -161,6 +127,52 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) | |||
| 161 | 127 | } | |
| 162 | 128 | } | |
| 163 | 129 | ||
| 130 | + /** Convenience function. Checks for JSON syntax error. | ||
| 131 | + * @param x A JSONTokener instance from which the JSONArray is constructed. | ||
| 132 | + * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. | ||
| 133 | + * @param isInitial Boolean indicating position of char | ||
| 134 | + * @return | ||
| 135 | + */ | ||
| 136 | + private static boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) { | ||
| 137 | + char nextChar; | ||
| 138 | + switch (x.nextClean()) { | ||
| 139 | + case 0: | ||
| 140 | + // array is unclosed. No ']' found, instead EOF | ||
| 141 | + throw x.syntaxError("Expected a ',' or ']'"); | ||
| 142 | + case ',': | ||
| 143 | + nextChar = x.nextClean(); | ||
| 144 | + if (nextChar == 0) { | ||
| 145 | + // array is unclosed. No ']' found, instead EOF | ||
| 146 | + throw x.syntaxError("Expected a ',' or ']'"); | ||
| 147 | + } | ||
| 148 | + if (nextChar == ']') { | ||
| 149 | + // trailing commas are not allowed in strict mode | ||
| 150 | + if (jsonParserConfiguration.isStrictMode()) { | ||
| 151 | + throw x.syntaxError("Strict mode error: Expected another array element"); | ||
| 152 | + } | ||
| 153 | + return true; | ||
| 154 | + } | ||
| 155 | + if (nextChar == ',') { | ||
| 156 | + // consecutive commas are not allowed in strict mode | ||
| 157 | + if (jsonParserConfiguration.isStrictMode()) { | ||
| 158 | + throw x.syntaxError("Strict mode error: Expected a valid array element"); | ||
| 159 | + } | ||
| 160 | + return true; | ||
| 161 | + } | ||
| 162 | + x.back(); | ||
| 163 | + break; | ||
| 164 | + case ']': | ||
| 165 | + if (isInitial && jsonParserConfiguration.isStrictMode() && | ||
| 166 | + x.nextClean() != 0) { | ||
| 167 | + throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text"); | ||
| 168 | + } | ||
| 169 | + return true; | ||
| 170 | + default: | ||
| 171 | + throw x.syntaxError("Expected a ',' or ']'"); | ||
| 172 | + } | ||
| 173 | + return false; | ||
| 174 | + } | ||
| 175 | + | ||
| 164 | 176 | /** | |
| 165 | 177 | * Construct a JSONArray from a source JSON text. | |
| 166 | 178 | * | |
@@ -735,11 +747,7 @@ public double optDouble(int index, double defaultValue) { | |||
| 735 | 747 | if (val == null) { | |
| 736 | 748 | return defaultValue; | |
| 737 | 749 | } | |
| 738 | - final double doubleValue = val.doubleValue(); | ||
| 739 | - // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { | ||
| 740 | - // return defaultValue; | ||
| 741 | - // } | ||
| 742 | - return doubleValue; | ||
| 750 | + return val.doubleValue(); | ||
| 743 | 751 | } | |
| 744 | 752 | ||
| 745 | 753 | /** | |
@@ -771,11 +779,7 @@ public Double optDoubleObject(int index, Double defaultValue) { | |||
| 771 | 779 | if (val == null) { | |
| 772 | 780 | return defaultValue; | |
| 773 | 781 | } | |
| 774 | - final Double doubleValue = val.doubleValue(); | ||
| 775 | - // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { | ||
| 776 | - // return defaultValue; | ||
| 777 | - // } | ||
| 778 | - return doubleValue; | ||
| 782 | + return val.doubleValue(); | ||
| 779 | 783 | } | |
| 780 | 784 | ||
| 781 | 785 | /** | |
@@ -807,11 +811,7 @@ public float optFloat(int index, float defaultValue) { | |||
| 807 | 811 | if (val == null) { | |
| 808 | 812 | return defaultValue; | |
| 809 | 813 | } | |
| 810 | - final float floatValue = val.floatValue(); | ||
| 811 | - // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { | ||
| 812 | - // return floatValue; | ||
| 813 | - // } | ||
| 814 | - return floatValue; | ||
| 814 | + return val.floatValue(); | ||
| 815 | 815 | } | |
| 816 | 816 | ||
| 817 | 817 | /** | |
@@ -843,11 +843,7 @@ public Float optFloatObject(int index, Float defaultValue) { | |||
| 843 | 843 | if (val == null) { | |
| 844 | 844 | return defaultValue; | |
| 845 | 845 | } | |
| 846 | - final Float floatValue = val.floatValue(); | ||
| 847 | - // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { | ||
| 848 | - // return floatValue; | ||
| 849 | - // } | ||
| 850 | - return floatValue; | ||
| 846 | + return val.floatValue(); | ||
| 851 | 847 | } | |
| 852 | 848 | ||
| 853 | 849 | /** | |
@@ -1645,29 +1641,44 @@ public boolean similar(Object other) { | |||
| 1645 | 1641 | if(valueThis == null) { | |
| 1646 | 1642 | return false; | |
| 1647 | 1643 | } | |
| 1648 | - if (valueThis instanceof JSONObject) { | ||
| 1649 | - if (!((JSONObject)valueThis).similar(valueOther)) { | ||
| 1650 | - return false; | ||
| 1651 | - } | ||
| 1652 | - } else if (valueThis instanceof JSONArray) { | ||
| 1653 | - if (!((JSONArray)valueThis).similar(valueOther)) { | ||
| 1654 | - return false; | ||
| 1655 | - } | ||
| 1656 | - } else if (valueThis instanceof Number && valueOther instanceof Number) { | ||
| 1657 | - if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) { | ||
| 1658 | - return false; | ||
| 1659 | - } | ||
| 1660 | - } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) { | ||
| 1661 | - if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) { | ||
| 1662 | - return false; | ||
| 1663 | - } | ||
| 1664 | - } else if (!valueThis.equals(valueOther)) { | ||
| 1644 | + if (!isSimilar(valueThis, valueOther)) { | ||
| 1665 | 1645 | return false; | |
| 1666 | 1646 | } | |
| 1667 | 1647 | } | |
| 1668 | 1648 | return true; | |
| 1669 | 1649 | } | |
| 1670 | 1650 | ||
| 1651 | + /** | ||
| 1652 | + * Convenience function; checks for object similarity | ||
| 1653 | + * @param valueThis | ||
| 1654 | + * Initial object to compare | ||
| 1655 | + * @param valueOther | ||
| 1656 | + * Comparison object | ||
| 1657 | + * @return boolean | ||
| 1658 | + */ | ||
| 1659 | + private boolean isSimilar(Object valueThis, Object valueOther) { | ||
| 1660 | + if (valueThis instanceof JSONObject) { | ||
| 1661 | + if (!((JSONObject)valueThis).similar(valueOther)) { | ||
| 1662 | + return false; | ||
| 1663 | + } | ||
| 1664 | + } else if (valueThis instanceof JSONArray) { | ||
| 1665 | + if (!((JSONArray)valueThis).similar(valueOther)) { | ||
| 1666 | + return false; | ||
| 1667 | + } | ||
| 1668 | + } else if (valueThis instanceof Number && valueOther instanceof Number) { | ||
| 1669 | + if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) { | ||
| 1670 | + return false; | ||
| 1671 | + } | ||
| 1672 | + } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) { | ||
| 1673 | + if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) { | ||
| 1674 | + return false; | ||
| 1675 | + } | ||
| 1676 | + } else if (!valueThis.equals(valueOther)) { | ||
| 1677 | + return false; | ||
| 1678 | + } | ||
| 1679 | + return true; | ||
| 1680 | + } | ||
| 1681 | + | ||
| 1671 | 1682 | /** | |
| 1672 | 1683 | * Produce a JSONObject by combining a JSONArray of names with the values of | |
| 1673 | 1684 | * this JSONArray. | |
@@ -1799,12 +1810,7 @@ public Writer write(Writer writer, int indentFactor, int indent) | |||
| 1799 | 1810 | writer.write('['); | |
| 1800 | 1811 | ||
| 1801 | 1812 | if (length == 1) { | |
| 1802 | - try { | ||
| 1803 | - JSONObject.writeValue(writer, this.myArrayList.get(0), | ||
| 1804 | - indentFactor, indent); | ||
| 1805 | - } catch (Exception e) { | ||
| 1806 | - throw new JSONException("Unable to write JSONArray value at index: 0", e); | ||
| 1807 | - } | ||
| 1813 | + writeArrayAttempt(writer, indentFactor, indent, 0); | ||
| 1808 | 1814 | } else if (length != 0) { | |
| 1809 | 1815 | final int newIndent = indent + indentFactor; | |
| 1810 | 1816 | ||
@@ -1816,12 +1822,7 @@ public Writer write(Writer writer, int indentFactor, int indent) | |||
| 1816 | 1822 | writer.write('\n'); | |
| 1817 | 1823 | } | |
| 1818 | 1824 | JSONObject.indent(writer, newIndent); | |
| 1819 | - try { | ||
| 1820 | - JSONObject.writeValue(writer, this.myArrayList.get(i), | ||
| 1821 | - indentFactor, newIndent); | ||
| 1822 | - } catch (Exception e) { | ||
| 1823 | - throw new JSONException("Unable to write JSONArray value at index: " + i, e); | ||
| 1824 | - } | ||
| 1825 | + writeArrayAttempt(writer, indentFactor, newIndent, i); | ||
| 1825 | 1826 | needsComma = true; | |
| 1826 | 1827 | } | |
| 1827 | 1828 | if (indentFactor > 0) { | |
@@ -1836,6 +1837,26 @@ public Writer write(Writer writer, int indentFactor, int indent) | |||
| 1836 | 1837 | } | |
| 1837 | 1838 | } | |
| 1838 | 1839 | ||
| 1840 | + /** | ||
| 1841 | + * Convenience function. Attempts to write | ||
| 1842 | + * @param writer | ||
| 1843 | + * Writes the serialized JSON | ||
| 1844 | + * @param indentFactor | ||
| 1845 | + * The number of spaces to add to each level of indentation. | ||
| 1846 | + * @param indent | ||
| 1847 | + * The indentation of the top level. | ||
| 1848 | + * @param i | ||
| 1849 | + * Index in array to be added | ||
| 1850 | + */ | ||
| 1851 | + private void writeArrayAttempt(Writer writer, int indentFactor, int indent, int i) { | ||
| 1852 | + try { | ||
| 1853 | + JSONObject.writeValue(writer, this.myArrayList.get(i), | ||
| 1854 | + indentFactor, indent); | ||
| 1855 | + } catch (Exception e) { | ||
| 1856 | + throw new JSONException("Unable to write JSONArray value at index: " + i, e); | ||
| 1857 | + } | ||
| 1858 | + } | ||
| 1859 | + | ||
| 1839 | 1860 | /** | |
| 1840 | 1861 | * Returns a java.util.List containing all of the elements in this array. | |
| 1841 | 1862 | * If an element in the array is a JSONArray or JSONObject it will also | |
| Back | FazBrowse Home | New Git URL |
0 commit comments