mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-24 00:03:17 -05:00
Compare commits
60 Commits
pre-releas
...
20231013
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f346203cd6 | ||
|
|
b180dbedbc | ||
|
|
cca6d1020f | ||
|
|
af5f780d5b | ||
|
|
495cec9037 | ||
|
|
56cb5f84c4 | ||
|
|
0cdc38ac24 | ||
|
|
d5277b126b | ||
|
|
c4cd526c53 | ||
|
|
776b5ccb85 | ||
|
|
fb99c06bad | ||
|
|
bc09f90e90 | ||
|
|
c93014cb53 | ||
|
|
0e4a94d91d | ||
|
|
1a38879c90 | ||
|
|
4c8cac22a8 | ||
|
|
fe45fa9cfb | ||
|
|
79af389f7a | ||
|
|
1726b6cf55 | ||
|
|
beb2fb5706 | ||
|
|
ff921db783 | ||
|
|
61bb60e752 | ||
|
|
ef68cdf810 | ||
|
|
eaa5611ba3 | ||
|
|
dbb113176b | ||
|
|
16967f322e | ||
|
|
284a316838 | ||
|
|
4e8231c512 | ||
|
|
db0fde2a56 | ||
|
|
661114c50d | ||
|
|
3e688afc66 | ||
|
|
01727fd0ed | ||
|
|
74cd73f97c | ||
|
|
c29d4881e0 | ||
|
|
7c1b6531e7 | ||
|
|
db122e5d3a | ||
|
|
a309931d20 | ||
|
|
e27da22e05 | ||
|
|
af6d07cecb | ||
|
|
64093366b3 | ||
|
|
9b69ec49ad | ||
|
|
2c674be1b6 | ||
|
|
be33deb7d5 | ||
|
|
48089a4da7 | ||
|
|
a4e152f4f0 | ||
|
|
3dd8f2ecd5 | ||
|
|
bae0b0dac9 | ||
|
|
e563dbcaaa | ||
|
|
50dfcc59b3 | ||
|
|
b2943eb395 | ||
|
|
60662e2f83 | ||
|
|
2a4bc3420a | ||
|
|
b6ff0db984 | ||
|
|
c8a9e15a57 | ||
|
|
402db6ad84 | ||
|
|
4951ec48c8 | ||
|
|
8ce0019a5d | ||
|
|
3d524349a1 | ||
|
|
a963115ac2 | ||
|
|
f959baa3cb |
6
.github/workflows/codeql-analysis.yml
vendored
6
.github/workflows/codeql-analysis.yml
vendored
@@ -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
|
||||
|
||||
37
.github/workflows/pipeline.yml
vendored
37
.github/workflows/pipeline.yml
vendored
@@ -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 }}
|
||||
|
||||
@@ -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?
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ JSON in Java [package org.json]
|
||||
|
||||
[](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
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
30
pom.xml
30
pom.xml
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20230618</version>
|
||||
<version>20231013</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<name>JSON in Java</name>
|
||||
@@ -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.
|
||||
</description>
|
||||
@@ -57,7 +57,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -69,7 +69,7 @@
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>1.9.5</version>
|
||||
<version>4.2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -79,7 +79,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<version>5.1.9</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
@@ -93,16 +93,16 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.1.2</version>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
@@ -115,7 +115,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.7</version>
|
||||
<version>3.5.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
@@ -131,7 +131,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
@@ -139,6 +139,12 @@
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<gpgArguments>
|
||||
<arg>--pinentry-mode</arg>
|
||||
<arg>loopback</arg>
|
||||
</gpgArguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
@@ -156,7 +162,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
|
||||
@@ -599,6 +599,38 @@ public class JSONArray implements Iterable<Object> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Boolean object associated with an index. It returns false
|
||||
* if there is no value at that index, or if the value is not Boolean.TRUE
|
||||
* or the String "true".
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The truth.
|
||||
*/
|
||||
public Boolean optBooleanObject(int index) {
|
||||
return this.optBooleanObject(index, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Boolean object associated with an index. It returns the
|
||||
* defaultValue if there is no value at that index or if it is not a Boolean
|
||||
* or the String "true" or "false" (case insensitive).
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* A boolean default.
|
||||
* @return The truth.
|
||||
*/
|
||||
public Boolean optBooleanObject(int index, Boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional double value associated with an index. NaN is returned
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
@@ -635,6 +667,42 @@ public class JSONArray implements Iterable<Object> {
|
||||
return doubleValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Double object associated with an index. NaN is returned
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The object.
|
||||
*/
|
||||
public Double optDoubleObject(int index) {
|
||||
return this.optDoubleObject(index, Double.NaN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional double value associated with an index. The defaultValue
|
||||
* is returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* subscript
|
||||
* @param defaultValue
|
||||
* The default object.
|
||||
* @return The object.
|
||||
*/
|
||||
public Double optDoubleObject(int index, Double defaultValue) {
|
||||
final Number val = this.optNumber(index, null);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
final Double doubleValue = val.doubleValue();
|
||||
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
return doubleValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional float value associated with an index. NaN is returned
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
@@ -671,6 +739,42 @@ public class JSONArray implements Iterable<Object> {
|
||||
return floatValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Float object associated with an index. NaN is returned
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The object.
|
||||
*/
|
||||
public Float optFloatObject(int index) {
|
||||
return this.optFloatObject(index, Float.NaN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Float object associated with an index. The defaultValue
|
||||
* is returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* subscript
|
||||
* @param defaultValue
|
||||
* The default object.
|
||||
* @return The object.
|
||||
*/
|
||||
public Float optFloatObject(int index, Float defaultValue) {
|
||||
final Number val = this.optNumber(index, null);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
final Float floatValue = val.floatValue();
|
||||
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
|
||||
// return floatValue;
|
||||
// }
|
||||
return floatValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional int value associated with an index. Zero is returned if
|
||||
* there is no value for the index, or if the value is not a number and
|
||||
@@ -703,6 +807,38 @@ public class JSONArray implements Iterable<Object> {
|
||||
return val.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Integer object associated with an index. Zero is returned if
|
||||
* there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The object.
|
||||
*/
|
||||
public Integer optIntegerObject(int index) {
|
||||
return this.optIntegerObject(index, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Integer object associated with an index. The defaultValue is
|
||||
* returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default object.
|
||||
* @return The object.
|
||||
*/
|
||||
public Integer optIntegerObject(int index, Integer defaultValue) {
|
||||
final Number val = this.optNumber(index, null);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return val.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the enum value associated with a key.
|
||||
*
|
||||
@@ -788,30 +924,57 @@ public class JSONArray implements Iterable<Object> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -846,6 +1009,38 @@ public class JSONArray implements Iterable<Object> {
|
||||
return val.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Long object associated with an index. Zero is returned if
|
||||
* there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The object.
|
||||
*/
|
||||
public Long optLongObject(int index) {
|
||||
return this.optLongObject(index, 0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Long object associated with an index. The defaultValue is
|
||||
* returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default object.
|
||||
* @return The object.
|
||||
*/
|
||||
public Long optLongObject(int index, Long defaultValue) {
|
||||
final Number val = this.optNumber(index, null);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return val.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional {@link Number} value associated with a key, or <code>null</code>
|
||||
* if there is no such key or if the value is not a number. If the value is a string,
|
||||
@@ -1451,9 +1646,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
@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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 ':'.
|
||||
@@ -256,6 +248,9 @@ public class JSONObject {
|
||||
if (x.nextClean() == '}') {
|
||||
return;
|
||||
}
|
||||
if (x.end()) {
|
||||
throw x.syntaxError("A JSONObject text must end with '}'");
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case '}':
|
||||
@@ -288,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));
|
||||
}
|
||||
}
|
||||
@@ -351,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();
|
||||
@@ -1131,6 +1129,45 @@ public class JSONObject {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional boolean object associated with a key. It returns false if there
|
||||
* is no such key, or if the value is not Boolean.TRUE or the String "true".
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @return The truth.
|
||||
*/
|
||||
public Boolean optBooleanObject(String key) {
|
||||
return this.optBooleanObject(key, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional boolean object associated with a key. It returns the
|
||||
* defaultValue if there is no such key, or if it is not a Boolean or the
|
||||
* String "true" or "false" (case insensitive).
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param defaultValue
|
||||
* The default.
|
||||
* @return The truth.
|
||||
*/
|
||||
public Boolean optBooleanObject(String key, Boolean defaultValue) {
|
||||
Object val = this.opt(key);
|
||||
if (NULL.equals(val)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (val instanceof Boolean){
|
||||
return ((Boolean) val).booleanValue();
|
||||
}
|
||||
try {
|
||||
// we'll use the get anyway because it does string conversion.
|
||||
return this.getBoolean(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional BigDecimal associated with a key, or the defaultValue if
|
||||
* there is no such key or if its value is not a number. If the value is a
|
||||
@@ -1294,7 +1331,39 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional double value associated with an index. NaN is returned
|
||||
* Get an optional Double object associated with a key, or NaN if there is no such
|
||||
* key or if its value is not a number. If the value is a string, an attempt
|
||||
* will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A string which is the key.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public Double optDoubleObject(String key) {
|
||||
return this.optDoubleObject(key, Double.NaN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional Double object associated with a key, or the defaultValue if
|
||||
* there is no such key or if its value is not a number. If the value is a
|
||||
* string, an attempt will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param defaultValue
|
||||
* The default.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public Double optDoubleObject(String key, Double defaultValue) {
|
||||
Number val = this.optNumber(key);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return val.doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional float value associated with an index. NaN is returned
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
@@ -1307,7 +1376,7 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional double value associated with an index. The defaultValue
|
||||
* Get the optional float value associated with an index. The defaultValue
|
||||
* is returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
@@ -1329,6 +1398,42 @@ public class JSONObject {
|
||||
return floatValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Float object associated with an index. NaN is returned
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @return The object.
|
||||
*/
|
||||
public Float optFloatObject(String key) {
|
||||
return this.optFloatObject(key, Float.NaN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional Float object associated with an index. The defaultValue
|
||||
* is returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param defaultValue
|
||||
* The default object.
|
||||
* @return The object.
|
||||
*/
|
||||
public Float optFloatObject(String key, Float defaultValue) {
|
||||
Number val = this.optNumber(key);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
final Float floatValue = val.floatValue();
|
||||
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
return floatValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional int value associated with a key, or zero if there is no
|
||||
* such key or if the value is not a number. If the value is a string, an
|
||||
@@ -1361,6 +1466,38 @@ public class JSONObject {
|
||||
return val.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional Integer object associated with a key, or zero if there is no
|
||||
* such key or if the value is not a number. If the value is a string, an
|
||||
* attempt will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public Integer optIntegerObject(String key) {
|
||||
return this.optIntegerObject(key, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional Integer object associated with a key, or the default if there
|
||||
* is no such key or if the value is not a number. If the value is a string,
|
||||
* an attempt will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param defaultValue
|
||||
* The default.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public Integer optIntegerObject(String key, Integer defaultValue) {
|
||||
final Number val = this.optNumber(key, null);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return val.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional JSONArray associated with a key. It returns null if there
|
||||
* is no such key, or if its value is not a JSONArray.
|
||||
@@ -1370,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1432,6 +1583,39 @@ public class JSONObject {
|
||||
return val.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional Long object associated with a key, or zero if there is no
|
||||
* such key or if the value is not a number. If the value is a string, an
|
||||
* attempt will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public Long optLongObject(String key) {
|
||||
return this.optLongObject(key, 0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional Long object associated with a key, or the default if there
|
||||
* is no such key or if the value is not a number. If the value is a string,
|
||||
* an attempt will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param defaultValue
|
||||
* The default.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public Long optLongObject(String key, Long defaultValue) {
|
||||
final Number val = this.optNumber(key, null);
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return val.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional {@link Number} value associated with a key, or <code>null</code>
|
||||
* if there is no such key or if the value is not a number. If the value is a string,
|
||||
@@ -1510,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<Object, Boolean>()));
|
||||
@@ -1537,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();
|
||||
@@ -2002,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 "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2200,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
|
||||
@@ -2224,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.
|
||||
@@ -2262,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.");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2298,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) {
|
||||
@@ -2308,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.
|
||||
*
|
||||
@@ -2395,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();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2709,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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Object> 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,
|
||||
@@ -537,43 +551,75 @@ public class JSONArrayTest {
|
||||
assertTrue("Array opt boolean implicit default",
|
||||
Boolean.FALSE == jsonArray.optBoolean(-1));
|
||||
|
||||
assertTrue("Array opt boolean object",
|
||||
Boolean.TRUE.equals(jsonArray.optBooleanObject(0)));
|
||||
assertTrue("Array opt boolean object default",
|
||||
Boolean.FALSE.equals(jsonArray.optBooleanObject(-1, Boolean.FALSE)));
|
||||
assertTrue("Array opt boolean object implicit default",
|
||||
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)));
|
||||
assertTrue("Array opt double object default",
|
||||
Double.valueOf(1).equals(jsonArray.optDoubleObject(0, 1D)));
|
||||
assertTrue("Array opt double object default implicit",
|
||||
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)));
|
||||
assertTrue("Array opt float object default",
|
||||
Float.valueOf(1).equals(jsonArray.optFloatObject(0, 1F)));
|
||||
assertTrue("Array opt float object default implicit",
|
||||
jsonArray.optFloatObject(99).isNaN());
|
||||
|
||||
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));
|
||||
|
||||
assertTrue("Array opt int object",
|
||||
Integer.valueOf(42).equals(jsonArray.optIntegerObject(7)));
|
||||
assertTrue("Array opt int object default",
|
||||
Integer.valueOf(-1).equals(jsonArray.optIntegerObject(0, -1)));
|
||||
assertTrue("Array opt int object default implicit",
|
||||
Integer.valueOf(0).equals(jsonArray.optIntegerObject(0)));
|
||||
|
||||
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));
|
||||
@@ -582,6 +628,13 @@ public class JSONArrayTest {
|
||||
assertTrue("Array opt long default implicit",
|
||||
0 == jsonArray.optLong(-1));
|
||||
|
||||
assertTrue("Array opt long object",
|
||||
Long.valueOf(0).equals(jsonArray.optLongObject(11)));
|
||||
assertTrue("Array opt long object default",
|
||||
Long.valueOf(-2).equals(jsonArray.optLongObject(-1, -2L)));
|
||||
assertTrue("Array opt long object default implicit",
|
||||
Long.valueOf(0).equals(jsonArray.optLongObject(-1)));
|
||||
|
||||
assertTrue("Array opt string",
|
||||
"hello".equals(jsonArray.optString(4)));
|
||||
assertTrue("Array opt string default implicit",
|
||||
@@ -599,10 +652,15 @@ public class JSONArrayTest {
|
||||
public void optStringConversion(){
|
||||
JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]");
|
||||
assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true);
|
||||
assertTrue("unexpected optBooleanObject value",Boolean.valueOf(true).equals(ja.optBooleanObject(1,false)));
|
||||
assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false);
|
||||
assertTrue("unexpected optBooleanObject value",Boolean.valueOf(false).equals(ja.optBooleanObject(2,true)));
|
||||
assertTrue("unexpected optInt value",ja.optInt(0,0)==123);
|
||||
assertTrue("unexpected optIntegerObject value",Integer.valueOf(123).equals(ja.optIntegerObject(0,0)));
|
||||
assertTrue("unexpected optLong value",ja.optLong(0,0)==123);
|
||||
assertTrue("unexpected optLongObject value",Long.valueOf(123).equals(ja.optLongObject(0,0L)));
|
||||
assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0);
|
||||
assertTrue("unexpected optDoubleObject value",Double.valueOf(123.0).equals(ja.optDoubleObject(0,0.0)));
|
||||
assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
|
||||
assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
|
||||
Util.checkJSONArrayMaps(ja);
|
||||
@@ -971,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);
|
||||
@@ -985,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<JSONArray>(Arrays.asList(
|
||||
jsonArray, nestedJsonArray
|
||||
@@ -1329,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);
|
||||
|
||||
100
src/test/java/org/json/junit/JSONObjectDecimalTest.java
Normal file
100
src/test/java/org/json/junit/JSONObjectDecimalTest.java
Normal file
@@ -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")));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -23,7 +23,10 @@ public class JSONObjectNumberTest {
|
||||
@Parameters(name = "{index}: {0}")
|
||||
public static Collection<Object[]> 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}
|
||||
@@ -109,18 +113,38 @@ public class JSONObjectNumberTest {
|
||||
assertEquals(value.floatValue(), object.optFloat("value"), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptFloatObject() {
|
||||
assertEquals((Float) value.floatValue(), object.optFloatObject("value"), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptDouble() {
|
||||
assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptDoubleObject() {
|
||||
assertEquals((Double) value.doubleValue(), object.optDoubleObject("value"), 0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptInt() {
|
||||
assertEquals(value.intValue(), object.optInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptIntegerObject() {
|
||||
assertEquals((Integer) value.intValue(), object.optIntegerObject("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptLong() {
|
||||
assertEquals(value.longValue(), object.optLong("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptLongObject() {
|
||||
assertEquals((Long) value.longValue(), object.optLongObject("value"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -231,6 +236,11 @@ public class JSONObjectTest {
|
||||
assert 26315000000253009L == actualLong : "Incorrect key value. Got "
|
||||
+ actualLong + " expected " + str;
|
||||
|
||||
final Long actualLongObject = json.optLongObject("key");
|
||||
assert actualLongObject != 0L : "Unable to extract Long value for string " + str;
|
||||
assert Long.valueOf(26315000000253009L).equals(actualLongObject) : "Incorrect key value. Got "
|
||||
+ actualLongObject + " expected " + str;
|
||||
|
||||
final String actualString = json.optString("key");
|
||||
assert str.equals(actualString) : "Incorrect key value. Got "
|
||||
+ actualString + " expected " + str;
|
||||
@@ -299,12 +309,12 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void jsonObjectByMap() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
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
|
||||
@@ -565,13 +575,13 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void jsonObjectByMapWithNullValue() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
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
|
||||
@@ -621,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);
|
||||
}
|
||||
|
||||
@@ -773,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) {}
|
||||
|
||||
@@ -805,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) {}
|
||||
|
||||
@@ -830,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]+
|
||||
@@ -866,9 +876,11 @@ public class JSONObjectTest {
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey"));
|
||||
assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey"));
|
||||
assertTrue("opt trueKey should be true", jsonObject.optBooleanObject("trueKey"));
|
||||
assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey"));
|
||||
assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey"));
|
||||
assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey"));
|
||||
assertTrue("trueStrKey should be true", jsonObject.optBooleanObject("trueStrKey"));
|
||||
assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey"));
|
||||
assertTrue("stringKey should be string",
|
||||
jsonObject.getString("stringKey").equals("hello world!"));
|
||||
@@ -883,7 +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", NaN)));
|
||||
assertTrue("opt negZeroKey should be a Double",
|
||||
jsonObject.opt("negZeroKey") instanceof Double);
|
||||
assertTrue("get negZeroKey should be a Double",
|
||||
@@ -896,6 +912,10 @@ public class JSONObjectTest {
|
||||
Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0);
|
||||
assertTrue("opt negZeroStrKey with Default should be double",
|
||||
Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
|
||||
assertTrue("opt negZeroKey should be Double",
|
||||
Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroKey")));
|
||||
assertTrue("opt negZeroStrKey with Default should be Double",
|
||||
Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroStrKey")));
|
||||
assertTrue("optNumber negZeroKey should be -0.0",
|
||||
Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0);
|
||||
assertTrue("optNumber negZeroStrKey should be -0.0",
|
||||
@@ -904,10 +924,18 @@ public class JSONObjectTest {
|
||||
jsonObject.optFloat("doubleKey") == -23.45e7f);
|
||||
assertTrue("optFloat doubleKey with Default should be float",
|
||||
jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f);
|
||||
assertTrue("optFloat doubleKey should be Float",
|
||||
Float.valueOf(-23.45e7f).equals(jsonObject.optFloatObject("doubleKey")));
|
||||
assertTrue("optFloat doubleKey with Default should be Float",
|
||||
Float.valueOf(1f).equals(jsonObject.optFloatObject("doubleStrKey", Float.NaN)));
|
||||
assertTrue("intKey should be int",
|
||||
jsonObject.optInt("intKey") == 42);
|
||||
assertTrue("opt intKey should be int",
|
||||
jsonObject.optInt("intKey", 0) == 42);
|
||||
assertTrue("intKey should be Integer",
|
||||
Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey")));
|
||||
assertTrue("opt intKey should be Integer",
|
||||
Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey", 0)));
|
||||
assertTrue("opt intKey with default should be int",
|
||||
jsonObject.getInt("intKey") == 42);
|
||||
assertTrue("intStrKey should be int",
|
||||
@@ -918,6 +946,10 @@ public class JSONObjectTest {
|
||||
jsonObject.optLong("longKey") == 1234567890123456789L);
|
||||
assertTrue("opt longKey with default should be long",
|
||||
jsonObject.optLong("longKey", 0) == 1234567890123456789L);
|
||||
assertTrue("opt longKey should be Long",
|
||||
Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey")));
|
||||
assertTrue("opt longKey with default should be Long",
|
||||
Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey", 0L)));
|
||||
assertTrue("longStrKey should be long",
|
||||
jsonObject.getLong("longStrKey") == 987654321098765432L);
|
||||
assertTrue("optNumber int should return Integer",
|
||||
@@ -969,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.
|
||||
*/
|
||||
@@ -979,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")));
|
||||
}
|
||||
@@ -1033,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;
|
||||
@@ -1048,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",
|
||||
@@ -1067,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);
|
||||
}
|
||||
|
||||
@@ -1232,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);
|
||||
@@ -1686,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);
|
||||
@@ -1942,7 +2037,7 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void jsonObjectToStringSuppressWarningOnCastToMap() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
Map<String, String> map = new HashMap();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("abc", "def");
|
||||
jsonObject.put("key", map);
|
||||
|
||||
@@ -2013,14 +2108,14 @@ public class JSONObjectTest {
|
||||
assertTrue("map valueToString() incorrect",
|
||||
jsonObject.toString().equals(JSONObject.valueToString(map)));
|
||||
Collection<Integer> collection = new ArrayList<Integer>();
|
||||
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);
|
||||
@@ -2058,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));
|
||||
|
||||
@@ -2085,9 +2180,9 @@ public class JSONObjectTest {
|
||||
|
||||
// wrap collection returns JSONArray
|
||||
Collection<Integer> collection = new ArrayList<Integer>();
|
||||
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
|
||||
@@ -2098,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
|
||||
@@ -2198,6 +2293,51 @@ 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}";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"A JSONObject text must end with '}' at 15 [character 16 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// append to wrong key
|
||||
String str = "{\"myKey\":true, \"myOtherKey\":false}";
|
||||
@@ -2252,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);
|
||||
@@ -2465,20 +2605,32 @@ public class JSONObjectTest {
|
||||
BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0);
|
||||
assertTrue("optBoolean() should return default boolean",
|
||||
jsonObject.optBoolean("myKey", true));
|
||||
assertTrue("optBooleanObject() should return default Boolean",
|
||||
jsonObject.optBooleanObject("myKey", true));
|
||||
assertTrue("optInt() should return default int",
|
||||
42 == jsonObject.optInt("myKey", 42));
|
||||
assertTrue("optIntegerObject() should return default Integer",
|
||||
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 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",
|
||||
42l == jsonObject.optLong("myKey", 42l));
|
||||
assertTrue("optLongObject() should return default Long",
|
||||
Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l)));
|
||||
assertTrue("optDouble() should return default double",
|
||||
42.3d == jsonObject.optDouble("myKey", 42.3d));
|
||||
assertTrue("optDoubleObject() should return default Double",
|
||||
Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d)));
|
||||
assertTrue("optFloat() should return default float",
|
||||
42.3f == jsonObject.optFloat("myKey", 42.3f));
|
||||
assertTrue("optFloatObject() should return default Float",
|
||||
Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f)));
|
||||
assertTrue("optNumber() should return default Number",
|
||||
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
|
||||
assertTrue("optString() should return default string",
|
||||
@@ -2502,20 +2654,32 @@ public class JSONObjectTest {
|
||||
BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0);
|
||||
assertTrue("optBoolean() should return default boolean",
|
||||
jsonObject.optBoolean("myKey", true));
|
||||
assertTrue("optBooleanObject() should return default Boolean",
|
||||
jsonObject.optBooleanObject("myKey", true));
|
||||
assertTrue("optInt() should return default int",
|
||||
42 == jsonObject.optInt("myKey", 42));
|
||||
assertTrue("optIntegerObject() should return default Integer",
|
||||
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 ",
|
||||
jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue"));
|
||||
assertTrue("optLong() should return default long",
|
||||
42l == jsonObject.optLong("myKey", 42l));
|
||||
assertTrue("optLongObject() should return default Long",
|
||||
Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l)));
|
||||
assertTrue("optDouble() should return default double",
|
||||
42.3d == jsonObject.optDouble("myKey", 42.3d));
|
||||
assertTrue("optDoubleObject() should return default Double",
|
||||
Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d)));
|
||||
assertTrue("optFloat() should return default float",
|
||||
42.3f == jsonObject.optFloat("myKey", 42.3f));
|
||||
assertTrue("optFloatObject() should return default Float",
|
||||
Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f)));
|
||||
assertTrue("optNumber() should return default Number",
|
||||
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
|
||||
assertTrue("optString() should return default string",
|
||||
@@ -2530,11 +2694,17 @@ public class JSONObjectTest {
|
||||
public void jsonObjectOptStringConversion() {
|
||||
JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}");
|
||||
assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true);
|
||||
assertTrue("unexpected optBooleanObject value",Boolean.valueOf(true).equals(jo.optBooleanObject("true",false)));
|
||||
assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false);
|
||||
assertTrue("unexpected optBooleanObject value",Boolean.valueOf(false).equals(jo.optBooleanObject("false",true)));
|
||||
assertTrue("unexpected optInt value",jo.optInt("int",0)==123);
|
||||
assertTrue("unexpected optIntegerObject value",Integer.valueOf(123).equals(jo.optIntegerObject("int",0)));
|
||||
assertTrue("unexpected optLong value",jo.optLong("int",0)==123l);
|
||||
assertTrue("unexpected optLongObject value",Long.valueOf(123l).equals(jo.optLongObject("int",0L)));
|
||||
assertTrue("unexpected optDouble value",jo.optDouble("int",0.0d)==123.0d);
|
||||
assertTrue("unexpected optDoubleObject value",Double.valueOf(123.0d).equals(jo.optDoubleObject("int",0.0d)));
|
||||
assertTrue("unexpected optFloat value",jo.optFloat("int",0.0f)==123.0f);
|
||||
assertTrue("unexpected optFloatObject value",Float.valueOf(123.0f).equals(jo.optFloatObject("int",0.0f)));
|
||||
assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
|
||||
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
|
||||
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
|
||||
@@ -2555,23 +2725,35 @@ public class JSONObjectTest {
|
||||
assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumber",null));
|
||||
assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null));
|
||||
assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0);
|
||||
assertEquals(1.9007199254740992E16, jo.optDoubleObject("largeNumber"),0.0);
|
||||
assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f);
|
||||
assertEquals(1.90071995E16f, jo.optFloatObject("largeNumber"),0.0f);
|
||||
assertEquals(19007199254740993l, jo.optLong("largeNumber"));
|
||||
assertEquals(Long.valueOf(19007199254740993l), jo.optLongObject("largeNumber"));
|
||||
assertEquals(1874919425, jo.optInt("largeNumber"));
|
||||
assertEquals(Integer.valueOf(1874919425), jo.optIntegerObject("largeNumber"));
|
||||
|
||||
// conversion from a string
|
||||
assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumberStr",null));
|
||||
assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumberStr",null));
|
||||
assertEquals(1.9007199254740992E16, jo.optDouble("largeNumberStr"),0.0);
|
||||
assertEquals(1.9007199254740992E16, jo.optDoubleObject("largeNumberStr"),0.0);
|
||||
assertEquals(1.90071995E16f, jo.optFloat("largeNumberStr"),0.0f);
|
||||
assertEquals(1.90071995E16f, jo.optFloatObject("largeNumberStr"),0.0f);
|
||||
assertEquals(19007199254740993l, jo.optLong("largeNumberStr"));
|
||||
assertEquals(Long.valueOf(19007199254740993l), jo.optLongObject("largeNumberStr"));
|
||||
assertEquals(1874919425, jo.optInt("largeNumberStr"));
|
||||
assertEquals(Integer.valueOf(1874919425), jo.optIntegerObject("largeNumberStr"));
|
||||
|
||||
// the integer portion of the actual value is larger than a double can hold.
|
||||
assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber"));
|
||||
assertNotEquals(Long.valueOf((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optLongObject("largeNumber"));
|
||||
assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumber"));
|
||||
assertNotEquals(Integer.valueOf((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optIntegerObject("largeNumber"));
|
||||
assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumberStr"));
|
||||
assertNotEquals(Long.valueOf((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optLongObject("largeNumberStr"));
|
||||
assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumberStr"));
|
||||
assertNotEquals(Integer.valueOf((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optIntegerObject("largeNumberStr"));
|
||||
assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
|
||||
assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
|
||||
Util.checkJSONObjectMaps(jo);
|
||||
@@ -3166,7 +3348,7 @@ public class JSONObjectTest {
|
||||
@SuppressWarnings("boxing")
|
||||
@Test
|
||||
public void testGenericBean() {
|
||||
GenericBean<Integer> bean = new GenericBean(42);
|
||||
GenericBean<Integer> bean = new GenericBean<>(42);
|
||||
final JSONObject jo = new JSONObject(bean);
|
||||
assertEquals(jo.keySet().toString(), 8, jo.length());
|
||||
assertEquals(42, jo.get("genericValue"));
|
||||
@@ -3215,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 + "}]");
|
||||
@@ -3509,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<String, Number> map = new HashMap<>();
|
||||
map.put("a", nonFinite);
|
||||
|
||||
assertThrows(JSONException.class, () -> new JSONObject(map));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issue713BeanConstructorWithNonFiniteNumbers() {
|
||||
for (Number nonFinite : NON_FINITE_NUMBERS) {
|
||||
GenericBean<Number> bean = new GenericBean<>(nonFinite);
|
||||
assertThrows(JSONException.class, () -> new JSONObject(bean));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
55
src/test/java/org/json/junit/JsonNumberZeroTest.java
Normal file
55
src/test/java/org/json/junit/JsonNumberZeroTest.java
Normal file
@@ -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")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -915,7 +916,7 @@ public class XMLTest {
|
||||
InputStream xmlStream = null;
|
||||
try {
|
||||
xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue537.xml");
|
||||
Reader xmlReader = new InputStreamReader(xmlStream);
|
||||
Reader xmlReader = new InputStreamReader(xmlStream, Charset.forName("UTF-8"));
|
||||
JSONObject actual = XML.toJSONObject(xmlReader, true);
|
||||
InputStream jsonStream = null;
|
||||
try {
|
||||
@@ -1222,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());
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.io.StringReader;
|
||||
* @param <T>
|
||||
* generic number value
|
||||
*/
|
||||
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
|
||||
public class GenericBean<T extends Number> implements MyBean {
|
||||
/**
|
||||
* @param genericValue
|
||||
* value to initiate with
|
||||
|
||||
Reference in New Issue
Block a user