Skip to content
Open
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
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
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Up @@ -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\"}");
}
}