Skip to content

Commit

Permalink
Merge pull request #274 from johnjaylward/NumberOutputFix
Browse files Browse the repository at this point in the history
Fix for number output bug.
  • Loading branch information
stleary committed Sep 9, 2016
2 parents ebe69df + 88f65c5 commit c24be0e
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ public static String numberToString(Number number) throws JSONException {
}
testValidity(number);

// Shave off trailing zeros and decimal point, if possible.
// Shave off trailing zeros and decimal point, if possible.

String string = number.toString();
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
Expand Down Expand Up @@ -1693,7 +1693,18 @@ public static String valueToString(Object value) throws JSONException {
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) {
Expand Down Expand Up @@ -1786,6 +1797,32 @@ static final Writer writeValue(Writer writer, Object value,
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) {
Expand All @@ -1798,20 +1835,6 @@ static final Writer writeValue(Writer writer, Object value,
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);
}
Expand Down

0 comments on commit c24be0e

Please sign in to comment.