Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a test case for an enum implementing JSONString #870

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,7 @@ static final Writer writeValue(Writer writer, Object value,
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONString) {
// JSONString must be checked first, so it can overwrite behaviour of other types below
Object o;
try {
o = ((JSONString) value).toJSONString();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/json/junit/JSONStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ public void testNullStringValue() throws Exception {
}
}

@Test
public void testEnumJSONString() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", MyEnum.MY_ENUM);
assertEquals("{\"key\":\"myJsonString\"}", jsonObject.toString());
}

private enum MyEnum implements JSONString {
MY_ENUM;

@Override
public String toJSONString() {
return "\"myJsonString\"";
}
}

/**
* A JSONString that returns a valid JSON string value.
*/
Expand Down
Loading