Browse files

whitespaces processing

  • Loading branch information...
1 parent e3ddaad commit 4b2a2c1305907eabe736414f70b485dea5687070 @ropalka ropalka committed Aug 30, 2012
View
8 TODO
@@ -1,4 +1,8 @@
-P1 Implement JSON reader whitespace processing - http://en.wikipedia.org/wiki/Whitespace_character
+P1 configure checkstyle
P2 Implement configuration properties
+ * enable indentation
+ * string length limits
+ * number limits
+ * nesting limits
P2 implement indentation support
-P3 configure checkstyle
+
View
4 impl/src/main/java/com/fossnova/json/stream/JsonReaderImpl.java
@@ -21,6 +21,7 @@
import static com.fossnova.json.stream.JsonConstants.BACKSLASH;
import static com.fossnova.json.stream.Utils.isControl;
+import static com.fossnova.json.stream.Utils.isWhitespace;
import static com.fossnova.json.stream.Utils.toUnicodeString;
import java.io.IOException;
@@ -167,6 +168,9 @@ public JsonEvent next() throws IOException {
}
break;
default: {
+ if ( isWhitespace( nextCharacter ) ) {
+ continue;
+ }
if ( nextCharacter >= 0 ) {
throw new JsonException( "Unexpected character '" + toUnicodeString( nextCharacter ) + "' while reading JSON stream" );
} else {
View
4 impl/src/main/java/com/fossnova/json/stream/Utils.java
@@ -43,6 +43,10 @@ static boolean isControl( final int c ) {
return false;
}
+ static boolean isWhitespace( final int c ) {
+ return ( c == ' ' ) || ( c == '\t' ) || ( c == '\r' ) || ( c == '\n' );
+ }
+
static String toUnicodeString( final int c ) {
final StringBuilder sb = new StringBuilder();
sb.append( BACKSLASH ).append( 'u' );
View
17 tests/src/test/java/test/fossnova/json/stream/ValidJsonReaderTestCase.java
@@ -32,17 +32,17 @@
public final class ValidJsonReaderTestCase extends AbstractJsonTestCase {
@Test
- public void emptyObject() throws IOException {
- final JsonReader reader = getJsonReader( "{}" );
+ public void emptyObjectWithWhitespaces() throws IOException {
+ final JsonReader reader = getJsonReader( "{ \t\r\n}" );
assertObjectStartState( reader );
assertObjectEndState( reader );
assertFinalState( reader );
reader.close();
}
@Test
- public void simpleObject() throws IOException {
- final JsonReader reader = getJsonReader( "{\"a\":\"b\"}" );
+ public void simpleObjectWithWhitespaces() throws IOException {
+ final JsonReader reader = getJsonReader( "{\r\n\t\"a\" : \"b\"\r\n}" );
assertObjectStartState( reader );
assertStringState( reader, "a" );
assertStringState( reader, "b" );
@@ -106,17 +106,18 @@ public void theMostComplexObject() throws IOException {
}
@Test
- public void emptyArray() throws IOException {
- final JsonReader reader = getJsonReader( "[]" );
+ public void emptyArrayWithWhitespaces() throws IOException {
+ final JsonReader reader = getJsonReader( "[ \t\r\n]" );
assertArrayStartState( reader );
assertArrayEndState( reader );
assertFinalState( reader );
reader.close();
}
@Test
- public void moreComplexArray() throws IOException {
- final String data = "[\"0\",1,2,3,4,5.0,6.0," + "700000000000000000000000000000000000000,800000000000000000000000000000000000000.000000000000000000000000000009,true,null]";
+ public void moreComplexArrayWithWhitespaces() throws IOException {
+ final String data = "[ \"0\" \r\n, 1 \r\n, 2 \r\n, 3 \r\n, 4 \r\n, 5.0 \r\n, 6.0\r\n, "
+ + "700000000000000000000000000000000000000,800000000000000000000000000000000000000.000000000000000000000000000009 \r\n, true \r\n, null \r\n]";
final JsonReader reader = getJsonReader( data );
assertArrayStartState( reader );
assertStringState( reader, "0" );

0 comments on commit 4b2a2c1

Please sign in to comment.