Skip to content

Turn off or on forward slash escaping on an already created JSONObject. #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/main/java/org/codehaus/jettison/json/JSONObject.java
Original file line number Diff line number Diff line change
@@ -143,6 +143,8 @@ public boolean isExplicitNull() {
private List ignoredElements;
private boolean writeNullAsString = true;
private boolean escapeForwardSlashAlways = true;


/**
* It is sometimes more convenient and less ambiguous to have a
* <code>NULL</code> object than to use Java's <code>null</code> value.
@@ -1385,4 +1387,12 @@ public Writer write(Writer writer) throws JSONException {
throw new JSONException(e);
}
}

public boolean isEscapeForwardSlashAlways() {
return escapeForwardSlashAlways;
}

public void setEscapeForwardSlashAlways(boolean escapeForwardSlashAlways) {
this.escapeForwardSlashAlways = escapeForwardSlashAlways;
}
}
16 changes: 16 additions & 0 deletions src/test/java/org/codehaus/jettison/json/JSONObjectTest.java
Original file line number Diff line number Diff line change
@@ -77,4 +77,20 @@ public void testMissingIsNull() throws Exception {
JSONObject obj = new JSONObject("{\"a\":null}");
assertTrue(obj.isNull("b"));
}

public void testSlashEscapingTurnedOnByDefault() throws Exception {
JSONObject obj = new JSONObject();
obj.put("key", "http://example.com/foo");
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
}

public void testForwardSlashEscapingModifiedfBySetter() throws Exception {
JSONObject obj = new JSONObject();
obj.put("key", "http://example.com/foo");
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
obj.setEscapeForwardSlashAlways(false);
assertEquals(obj.toString(), "{\"key\":\"http://example.com/foo\"}");
obj.setEscapeForwardSlashAlways(true);
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
}
}