mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-05 00:00:52 -05:00
Fix sonarqube reliability issues
This commit is contained in:
@@ -9,6 +9,7 @@ import java.io.StringReader;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides static methods to convert an XML text into a JSONObject, and to
|
* This provides static methods to convert an XML text into a JSONObject, and to
|
||||||
@@ -80,7 +81,7 @@ public class XML {
|
|||||||
public Iterator<Integer> iterator() {
|
public Iterator<Integer> iterator() {
|
||||||
return new Iterator<Integer>() {
|
return new Iterator<Integer>() {
|
||||||
private int nextIndex = 0;
|
private int nextIndex = 0;
|
||||||
private int length = string.length();
|
private final int length = string.length();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
@@ -89,6 +90,9 @@ public class XML {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer next() {
|
public Integer next() {
|
||||||
|
if (!hasNext()) {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
int result = string.codePointAt(this.nextIndex);
|
int result = string.codePointAt(this.nextIndex);
|
||||||
this.nextIndex += Character.charCount(result);
|
this.nextIndex += Character.charCount(result);
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -3117,12 +3117,13 @@ public class JSONObjectTest {
|
|||||||
|
|
||||||
// test a more complex object
|
// test a more complex object
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
try {
|
|
||||||
new JSONObject()
|
JSONObject object = new JSONObject()
|
||||||
.put("somethingElse", "a value")
|
.put("somethingElse", "a value")
|
||||||
.put("someKey", new JSONArray()
|
.put("someKey", new JSONArray()
|
||||||
.put(new JSONObject().put("key1", new BrokenToString())))
|
.put(new JSONObject().put("key1", new BrokenToString())));
|
||||||
.write(writer).toString();
|
try {
|
||||||
|
object.write(writer).toString();
|
||||||
fail("Expected an exception, got a String value");
|
fail("Expected an exception, got a String value");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
||||||
@@ -3133,17 +3134,18 @@ public class JSONObjectTest {
|
|||||||
writer.close();
|
writer.close();
|
||||||
} catch (Exception e) {}
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test a more slightly complex object
|
// test a more slightly complex object
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
try {
|
|
||||||
new JSONObject()
|
object = new JSONObject()
|
||||||
.put("somethingElse", "a value")
|
.put("somethingElse", "a value")
|
||||||
.put("someKey", new JSONArray()
|
.put("someKey", new JSONArray()
|
||||||
.put(new JSONObject().put("key1", new BrokenToString()))
|
.put(new JSONObject().put("key1", new BrokenToString()))
|
||||||
.put(12345)
|
.put(12345)
|
||||||
)
|
);
|
||||||
.write(writer).toString();
|
try {
|
||||||
|
object.write(writer).toString();
|
||||||
fail("Expected an exception, got a String value");
|
fail("Expected an exception, got a String value");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user