6 Commits

Author SHA1 Message Date
Sean Leary
e635f40238 Merge pull request #1027 from Simulant87/1023-set-default-locale
Save/restore default locale in test
2025-12-29 19:42:57 -06:00
Sean Leary
d5e744ca90 Merge pull request #1028 from Simulant87/fix-sonarqube-reliability-issues
Refactoring: Fix sonarqube reliability issues
2025-12-29 19:42:02 -06:00
Sean Leary
e0c4086168 Merge pull request #1029 from Simulant87/external-javadoc-badge
add badge to external hosted javadoc
2025-12-29 19:41:31 -06:00
Simulant
96353de304 add badge to external hosted javadoc 2025-12-21 23:16:01 +01:00
Simulant
8cbb4d5bb3 Fix sonarqube reliability issues 2025-12-20 22:57:24 +01:00
Simulant
421abfdc1f save and restore the current default locale, to avoid any side effects on other executions in the same JVM 2025-12-20 22:27:45 +01:00
4 changed files with 42 additions and 29 deletions

View File

@@ -9,6 +9,7 @@ JSON in Java [package org.json]
[![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json)
[![Java CI with Maven](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml) [![Java CI with Maven](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml)
[![CodeQL](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml) [![CodeQL](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml)
[![javadoc](https://javadoc.io/badge2/org.json/json/javadoc.svg)](https://javadoc.io/doc/org.json/json)
**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20251224/json-20251224.jar)** **[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20251224/json-20251224.jar)**

View File

@@ -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;

View File

@@ -36,25 +36,31 @@ public class JSONObjectLocaleTest {
MyLocaleBean myLocaleBean = new MyLocaleBean(); MyLocaleBean myLocaleBean = new MyLocaleBean();
/** // save and restore the current default locale, to avoid any side effects on other executions in the same JVM
* This is just the control case which happens when the locale.ROOT Locale defaultLocale = Locale.getDefault();
* lowercasing behavior is the same as the current locale. try {
*/ /**
Locale.setDefault(new Locale("en")); * This is just the control case which happens when the locale.ROOT
JSONObject jsonen = new JSONObject(myLocaleBean); * lowercasing behavior is the same as the current locale.
assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length()); */
assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i")); Locale.setDefault(new Locale("en"));
assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id")); JSONObject jsonen = new JSONObject(myLocaleBean);
assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length());
assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i"));
assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id"));
/** /**
* Without the JSON-Java change, these keys would be stored internally as * Without the JSON-Java change, these keys would be stored internally as
* starting with the letter, 'ı' (dotless i), since the lowercasing of * starting with the letter, 'ı' (dotless i), since the lowercasing of
* the getI and getId keys would be specific to the Turkish locale. * the getI and getId keys would be specific to the Turkish locale.
*/ */
Locale.setDefault(new Locale("tr")); Locale.setDefault(new Locale("tr"));
JSONObject jsontr = new JSONObject(myLocaleBean); JSONObject jsontr = new JSONObject(myLocaleBean);
assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length()); assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length());
assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i")); assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i"));
assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id")); assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id"));
} finally {
Locale.setDefault(defaultLocale);
}
} }
} }

View File

@@ -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());
@@ -3136,14 +3137,15 @@ public class JSONObjectTest {
// 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());