diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 4afee844346fb0ce91c856dc0ae514fe8b64ed4f..df4bf7981ad4225251dc087a561ef8e8eab757a1 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -25,11 +25,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -40,4 +40,4 @@ jobs: - run: "mvn clean compile -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true" - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 08352a085fd69569695c5990c1f0ade102de3c77..5e1dd4251cd476564900e18b151998d0d5b559c7 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -10,46 +10,21 @@ on: branches: [ master ] jobs: - # old-school build and jar method. No tests run or compiled. - build-1_6: - runs-on: ubuntu-latest - strategy: - matrix: - # build for java 1.6, however don't run any tests - java: [ 1.6 ] - name: Java ${{ matrix.java }} - steps: - - uses: actions/checkout@v2 - - name: Setup java - uses: actions/setup-java@v1 - with: - java-version: ${{ matrix.java }} - - name: Compile Java ${{ matrix.java }} - run: | - mkdir -p target/classes - javac -d target/classes/ src/main/java/org/json/*.java - - name: Create java ${{ matrix.java }} JAR - run: | - jar cvf target/org.json.jar -C target/classes . - - name: Upload Java ${{ matrix.java }} JAR - uses: actions/upload-artifact@v1 - with: - name: Java ${{ matrix.java }} JAR - path: target/org.json.jar - build: runs-on: ubuntu-latest strategy: matrix: # build against supported Java LTS versions: - java: [ 8, 11 ] + java: [ 8, 11, 17 ] name: Java ${{ matrix.java }} steps: - - uses: actions/checkout@v2 - - name: Setup java - uses: actions/setup-java@v1 + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3 with: + distribution: 'temurin' java-version: ${{ matrix.java }} + cache: 'maven' - name: Compile Java ${{ matrix.java }} run: mvn clean compile -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true - name: Run Tests ${{ matrix.java }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d81ff6147a1515d1be41db5e66d52cd33721a12d..8102dcf6335c371503433428a95ee5d2636ab4ff 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,8 +1,8 @@ # Contribution Guidelines -Feel free to work on any issue with a #hacktoberfest label. +Feel free to work on any open issue, you don't need to ask permission first. This year, the hacktoberfest label will be added to any PR and associated issue during the month of October. -If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, a hacktoberfest label will be added. +If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, it will be available to work on (if not, it will be closed after review). # Who is allowed to submit pull requests for this project? diff --git a/README.md b/README.md index e999230ba14d958fcea2daf4aabd81ae33d9485e..9f0134206db0d4511f561e1ab148cb9d0993eb94 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ JSON in Java [package org.json] [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230618/json-20230618.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20231013/json-20231013.jar)** # Overview diff --git a/build.gradle b/build.gradle index 63a31a73e3ceb7eacc554a84d47cf8f4ee0de50d..fbc2ff1f1c795be4835a79eca74e72434d3e8c24 100644 --- a/build.gradle +++ b/build.gradle @@ -20,9 +20,9 @@ repositories { } dependencies { - testImplementation 'junit:junit:4.13.1' + testImplementation 'junit:junit:4.13.2' testImplementation 'com.jayway.jsonpath:json-path:2.1.0' - testImplementation 'org.mockito:mockito-core:1.9.5' + testImplementation 'org.mockito:mockito-core:4.2.0' } subprojects { @@ -30,9 +30,9 @@ subprojects { } group = 'org.json' -version = 'v20211205-SNAPSHOT' +version = 'v20230618-SNAPSHOT' description = 'JSON in Java' -sourceCompatibility = '1.7' +sourceCompatibility = '1.8' configurations.all { } diff --git a/docs/RELEASES.md b/docs/RELEASES.md index ae439831ca053319d4adea7722347fb39aed654a..2b8aaa26702ab866b5824b5c3c9b8bb0630b159f 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -5,6 +5,8 @@ and artifactId "json". For example: [https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav) ~~~ +20231013 First release with minimum Java version 1.8. Recent commits, including fixes for CVE-2023-5072. + 20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater. 20230227 Fix for CVE-2022-45688 and recent commits diff --git a/pom.xml b/pom.xml index b9e0e608d978beaf0878e851c44117e7d8a98b21..77bbdaca38096204d8fac9b0f553e189d674d1f2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.json json - 20230618 + 20231013 bundle JSON in Java @@ -15,7 +15,7 @@ It also includes the capability to convert between JSON and XML, HTTP headers, Cookies, and CDL. - This is a reference implementation. There is a large number of JSON packages + This is a reference implementation. There are a large number of JSON packages in Java. Perhaps someday the Java community will standardize on one. Until then, choose carefully. @@ -57,7 +57,7 @@ junit junit - 4.13.1 + 4.13.2 test @@ -69,7 +69,7 @@ org.mockito mockito-core - 1.9.5 + 4.2.0 test @@ -79,7 +79,7 @@ org.apache.felix maven-bundle-plugin - 3.0.1 + 5.1.9 true @@ -93,16 +93,16 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.11.0 - 1.6 - 1.6 + 1.8 + 1.8 org.apache.maven.plugins maven-source-plugin - 2.1.2 + 3.3.0 attach-sources @@ -115,7 +115,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.7 + 3.5.0 attach-javadocs @@ -131,7 +131,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.5 + 1.6 sign-artifacts @@ -162,7 +162,7 @@ org.apache.maven.plugins maven-jar-plugin - 3.2.0 + 3.3.0 diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 375d03888e054dfeba15a88a8c36ac0876e8597c..b0c912d1fbdcd730f076d9b84870f64101d03291 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -924,30 +924,57 @@ public class JSONArray implements Iterable { } /** - * Get the optional JSONArray associated with an index. + * Get the optional JSONArray associated with an index. Null is returned if + * there is no value at that index or if the value is not a JSONArray. * * @param index - * subscript - * @return A JSONArray value, or null if the index has no value, or if the - * value is not a JSONArray. + * The index must be between 0 and length() - 1. + * @return A JSONArray value. */ public JSONArray optJSONArray(int index) { - Object o = this.opt(index); - return o instanceof JSONArray ? (JSONArray) o : null; + return this.optJSONArray(index, null); + } + + /** + * Get the optional JSONArray associated with an index. The defaultValue is returned if + * there is no value at that index or if the value is not a JSONArray. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default. + * @return A JSONArray value. + */ + public JSONArray optJSONArray(int index, JSONArray defaultValue) { + Object object = this.opt(index); + return object instanceof JSONArray ? (JSONArray) object : defaultValue; } /** * Get the optional JSONObject associated with an index. Null is returned if - * the key is not found, or null if the index has no value, or if the value - * is not a JSONObject. + * there is no value at that index or if the value is not a JSONObject. * * @param index * The index must be between 0 and length() - 1. * @return A JSONObject value. */ public JSONObject optJSONObject(int index) { - Object o = this.opt(index); - return o instanceof JSONObject ? (JSONObject) o : null; + return this.optJSONObject(index, null); + } + + /** + * Get the optional JSONObject associated with an index. The defaultValue is returned if + * there is no value at that index or if the value is not a JSONObject. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default. + * @return A JSONObject value. + */ + public JSONObject optJSONObject(int index, JSONObject defaultValue) { + Object object = this.opt(index); + return object instanceof JSONObject ? (JSONObject) object : defaultValue; } /** @@ -1619,9 +1646,7 @@ public class JSONArray implements Iterable { @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { StringWriter sw = new StringWriter(); - synchronized (sw.getBuffer()) { - return this.write(sw, indentFactor, 0).toString(); - } + return this.write(sw, indentFactor, 0).toString(); } /** diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5e00eb9a3498e1a47495ab743d37a3f6b99ded16..fbf225e9f9e236b4021fb7b0c48b78a6d870fdf6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -208,22 +208,14 @@ public class JSONObject { throw x.syntaxError("A JSONObject text must begin with '{'"); } for (;;) { - char prev = x.getPrevious(); c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; - case '{': - case '[': - if(prev=='{') { - throw x.syntaxError("A JSON Object can not directly nest another JSON Object or JSON Array."); - } - // fall through default: - x.back(); - key = x.nextValue().toString(); + key = x.nextSimpleValue(c).toString(); } // The key is followed by ':'. @@ -291,6 +283,7 @@ public class JSONObject { } final Object value = e.getValue(); if (value != null) { + testValidity(value); this.map.put(String.valueOf(e.getKey()), wrap(value)); } } @@ -354,6 +347,8 @@ public class JSONObject { * @param bean * An object that has getter methods that should be used to make * a JSONObject. + * @throws JSONException + * If a getter returned a non-finite number. */ public JSONObject(Object bean) { this(); @@ -1512,8 +1507,22 @@ public class JSONObject { * @return A JSONArray which is the value. */ public JSONArray optJSONArray(String key) { - Object o = this.opt(key); - return o instanceof JSONArray ? (JSONArray) o : null; + return this.optJSONArray(key, null); + } + + /** + * Get an optional JSONArray associated with a key, or the default if there + * is no such key, or if its value is not a JSONArray. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return A JSONArray which is the value. + */ + public JSONArray optJSONArray(String key, JSONArray defaultValue) { + Object object = this.opt(key); + return object instanceof JSONArray ? (JSONArray) object : defaultValue; } /** @@ -1685,6 +1694,8 @@ public class JSONObject { * * @param bean * the bean + * @throws JSONException + * If a getter returned a non-finite number. */ private void populateMap(Object bean) { populateMap(bean, Collections.newSetFromMap(new IdentityHashMap())); @@ -1712,21 +1723,22 @@ public class JSONObject { final Object result = method.invoke(bean); if (result != null) { // check cyclic dependency and throw error if needed - // the wrap and populateMap combination method is + // the wrap and populateMap combination method is // itself DFS recursive if (objectsRecord.contains(result)) { throw recursivelyDefinedObjectException(key); } - + objectsRecord.add(result); + testValidity(result); this.map.put(key, wrap(result, objectsRecord)); objectsRecord.remove(result); // we don't use the result anywhere outside of wrap // if it's a resource we should be sure to close it - // after calling toString + // after calling toString if (result instanceof Closeable) { try { ((Closeable) result).close(); @@ -2177,13 +2189,11 @@ public class JSONObject { @SuppressWarnings("resource") public static String quote(String string) { StringWriter sw = new StringWriter(); - synchronized (sw.getBuffer()) { - try { - return quote(string, sw).toString(); - } catch (IOException ignored) { - // will never happen - we are writing to a string writer - return ""; - } + try { + return quote(string, sw).toString(); + } catch (IOException ignored) { + // will never happen - we are writing to a string writer + return ""; } } @@ -2375,14 +2385,21 @@ public class JSONObject { * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. * - * @param val value to convert + * @param input value to convert * @return Number representation of the value. * @throws NumberFormatException thrown if the value is not a valid number. A public * caller should catch this and wrap it in a {@link JSONException} if applicable. */ - protected static Number stringToNumber(final String val) throws NumberFormatException { + protected static Number stringToNumber(final String input) throws NumberFormatException { + String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if ((initial >= '0' && initial <= '9') || initial == '-' ) { // decimal representation if (isDecimalNotation(val)) { // Use a BigDecimal all the time so we keep the original @@ -2399,25 +2416,26 @@ public class JSONObject { try { Double d = Double.valueOf(val); if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } return d; } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } } - // block items like 00 01 etc. Java number parsers treat these as Octal. + val = removeLeadingZerosOfNumber(input); + initial = val.charAt(0); if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } // integer representation. @@ -2437,7 +2455,7 @@ public class JSONObject { } return bi; } - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } /** @@ -2473,8 +2491,7 @@ public class JSONObject { * produced, then the value will just be a string. */ - char initial = string.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if (potentialNumber(string)) { try { return stringToNumber(string); } catch (Exception ignore) { @@ -2483,6 +2500,28 @@ public class JSONObject { return string; } + + private static boolean potentialNumber(String value){ + if (value == null || value.isEmpty()){ + return false; + } + return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); + } + + private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ + if (index >= value.length()){ + return false; + } + return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); + } + + private static boolean digitAtIndex(String value, int index){ + if (index >= value.length()){ + return false; + } + return value.charAt(index) >= '0' && value.charAt(index) <= '9'; + } + /** * Throw an exception if the object is a NaN or infinite number. * @@ -2570,9 +2609,7 @@ public class JSONObject { @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { StringWriter w = new StringWriter(); - synchronized (w.getBuffer()) { - return this.write(w, indentFactor, 0).toString(); - } + return this.write(w, indentFactor, 0).toString(); } /** @@ -2884,4 +2921,24 @@ public class JSONObject { "JavaBean object contains recursively defined member variable of key " + quote(key) ); } + + /** + * For a prospective number, remove the leading zeros + * @param value prospective number + * @return number without leading zeros + */ + private static String removeLeadingZerosOfNumber(String value){ + if (value.equals("-")){return value;} + boolean negativeFirstChar = (value.charAt(0) == '-'); + int counter = negativeFirstChar ? 1:0; + while (counter < value.length()){ + if (value.charAt(counter) != '0'){ + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); + } + ++counter; + } + if (negativeFirstChar) {return "-0";} + return "0"; + } } diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 5dc8ae85a6574cb5efe482d7119b34c6acda8681..4a83a697164d60be0e994432842138a98e3885c6 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -402,12 +402,7 @@ public class JSONTokener { */ public Object nextValue() throws JSONException { char c = this.nextClean(); - String string; - switch (c) { - case '"': - case '\'': - return this.nextString(c); case '{': this.back(); try { @@ -423,6 +418,17 @@ public class JSONTokener { throw new JSONException("JSON Array or Object depth too large to process.", e); } } + return nextSimpleValue(c); + } + + Object nextSimpleValue(char c) { + String string; + + switch (c) { + case '"': + case '\'': + return this.nextString(c); + } /* * Handle unquoted text. This could be the values true, false, or diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index aea4e30e8e1c939195a3c44031802283e6acecae..e877070d0adec8a08248446218c5e892db96e2e6 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -118,7 +118,7 @@ public class JSONArrayTest { * Expects a JSONException. */ @Test - public void emptStr() { + public void emptyStr() { String str = ""; try { assertNull("Should throw an exception", new JSONArray(str)); @@ -368,16 +368,16 @@ public class JSONArrayTest { "hello".equals(jsonArray.getString(4))); // doubles assertTrue("Array double", - new Double(23.45e-4).equals(jsonArray.getDouble(5))); + Double.valueOf(23.45e-4).equals(jsonArray.getDouble(5))); assertTrue("Array string double", - new Double(23.45).equals(jsonArray.getDouble(6))); + Double.valueOf(23.45).equals(jsonArray.getDouble(6))); assertTrue("Array double can be float", - new Float(23.45e-4f).equals(jsonArray.getFloat(5))); + Float.valueOf(23.45e-4f).equals(jsonArray.getFloat(5))); // ints assertTrue("Array value int", - new Integer(42).equals(jsonArray.getInt(7))); + Integer.valueOf(42).equals(jsonArray.getInt(7))); assertTrue("Array value string int", - new Integer(43).equals(jsonArray.getInt(8))); + Integer.valueOf(43).equals(jsonArray.getInt(8))); // nested objects JSONArray nestedJsonArray = jsonArray.getJSONArray(9); assertTrue("Array value JSONArray", nestedJsonArray != null); @@ -385,9 +385,9 @@ public class JSONArrayTest { assertTrue("Array value JSONObject", nestedJsonObject != null); // longs assertTrue("Array value long", - new Long(0).equals(jsonArray.getLong(11))); + Long.valueOf(0).equals(jsonArray.getLong(11))); assertTrue("Array value string long", - new Long(-1).equals(jsonArray.getLong(12))); + Long.valueOf(-1).equals(jsonArray.getLong(12))); assertTrue("Array value null", jsonArray.isNull(-1)); Util.checkJSONArrayMaps(jsonArray); @@ -460,6 +460,20 @@ public class JSONArrayTest { Util.checkJSONArrayMaps(jsonArray); } + /** + * The JSON parser is permissive of unambiguous unquoted keys and values. + * Such JSON text should be allowed, even if it does not strictly conform + * to the spec. However, after being parsed, toString() should emit strictly + * conforming JSON text. + */ + @Test + public void unquotedText() { + String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]"; + JSONArray jsonArray = new JSONArray(str); + List expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45"); + assertEquals(expected, jsonArray.toList()); + } + /** * Exercise JSONArray.join() by converting a JSONArray into a * comma-separated string. Since this is very nearly a JSON document, @@ -545,11 +559,11 @@ public class JSONArrayTest { Boolean.FALSE.equals(jsonArray.optBooleanObject(-1))); assertTrue("Array opt double", - new Double(23.45e-4).equals(jsonArray.optDouble(5))); + Double.valueOf(23.45e-4).equals(jsonArray.optDouble(5))); assertTrue("Array opt double default", - new Double(1).equals(jsonArray.optDouble(0, 1))); + Double.valueOf(1).equals(jsonArray.optDouble(0, 1))); assertTrue("Array opt double default implicit", - new Double(jsonArray.optDouble(99)).isNaN()); + Double.valueOf(jsonArray.optDouble(99)).isNaN()); assertTrue("Array opt double object", Double.valueOf(23.45e-4).equals(jsonArray.optDoubleObject(5))); @@ -559,11 +573,11 @@ public class JSONArrayTest { jsonArray.optDoubleObject(99).isNaN()); assertTrue("Array opt float", - new Float(23.45e-4).equals(jsonArray.optFloat(5))); + Float.valueOf(Double.valueOf(23.45e-4).floatValue()).equals(jsonArray.optFloat(5))); assertTrue("Array opt float default", - new Float(1).equals(jsonArray.optFloat(0, 1))); + Float.valueOf(1).equals(jsonArray.optFloat(0, 1))); assertTrue("Array opt float default implicit", - new Float(jsonArray.optFloat(99)).isNaN()); + Float.valueOf(jsonArray.optFloat(99)).isNaN()); assertTrue("Array opt float object", Float.valueOf(23.45e-4F).equals(jsonArray.optFloatObject(5))); @@ -575,14 +589,14 @@ public class JSONArrayTest { assertTrue("Array opt Number", BigDecimal.valueOf(23.45e-4).equals(jsonArray.optNumber(5))); assertTrue("Array opt Number default", - new Double(1).equals(jsonArray.optNumber(0, 1d))); + Double.valueOf(1).equals(jsonArray.optNumber(0, 1d))); assertTrue("Array opt Number default implicit", - new Double(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN()); + Double.valueOf(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN()); assertTrue("Array opt int", - new Integer(42).equals(jsonArray.optInt(7))); + Integer.valueOf(42).equals(jsonArray.optInt(7))); assertTrue("Array opt int default", - new Integer(-1).equals(jsonArray.optInt(0, -1))); + Integer.valueOf(-1).equals(jsonArray.optInt(0, -1))); assertTrue("Array opt int default implicit", 0 == jsonArray.optInt(0)); @@ -595,13 +609,17 @@ public class JSONArrayTest { JSONArray nestedJsonArray = jsonArray.optJSONArray(9); assertTrue("Array opt JSONArray", nestedJsonArray != null); - assertTrue("Array opt JSONArray default", + assertTrue("Array opt JSONArray null", null == jsonArray.optJSONArray(99)); + assertTrue("Array opt JSONArray default", + "value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0))); JSONObject nestedJsonObject = jsonArray.optJSONObject(10); assertTrue("Array opt JSONObject", nestedJsonObject != null); - assertTrue("Array opt JSONObject default", + assertTrue("Array opt JSONObject null", null == jsonArray.optJSONObject(99)); + assertTrue("Array opt JSONObject default", + "value".equals(jsonArray.optJSONObject(99, new JSONObject("{\"key\":\"value\"}")).getString("key"))); assertTrue("Array opt long", 0 == jsonArray.optLong(11)); @@ -1011,12 +1029,12 @@ public class JSONArrayTest { assertTrue("Array double [23.45e-4]", new BigDecimal("0.002345").equals(it.next())); assertTrue("Array string double", - new Double(23.45).equals(Double.parseDouble((String)it.next()))); + Double.valueOf(23.45).equals(Double.parseDouble((String)it.next()))); assertTrue("Array value int", - new Integer(42).equals(it.next())); + Integer.valueOf(42).equals(it.next())); assertTrue("Array value string int", - new Integer(43).equals(Integer.parseInt((String)it.next()))); + Integer.valueOf(43).equals(Integer.parseInt((String)it.next()))); JSONArray nestedJsonArray = (JSONArray)it.next(); assertTrue("Array value JSONArray", nestedJsonArray != null); @@ -1025,9 +1043,9 @@ public class JSONArrayTest { assertTrue("Array value JSONObject", nestedJsonObject != null); assertTrue("Array value long", - new Long(0).equals(((Number) it.next()).longValue())); + Long.valueOf(0).equals(((Number) it.next()).longValue())); assertTrue("Array value string long", - new Long(-1).equals(Long.parseLong((String) it.next()))); + Long.valueOf(-1).equals(Long.parseLong((String) it.next()))); assertTrue("should be at end of array", !it.hasNext()); Util.checkJSONArraysMaps(new ArrayList(Arrays.asList( jsonArray, nestedJsonArray @@ -1369,7 +1387,7 @@ public class JSONArrayTest { @Test(expected = JSONException.class) public void issue654StackOverflowInputWellFormed() { //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes)); - final InputStream resourceAsStream = JSONObjectTest.class.getClassLoader().getResourceAsStream("Issue654WellFormedArray.json"); + final InputStream resourceAsStream = JSONArrayTest.class.getClassLoader().getResourceAsStream("Issue654WellFormedArray.json"); JSONTokener tokener = new JSONTokener(resourceAsStream); JSONArray json_input = new JSONArray(tokener); assertNotNull(json_input); diff --git a/src/test/java/org/json/junit/JSONObjectDecimalTest.java b/src/test/java/org/json/junit/JSONObjectDecimalTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3302f0a24a8ed2e6f0dd2502a3ebf8380de13c00 --- /dev/null +++ b/src/test/java/org/json/junit/JSONObjectDecimalTest.java @@ -0,0 +1,100 @@ +package org.json.junit; + +import org.json.JSONObject; +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import static org.junit.Assert.assertEquals; + +public class JSONObjectDecimalTest { + + @Test + public void shouldParseDecimalNumberThatStartsWithDecimalPoint(){ + JSONObject jsonObject = new JSONObject("{value:0.50}"); + assertEquals("Float not recognized", 0.5f, jsonObject.getFloat("value"), 0.0f); + assertEquals("Float not recognized", 0.5f, jsonObject.optFloat("value"), 0.0f); + assertEquals("Float not recognized", 0.5f, jsonObject.optFloatObject("value"), 0.0f); + assertEquals("Double not recognized", 0.5d, jsonObject.optDouble("value"), 0.0f); + assertEquals("Double not recognized", 0.5d, jsonObject.optDoubleObject("value"), 0.0f); + assertEquals("Double not recognized", 0.5d, jsonObject.getDouble("value"), 0.0f); + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.5).compareTo(jsonObject.getBigDecimal("value"))); + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); + } + + + + @Test + public void shouldParseNegativeDecimalNumberThatStartsWithDecimalPoint(){ + JSONObject jsonObject = new JSONObject("{value:-.50}"); + assertEquals("Float not recognized", -0.5f, jsonObject.getFloat("value"), 0.0f); + assertEquals("Float not recognized", -0.5f, jsonObject.optFloat("value"), 0.0f); + assertEquals("Float not recognized", -0.5f, jsonObject.optFloatObject("value"), 0.0f); + assertEquals("Double not recognized", -0.5d, jsonObject.optDouble("value"), 0.0f); + assertEquals("Double not recognized", -0.5d, jsonObject.optDoubleObject("value"), 0.0f); + assertEquals("Double not recognized", -0.5d, jsonObject.getDouble("value"), 0.0f); + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.5).compareTo(jsonObject.getBigDecimal("value"))); + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); + } + + @Test + public void shouldParseDecimalNumberThatHasZeroBeforeWithDecimalPoint(){ + JSONObject jsonObject = new JSONObject("{value:00.050}"); + assertEquals("Float not recognized", 0.05f, jsonObject.getFloat("value"), 0.0f); + assertEquals("Float not recognized", 0.05f, jsonObject.optFloat("value"), 0.0f); + assertEquals("Float not recognized", 0.05f, jsonObject.optFloatObject("value"), 0.0f); + assertEquals("Double not recognized", 0.05d, jsonObject.optDouble("value"), 0.0f); + assertEquals("Double not recognized", 0.05d, jsonObject.optDoubleObject("value"), 0.0f); + assertEquals("Double not recognized", 0.05d, jsonObject.getDouble("value"), 0.0f); + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.05).compareTo(jsonObject.getBigDecimal("value"))); + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); + } + + @Test + public void shouldParseNegativeDecimalNumberThatHasZeroBeforeWithDecimalPoint(){ + JSONObject jsonObject = new JSONObject("{value:-00.050}"); + assertEquals("Float not recognized", -0.05f, jsonObject.getFloat("value"), 0.0f); + assertEquals("Float not recognized", -0.05f, jsonObject.optFloat("value"), 0.0f); + assertEquals("Float not recognized", -0.05f, jsonObject.optFloatObject("value"), 0.0f); + assertEquals("Double not recognized", -0.05d, jsonObject.optDouble("value"), 0.0f); + assertEquals("Double not recognized", -0.05d, jsonObject.optDoubleObject("value"), 0.0f); + assertEquals("Double not recognized", -0.05d, jsonObject.getDouble("value"), 0.0f); + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.05).compareTo(jsonObject.getBigDecimal("value"))); + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); + } + + +} diff --git a/src/test/java/org/json/junit/JSONObjectNumberTest.java b/src/test/java/org/json/junit/JSONObjectNumberTest.java index 43173a288f81eb60e5c586b487df3b5f85f2c7da..14e68d66b0f0369a0d3086a617de7991e76af7ff 100644 --- a/src/test/java/org/json/junit/JSONObjectNumberTest.java +++ b/src/test/java/org/json/junit/JSONObjectNumberTest.java @@ -23,7 +23,10 @@ public class JSONObjectNumberTest { @Parameters(name = "{index}: {0}") public static Collection data() { return Arrays.asList(new Object[][]{ - {"{value:50}", 1}, + {"{value:0050}", 1}, + {"{value:0050.0000}", 1}, + {"{value:-0050}", -1}, + {"{value:-0050.0000}", -1}, {"{value:50.0}", 1}, {"{value:5e1}", 1}, {"{value:5E1}", 1}, @@ -32,6 +35,7 @@ public class JSONObjectNumberTest { {"{value:-50}", -1}, {"{value:-50.0}", -1}, {"{value:-5e1}", -1}, + {"{value:-0005e1}", -1}, {"{value:-5E1}", -1}, {"{value:-5e1}", -1}, {"{value:'-50'}", -1} diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e869a8d5c760064f4ef126146e82829c38a7eac8..5adb6ce5163af84f6dd4cf0f471f5f66aca93763 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -4,11 +4,13 @@ package org.json.junit; Public Domain. */ +import static java.lang.Double.NaN; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -54,7 +56,6 @@ import org.json.junit.data.Singleton; import org.json.junit.data.SingletonEnum; import org.json.junit.data.WeirdList; import org.junit.Test; -import org.json.junit.Util; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; @@ -206,13 +207,17 @@ public class JSONObjectTest { */ @Test public void unquotedText() { - String str = "{key1:value1, key2:42}"; + String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}"; JSONObject jsonObject = new JSONObject(str); String textStr = jsonObject.toString(); assertTrue("expected key1", textStr.contains("\"key1\"")); assertTrue("expected value1", textStr.contains("\"value1\"")); assertTrue("expected key2", textStr.contains("\"key2\"")); assertTrue("expected 42", textStr.contains("42")); + assertTrue("expected 1.2", textStr.contains("\"1.2\"")); + assertTrue("expected 3.4", textStr.contains("3.4")); + assertTrue("expected -7E+5", textStr.contains("\"-7E+5\"")); + assertTrue("expected something!", textStr.contains("\"something!\"")); Util.checkJSONObjectMaps(jsonObject); } @@ -304,12 +309,12 @@ public class JSONObjectTest { @Test public void jsonObjectByMap() { Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); + map.put("trueKey", Boolean.valueOf(true)); + map.put("falseKey", Boolean.valueOf(false)); map.put("stringKey", "hello world!"); map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); + map.put("intKey", Long.valueOf(42)); + map.put("doubleKey", Double.valueOf(-23.45e67)); JSONObject jsonObject = new JSONObject(map); // validate JSON @@ -570,13 +575,13 @@ public class JSONObjectTest { @Test public void jsonObjectByMapWithNullValue() { Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); + map.put("trueKey", Boolean.valueOf(true)); + map.put("falseKey", Boolean.valueOf(false)); map.put("stringKey", "hello world!"); map.put("nullKey", null); map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); + map.put("intKey", Long.valueOf(42)); + map.put("doubleKey", Double.valueOf(-23.45e67)); JSONObject jsonObject = new JSONObject(map); // validate JSON @@ -626,9 +631,9 @@ public class JSONObjectTest { assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); // sorry, mockito artifact - assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); - assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); - assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); + assertTrue("expected 2 mockitoInterceptor items", ((Map)(JsonPath.read(doc, "$.mockitoInterceptor"))).size() == 2); + assertTrue("expected 0 mockitoInterceptor.serializationSupport items", + ((Map)(JsonPath.read(doc, "$.mockitoInterceptor.serializationSupport"))).size() == 0); Util.checkJSONObjectMaps(jsonObject); } @@ -778,7 +783,7 @@ public class JSONObjectTest { jsonObject.accumulate("myArray", -23.45e7); // include an unsupported object for coverage try { - jsonObject.accumulate("myArray", Double.NaN); + jsonObject.accumulate("myArray", NaN); fail("Expected exception"); } catch (JSONException ignored) {} @@ -810,7 +815,7 @@ public class JSONObjectTest { jsonObject.append("myArray", -23.45e7); // include an unsupported object for coverage try { - jsonObject.append("myArray", Double.NaN); + jsonObject.append("myArray", NaN); fail("Expected exception"); } catch (JSONException ignored) {} @@ -835,7 +840,7 @@ public class JSONObjectTest { public void jsonObjectDoubleToString() { String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, - Double.NaN, Double.NEGATIVE_INFINITY }; + NaN, Double.NEGATIVE_INFINITY }; for (int i = 0; i < expectedStrs.length; ++i) { String actualStr = JSONObject.doubleToString(doubles[i]); assertTrue("value expected ["+expectedStrs[i]+ @@ -890,11 +895,11 @@ public class JSONObjectTest { assertTrue("opt doubleKey should be double", jsonObject.optDouble("doubleKey") == -23.45e7); assertTrue("opt doubleKey with Default should be double", - jsonObject.optDouble("doubleStrKey", Double.NaN) == 1); + jsonObject.optDouble("doubleStrKey", NaN) == 1); assertTrue("opt doubleKey should be Double", Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey"))); assertTrue("opt doubleKey with Default should be Double", - Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", Double.NaN))); + Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", NaN))); assertTrue("opt negZeroKey should be a Double", jsonObject.opt("negZeroKey") instanceof Double); assertTrue("get negZeroKey should be a Double", @@ -996,7 +1001,7 @@ public class JSONObjectTest { assertTrue( "0.2 should be a BigDecimal!", JSONObject.stringToValue( "0.2" ) instanceof BigDecimal ); assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!", - JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof BigDecimal ); + JSONObject.stringToValue( Double.valueOf( "0.2f" ).toString() ) instanceof BigDecimal ); /** * This test documents a need for BigDecimal conversion. */ @@ -1006,13 +1011,13 @@ public class JSONObjectTest { assertTrue( "1 should be an Integer!", JSONObject.stringToValue( "1" ) instanceof Integer ); assertTrue( "Integer.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); + JSONObject.stringToValue( Integer.valueOf( Integer.MAX_VALUE ).toString() ) instanceof Integer ); assertTrue( "Large integers should be a Long!", JSONObject.stringToValue( Long.valueOf(((long)Integer.MAX_VALUE) + 1 ) .toString() ) instanceof Long ); assertTrue( "Long.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); + JSONObject.stringToValue( Long.valueOf( Long.MAX_VALUE ).toString() ) instanceof Long ); - String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); + String str = new BigInteger( Long.valueOf( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); assertTrue( "Really large integers currently evaluate to BigInteger", JSONObject.stringToValue(str).equals(new BigInteger("9223372036854775808"))); } @@ -1060,12 +1065,21 @@ public class JSONObjectTest { "\"tooManyZeros\":00,"+ "\"negativeInfinite\":-Infinity,"+ "\"negativeNaN\":-NaN,"+ + "\"negativeNaNWithLeadingZeros\":-00NaN,"+ "\"negativeFraction\":-.01,"+ "\"tooManyZerosFraction\":00.001,"+ "\"negativeHexFloat\":-0x1.fffp1,"+ "\"hexFloat\":0x1.0P-1074,"+ "\"floatIdentifier\":0.1f,"+ - "\"doubleIdentifier\":0.1d"+ + "\"doubleIdentifier\":0.1d,"+ + "\"doubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":0000000.1d,"+ + "\"negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":-0000000.1d,"+ + "\"doubleIdentifierWithMultipleLeadingZerosAfterDecimal\":0000000.0001d,"+ + "\"negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal\":-0000000.0001d,"+ + "\"integerWithLeadingZeros\":000900,"+ + "\"integerWithAllZeros\":00000,"+ + "\"compositeWithLeadingZeros\":00800.90d,"+ + "\"decimalPositiveWithoutNumberBeforeDecimalPoint\":.90,"+ "}"; JSONObject jsonObject = new JSONObject(str); Object obj; @@ -1075,17 +1089,24 @@ public class JSONObjectTest { assertTrue("hexNumber currently evaluates to string", obj.equals("-0x123")); assertTrue( "tooManyZeros currently evaluates to string", - jsonObject.get( "tooManyZeros" ).equals("00")); + jsonObject.get( "tooManyZeros" ).equals(0)); obj = jsonObject.get("negativeInfinite"); assertTrue( "negativeInfinite currently evaluates to string", obj.equals("-Infinity")); obj = jsonObject.get("negativeNaN"); assertTrue( "negativeNaN currently evaluates to string", obj.equals("-NaN")); + obj = jsonObject.get("negativeNaNWithLeadingZeros"); + assertTrue( "negativeNaNWithLeadingZeros currently evaluates to string", + obj.equals("-00NaN")); assertTrue( "negativeFraction currently evaluates to double -0.01", jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-0.01))); assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", jsonObject.get( "tooManyZerosFraction" ).equals(BigDecimal.valueOf(0.001))); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.getLong( "tooManyZerosFraction" )==0); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.optLong( "tooManyZerosFraction" )==0); assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", jsonObject.get( "negativeHexFloat" ).equals(Double.valueOf(-3.99951171875))); assertTrue("hexFloat currently evaluates to double 4.9E-324", @@ -1094,6 +1115,53 @@ public class JSONObjectTest { jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1))); assertTrue("doubleIdentifier currently evaluates to double 0.1", jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1))); + assertTrue("doubleIdentifierWithMultipleLeadingZerosBeforeDecimal currently evaluates to double 0.1", + jsonObject.get("doubleIdentifierWithMultipleLeadingZerosBeforeDecimal").equals(Double.valueOf(0.1))); + assertTrue("negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal currently evaluates to double -0.1", + jsonObject.get("negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal").equals(Double.valueOf(-0.1))); + assertTrue("doubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double 0.0001", + jsonObject.get("doubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(0.0001))); + assertTrue("doubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double 0.0001", + jsonObject.get("doubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(0.0001))); + assertTrue("negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double -0.0001", + jsonObject.get("negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(-0.0001))); + assertTrue("Integer does not evaluate to 900", + jsonObject.get("integerWithLeadingZeros").equals(900)); + assertTrue("Integer does not evaluate to 900", + jsonObject.getInt("integerWithLeadingZeros")==900); + assertTrue("Integer does not evaluate to 900", + jsonObject.optInt("integerWithLeadingZeros")==900); + assertTrue("Integer does not evaluate to 0", + jsonObject.get("integerWithAllZeros").equals(0)); + assertTrue("Integer does not evaluate to 0", + jsonObject.getInt("integerWithAllZeros")==0); + assertTrue("Integer does not evaluate to 0", + jsonObject.optInt("integerWithAllZeros")==0); + assertTrue("Double does not evaluate to 800.90", + jsonObject.get("compositeWithLeadingZeros").equals(800.90)); + assertTrue("Double does not evaluate to 800.90", + jsonObject.getDouble("compositeWithLeadingZeros")==800.9d); + assertTrue("Integer does not evaluate to 800", + jsonObject.optInt("compositeWithLeadingZeros")==800); + assertTrue("Long does not evaluate to 800.90", + jsonObject.getLong("compositeWithLeadingZeros")==800); + assertTrue("Long does not evaluate to 800.90", + jsonObject.optLong("compositeWithLeadingZeros")==800); + assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match", + 0.9d,jsonObject.getDouble("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d); + assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match", + 0.9d,jsonObject.optDouble("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d); + assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match", + 0.0d,jsonObject.optLong("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d); + + assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", + 0.0001d,jsonObject.getDouble("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d); + assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", + 0.0001d,jsonObject.optDouble("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d); + assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", + 0.0d, jsonObject.getLong("doubleIdentifierWithMultipleLeadingZerosAfterDecimal") , 0.0d); + assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", + 0.0d,jsonObject.optLong("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d); Util.checkJSONObjectMaps(jsonObject); } @@ -1259,8 +1327,8 @@ public class JSONObjectTest { String key30 = "key30"; String key31 = "key31"; JSONObject jsonObject = new JSONObject(); - jsonObject.put(key30, new Double(3.0)); - jsonObject.put(key31, new Double(3.1)); + jsonObject.put(key30, Double.valueOf(3.0)); + jsonObject.put(key31, Double.valueOf(3.1)); assertTrue("3.0 should remain a double", jsonObject.getDouble(key30) == 3); @@ -1713,19 +1781,19 @@ public class JSONObjectTest { */ assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); - Double d1 = new Double( 1.1f ); - Double d2 = new Double( "1.1f" ); + Double d1 = Double.valueOf( 1.1f ); + Double d2 = Double.valueOf( "1.1f" ); assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); - assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); + assertTrue( "Correctly converting float to double via base10 (string) representation!", Double.valueOf( 3.1d ).equals( Double.valueOf( Float.valueOf( 3.1f ).toString() ) ) ); // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject JSONObject jo = new JSONObject(); jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double - assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); + assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( Double.valueOf( 3.1d ) ) ); JSONObject inc = new JSONObject(); - inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) + inc.put( "bug", Float.valueOf( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! // this.put(key, (Float) value + 1); @@ -1969,7 +2037,7 @@ public class JSONObjectTest { @Test public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("abc", "def"); jsonObject.put("key", map); @@ -2040,14 +2108,14 @@ public class JSONObjectTest { assertTrue("map valueToString() incorrect", jsonObject.toString().equals(JSONObject.valueToString(map))); Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); + collection.add(Integer.valueOf(1)); + collection.add(Integer.valueOf(2)); + collection.add(Integer.valueOf(3)); assertTrue("collection valueToString() expected: "+ jsonArray.toString()+ " actual: "+ JSONObject.valueToString(collection), jsonArray.toString().equals(JSONObject.valueToString(collection))); - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }; assertTrue("array valueToString() incorrect", jsonArray.toString().equals(JSONObject.valueToString(array))); Util.checkJSONObjectMaps(jsonObject); @@ -2085,7 +2153,7 @@ public class JSONObjectTest { JSONObject.NULL == JSONObject.wrap(null)); // wrap(Integer) returns Integer - Integer in = new Integer(1); + Integer in = Integer.valueOf(1); assertTrue("Integer wrap() incorrect", in == JSONObject.wrap(in)); @@ -2112,9 +2180,9 @@ public class JSONObjectTest { // wrap collection returns JSONArray Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); + collection.add(Integer.valueOf(1)); + collection.add(Integer.valueOf(2)); + collection.add(Integer.valueOf(3)); JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); // validate JSON @@ -2125,7 +2193,7 @@ public class JSONObjectTest { assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); // wrap Array returns JSONArray - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }; JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); // validate JSON @@ -2225,6 +2293,42 @@ public class JSONObjectTest { "Expected a ',' or '}' at 15 [character 16 line 1]", e.getMessage()); } + try { + // key is a nested map + String str = "{{\"foo\": \"bar\"}: \"baz\"}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Missing value at 1 [character 2 line 1]", + e.getMessage()); + } + try { + // key is a nested array containing a map + String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Missing value at 9 [character 10 line 1]", + e.getMessage()); + } + try { + // key contains } + String str = "{foo}: 2}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 5 [character 6 line 1]", + e.getMessage()); + } + try { + // key contains ] + String str = "{foo]: 2}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 5 [character 6 line 1]", + e.getMessage()); + } try { // \0 after , String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; @@ -2288,7 +2392,7 @@ public class JSONObjectTest { } try { // test validity of invalid double - JSONObject.testValidity(Double.NaN); + JSONObject.testValidity(NaN); fail("Expected an exception"); } catch (JSONException e) { assertTrue("", true); @@ -2511,6 +2615,8 @@ public class JSONObjectTest { MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONArray() should return default JSONArray", + "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0))); assertTrue("optJSONObject() should return default JSONObject ", jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue")); assertTrue("optLong() should return default long", @@ -2556,6 +2662,8 @@ public class JSONObjectTest { Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42))); assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return default JSONArray", + "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); assertTrue("optJSONObject() should return default JSONObject ", @@ -3240,7 +3348,7 @@ public class JSONObjectTest { @SuppressWarnings("boxing") @Test public void testGenericBean() { - GenericBean bean = new GenericBean(42); + GenericBean bean = new GenericBean<>(42); final JSONObject jo = new JSONObject(bean); assertEquals(jo.keySet().toString(), 8, jo.length()); assertEquals(42, jo.get("genericValue")); @@ -3289,6 +3397,7 @@ public class JSONObjectTest { * Sample test case from https://github.com/stleary/JSON-java/issues/531 * which verifies that no regression in double/BigDecimal support is present. */ + @Test public void testObjectToBigDecimal() { double value = 1412078745.01074; Reader reader = new StringReader("[{\"value\": " + value + "}]"); @@ -3583,4 +3692,25 @@ public class JSONObjectTest { .put("b", 2); assertFalse(jo1.similar(jo3)); } + + private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN, + Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN }; + + @Test + public void issue713MapConstructorWithNonFiniteNumbers() { + for (Number nonFinite : NON_FINITE_NUMBERS) { + Map map = new HashMap<>(); + map.put("a", nonFinite); + + assertThrows(JSONException.class, () -> new JSONObject(map)); + } + } + + @Test + public void issue713BeanConstructorWithNonFiniteNumbers() { + for (Number nonFinite : NON_FINITE_NUMBERS) { + GenericBean bean = new GenericBean<>(nonFinite); + assertThrows(JSONException.class, () -> new JSONObject(bean)); + } + } } diff --git a/src/test/java/org/json/junit/JsonNumberZeroTest.java b/src/test/java/org/json/junit/JsonNumberZeroTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bfa4ca9d8bc3bc7a644e40ad41e0ea28a6f6e044 --- /dev/null +++ b/src/test/java/org/json/junit/JsonNumberZeroTest.java @@ -0,0 +1,55 @@ +package org.json.junit; + +import org.json.JSONObject; +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import static org.junit.Assert.assertEquals; + +public class JsonNumberZeroTest { + + @Test + public void shouldParseNegativeZeroValueWithMultipleZeroDigit(){ + JSONObject jsonObject = new JSONObject("{value:-0000}"); + assertEquals("Float not recognized", -0f, jsonObject.getFloat("value"), 0.0f); + assertEquals("Float not recognized", -0f, jsonObject.optFloat("value"), 0.0f); + assertEquals("Float not recognized", -0f, jsonObject.optFloatObject("value"), 0.0f); + assertEquals("Double not recognized", -0d, jsonObject.optDouble("value"), 0.0f); + assertEquals("Double not recognized", -0.0d, jsonObject.optDoubleObject("value"), 0.0f); + assertEquals("Double not recognized", -0.0d, jsonObject.getDouble("value"), 0.0f); + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-0).compareTo(jsonObject.getBigDecimal("value"))); + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); + } + + @Test + public void shouldParseZeroValueWithMultipleZeroDigit(){ + JSONObject jsonObject = new JSONObject("{value:0000}"); + assertEquals("Float not recognized", 0f, jsonObject.getFloat("value"), 0.0f); + assertEquals("Float not recognized", 0f, jsonObject.optFloat("value"), 0.0f); + assertEquals("Float not recognized", 0f, jsonObject.optFloatObject("value"), 0.0f); + assertEquals("Double not recognized", 0d, jsonObject.optDouble("value"), 0.0f); + assertEquals("Double not recognized", 0.0d, jsonObject.optDoubleObject("value"), 0.0f); + assertEquals("Double not recognized", 0.0d, jsonObject.getDouble("value"), 0.0f); + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-0).compareTo(jsonObject.getBigDecimal("value"))); + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); + } + +} diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index e940032e03c24cb4834636f91a57fb26ca4da7ef..22d6131cbae0ff1e3117bd0b01b420403c1f29ad 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1223,32 +1223,18 @@ public class XMLTest { @Test public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){ - try { - InputStream jsonStream = null; - try { - jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json"); - final JSONObject object = new JSONObject(new JSONTokener(jsonStream)); - String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS,2); - InputStream xmlStream = null; - try { - xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml"); - int bufferSize = 1024; - char[] buffer = new char[bufferSize]; - StringBuilder expected = new StringBuilder(); - Reader in = new InputStreamReader(xmlStream, "UTF-8"); - for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { - expected.append(buffer, 0, numRead); - } - assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); - } finally { - if (xmlStream != null) { - xmlStream.close(); - } - } - } finally { - if (jsonStream != null) { - jsonStream.close(); + try (InputStream jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json")) { + final JSONObject object = new JSONObject(new JSONTokener(jsonStream)); + String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS, 2); + try (InputStream xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml")) { + int bufferSize = 1024; + char[] buffer = new char[bufferSize]; + StringBuilder expected = new StringBuilder(); + Reader in = new InputStreamReader(xmlStream, "UTF-8"); + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { + expected.append(buffer, 0, numRead); } + assertEquals(expected.toString(), actualString); } } catch (IOException e) { fail("file writer error: " +e.getMessage()); diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java index da6370d48191d5ef8f827de00877d8ac57a92161..dd46b88e641056f01a7e573b757db1492e320830 100644 --- a/src/test/java/org/json/junit/data/GenericBean.java +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -9,7 +9,7 @@ import java.io.StringReader; * @param * generic number value */ -public class GenericBean> implements MyBean { +public class GenericBean implements MyBean { /** * @param genericValue * value to initiate with