Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/org/json/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* This provides static methods to convert an XML text into a JSONObject, and to
Expand Down Expand Up @@ -80,7 +81,7 @@ private static Iterable<Integer> codePointIterator(final String string) {
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private int nextIndex = 0;
private int length = string.length();
private final int length = string.length();

@Override
public boolean hasNext() {
Expand All @@ -89,6 +90,9 @@ public boolean hasNext() {

@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int result = string.codePointAt(this.nextIndex);
this.nextIndex += Character.charCount(result);
return result;
Expand Down
20 changes: 11 additions & 9 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3117,12 +3117,13 @@ public void testJSONWriterException() {

// test a more complex object
writer = new StringWriter();
try {
new JSONObject()

JSONObject object = new JSONObject()
.put("somethingElse", "a value")
.put("someKey", new JSONArray()
.put(new JSONObject().put("key1", new BrokenToString())))
.write(writer).toString();
.put(new JSONObject().put("key1", new BrokenToString())));
try {
object.write(writer).toString();
fail("Expected an exception, got a String value");
} catch (JSONException e) {
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
Expand All @@ -3133,17 +3134,18 @@ public void testJSONWriterException() {
writer.close();
} catch (Exception e) {}
}

// test a more slightly complex object
writer = new StringWriter();
try {
new JSONObject()

object = new JSONObject()
.put("somethingElse", "a value")
.put("someKey", new JSONArray()
.put(new JSONObject().put("key1", new BrokenToString()))
.put(12345)
)
.write(writer).toString();
);
try {
object.write(writer).toString();
fail("Expected an exception, got a String value");
} catch (JSONException e) {
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
Expand Down
Loading