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