mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #274 from johnjaylward/NumberOutputFix
Fix for number output bug.
This commit is contained in:
commit
c24be0e4ea
1 changed files with 39 additions and 16 deletions
|
@ -1693,7 +1693,18 @@ public class JSONObject {
|
|||
throw new JSONException("Bad value from toJSONString: " + object);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return numberToString((Number) value);
|
||||
// not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
|
||||
final String numberAsString = numberToString((Number) value);
|
||||
try {
|
||||
// Use the BigDecimal constructor for it's parser to validate the format.
|
||||
new BigDecimal(numberAsString);
|
||||
// Close enough to a JSON number that we will return it unquoted
|
||||
return numberAsString;
|
||||
} catch (NumberFormatException ex){
|
||||
// The Number value is not a valid JSON number.
|
||||
// Instead we will quote it as a string
|
||||
return quote(numberAsString);
|
||||
}
|
||||
}
|
||||
if (value instanceof Boolean || value instanceof JSONObject
|
||||
|| value instanceof JSONArray) {
|
||||
|
@ -1786,6 +1797,32 @@ public class JSONObject {
|
|||
int indentFactor, int indent) throws JSONException, IOException {
|
||||
if (value == null || value.equals(null)) {
|
||||
writer.write("null");
|
||||
} else if (value instanceof JSONString) {
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
} else if (value instanceof Number) {
|
||||
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
|
||||
final String numberAsString = numberToString((Number) value);
|
||||
try {
|
||||
// Use the BigDecimal constructor for it's parser to validate the format.
|
||||
@SuppressWarnings("unused")
|
||||
BigDecimal testNum = new BigDecimal(numberAsString);
|
||||
// Close enough to a JSON number that we will use it unquoted
|
||||
writer.write(numberAsString);
|
||||
} catch (NumberFormatException ex){
|
||||
// The Number value is not a valid JSON number.
|
||||
// Instead we will quote it as a string
|
||||
quote(numberAsString, writer);
|
||||
}
|
||||
} else if (value instanceof Boolean) {
|
||||
writer.write(value.toString());
|
||||
} else if (value instanceof Enum<?>) {
|
||||
writer.write(quote(((Enum<?>)value).name()));
|
||||
} else if (value instanceof JSONObject) {
|
||||
((JSONObject) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof JSONArray) {
|
||||
|
@ -1798,20 +1835,6 @@ public class JSONObject {
|
|||
new JSONArray(coll).write(writer, indentFactor, indent);
|
||||
} else if (value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
writer.write(numberToString((Number) value));
|
||||
} else if (value instanceof Boolean) {
|
||||
writer.write(value.toString());
|
||||
} else if (value instanceof Enum<?>) {
|
||||
writer.write(quote(((Enum<?>)value).name()));
|
||||
} else if (value instanceof JSONString) {
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
} else {
|
||||
quote(value.toString(), writer);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue