Skip to content

Commit

Permalink
Skip byte order mark (BOM) in org.json
Browse files Browse the repository at this point in the history
Bug: http://code.google.com/p/android/issues/detail?id=18508
Change-Id: Ib992a0a4b22e340446abab14e4f32df5efcd0b49
  • Loading branch information
Jesse Wilson committed Jul 20, 2011
1 parent b1f55cb commit 037fb18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions json/src/main/java/org/json/JSONTokener.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public class JSONTokener {
* called. * called.
*/ */
public JSONTokener(String in) { public JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
}
this.in = in; this.in = in;
} }


Expand Down
12 changes: 12 additions & 0 deletions json/src/test/java/org/json/JSONTokenerTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -574,6 +574,18 @@ public void testSkipToStopsOnNull() {
assertEquals("skipTo shouldn't stop when it sees '\\0'", 'F', tokener.next()); assertEquals("skipTo shouldn't stop when it sees '\\0'", 'F', tokener.next());
} }


public void testBomIgnoredAsFirstCharacterOfDocument() throws JSONException {
JSONTokener tokener = new JSONTokener("\ufeff[]");
JSONArray array = (JSONArray) tokener.nextValue();
assertEquals(0, array.length());
}

public void testBomTreatedAsCharacterInRestOfDocument() throws JSONException {
JSONTokener tokener = new JSONTokener("[\ufeff]");
JSONArray array = (JSONArray) tokener.nextValue();
assertEquals(1, array.length());
}

public void testDehexchar() { public void testDehexchar() {
assertEquals( 0, JSONTokener.dehexchar('0')); assertEquals( 0, JSONTokener.dehexchar('0'));
assertEquals( 1, JSONTokener.dehexchar('1')); assertEquals( 1, JSONTokener.dehexchar('1'));
Expand Down

0 comments on commit 037fb18

Please sign in to comment.