mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Adds support to JSONObject wrap and write methods to explicitly handle Enums.
The new way enums are handled is to always place the actual enum in the JSONObject/JSONArray. When writing, we always write the actual "name" of the enum, so even with a toString override on the enum class, the value remains consistant and compatible with the optEnum/getEnum methods. The constructor JSONObject(Object) functions the same way as before when passing an enum and is consistent with other "value" types. For example, when creating a JSONObject with Long, Boolean, BigDecimal as the constructor parameter, the value will be treated as a "bean".
This commit is contained in:
parent
4e8e24d49d
commit
91107e3e82
1 changed files with 8 additions and 6 deletions
|
@ -1708,6 +1708,9 @@ public class JSONObject {
|
||||||
if (value.getClass().isArray()) {
|
if (value.getClass().isArray()) {
|
||||||
return new JSONArray(value).toString();
|
return new JSONArray(value).toString();
|
||||||
}
|
}
|
||||||
|
if(value instanceof Enum<?>){
|
||||||
|
return quote(((Enum<?>)value).name());
|
||||||
|
}
|
||||||
return quote(value.toString());
|
return quote(value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1730,12 +1733,9 @@ public class JSONObject {
|
||||||
}
|
}
|
||||||
if (object instanceof JSONObject || object instanceof JSONArray
|
if (object instanceof JSONObject || object instanceof JSONArray
|
||||||
|| NULL.equals(object) || object instanceof JSONString
|
|| NULL.equals(object) || object instanceof JSONString
|
||||||
|| object instanceof Byte || object instanceof Character
|
|| object instanceof Number || object instanceof Character
|
||||||
|| object instanceof Short || object instanceof Integer
|
|| object instanceof Boolean || object instanceof String
|
||||||
|| object instanceof Long || object instanceof Boolean
|
|| object instanceof Enum) {
|
||||||
|| object instanceof Float || object instanceof Double
|
|
||||||
|| object instanceof String || object instanceof BigInteger
|
|
||||||
|| object instanceof BigDecimal) {
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1797,6 +1797,8 @@ public class JSONObject {
|
||||||
writer.write(numberToString((Number) value));
|
writer.write(numberToString((Number) value));
|
||||||
} else if (value instanceof Boolean) {
|
} else if (value instanceof Boolean) {
|
||||||
writer.write(value.toString());
|
writer.write(value.toString());
|
||||||
|
} else if (value instanceof Enum<?>) {
|
||||||
|
writer.write(quote(((Enum<?>)value).name()));
|
||||||
} else if (value instanceof JSONString) {
|
} else if (value instanceof JSONString) {
|
||||||
Object o;
|
Object o;
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue