Skip to content
Browse files

Skip byte order mark (BOM) in org.json

Bug: http://code.google.com/p/android/issues/detail?id=18508
Change-Id: Ib992a0a4b22e340446abab14e4f32df5efcd0b49
  • Loading branch information...
1 parent b1f55cb commit 037fb18dd4ba3f54c5b2fb5386c5673e1db5babf Jesse Wilson committed Jul 20, 2011
Showing with 16 additions and 0 deletions.
  1. +4 −0 json/src/main/java/org/json/JSONTokener.java
  2. +12 −0 json/src/test/java/org/json/JSONTokenerTest.java
View
4 json/src/main/java/org/json/JSONTokener.java
@@ -76,6 +76,10 @@
* called.
*/
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;
}
View
12 json/src/test/java/org/json/JSONTokenerTest.java
@@ -574,6 +574,18 @@ public void testSkipToStopsOnNull() {
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() {
assertEquals( 0, JSONTokener.dehexchar('0'));
assertEquals( 1, JSONTokener.dehexchar('1'));

0 comments on commit 037fb18

Please sign in to comment.
Something went wrong with that request. Please try again.