Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixing comma issue

  • Loading branch information...
commit fa1dc22e345bcb05e02c037fd7b2612015f01b1b 1 parent 75cd1cf
@ropalka ropalka authored
View
4 impl/src/main/java/com/fossnova/json/JsonGrammarAnalyzer.java
@@ -106,6 +106,8 @@ private void putObjectEnd() {
stack.removeLast();
stack.removeLast();
canWriteComma = true;
+ } else if ( isLastOnStack( ARRAY_START) ) {
+ canWriteComma = true;
}
if ( isEmpty() ) {
setCannotContinue();
@@ -123,6 +125,8 @@ private void putArrayEnd() {
stack.removeLast();
stack.removeLast();
canWriteComma = true;
+ } else if ( isLastOnStack( ARRAY_START) ) {
+ canWriteComma = true;
}
if ( isEmpty() ) {
setCannotContinue();
View
14 tests/src/test/java/test/fossnova/json/AbstractJsonTestCase.java
@@ -32,12 +32,16 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.fossnova.json.JsonException;
+import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonReader;
+import org.fossnova.json.JsonWriter;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
@@ -296,4 +300,14 @@ private static void assertNotNumberException( final JsonReader reader ) throws I
assertEquals( "Current event isn't number", e.getMessage() );
}
}
+
+ static JsonReader getJsonReader( final String data ) throws IOException {
+ final ByteArrayInputStream bais = new ByteArrayInputStream( data.getBytes() );
+ return JsonFactory.newInstance().newJsonReader( bais );
+ }
+
+ static JsonWriter getJsonWriter() throws IOException {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ return JsonFactory.newInstance().newJsonWriter( baos );
+ }
}
View
50 tests/src/test/java/test/fossnova/json/InvalidJsonReaderTestCase.java
@@ -19,11 +19,8 @@
*/
package test.fossnova.json;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
-import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonReader;
import org.junit.Test;
@@ -33,8 +30,7 @@
public final class InvalidJsonReaderTestCase extends AbstractJsonTestCase {
// TODO: implement test - EOF at string, number, boolean, null, array or object structure
- // TODO: implement test - colon at array instead of comma
- // TODO: implement test - comma at object instead of colon and vice versa
+
@Test
public void emptyState() throws IOException {
read_colon();
@@ -66,10 +62,14 @@ public void emptyArrayStartState() throws IOException {
@Test
public void notEmptyArrayStartState() throws IOException {
+ read_arrayStart_arrayStart_arrayEnd_objectEnd();
+ read_arrayStart_objectStart_objectEnd_objectEnd();
read_arrayStart_string_objectEnd();
read_arrayStart_number_objectEnd();
read_arrayStart_boolean_objectEnd();
read_arrayStart_null_objectEnd();
+ read_arrayStart_arrayStart_arrayEnd_colon();
+ read_arrayStart_objectStart_objectEnd_colon();
read_arrayStart_string_colon();
read_arrayStart_number_colon();
read_arrayStart_boolean_colon();
@@ -106,6 +106,7 @@ public void arrayStartArrayEndState() throws IOException {
@Test
public void notEmptyObjectStartState() throws IOException {
+ // TODO: review test cases - add comma & colon words to method names
read_objectStart_string_comma();
read_objectStart_string_objectEnd();
read_objectStart_string_string_arrayEnd();
@@ -231,6 +232,22 @@ private void read_arrayStart_comma() throws IOException {
assertJsonException( reader, "Expecting ] { [ STRING NUMBER true false null" );
}
+ private void read_arrayStart_arrayStart_arrayEnd_objectEnd() throws IOException {
+ final JsonReader reader = getJsonReader( "[[]}" );
+ assertArrayStartState( reader );
+ assertArrayStartState( reader );
+ assertArrayEndState( reader );
+ assertJsonException( reader, "Expecting , ]" );
+ }
+
+ private void read_arrayStart_objectStart_objectEnd_objectEnd() throws IOException {
+ final JsonReader reader = getJsonReader( "[{}}" );
+ assertArrayStartState( reader );
+ assertObjectStartState( reader );
+ assertObjectEndState( reader );
+ assertJsonException( reader, "Expecting , ]" );
+ }
+
private void read_arrayStart_string_objectEnd() throws IOException {
final JsonReader reader = getJsonReader( "[\"\"}" );
assertArrayStartState( reader );
@@ -263,6 +280,22 @@ private void read_arrayStart_null_objectEnd() throws IOException {
assertJsonException( reader, "Expecting , ]" );
}
+ private void read_arrayStart_arrayStart_arrayEnd_colon() throws IOException {
+ final JsonReader reader = getJsonReader( "[[]:" );
+ assertArrayStartState( reader );
+ assertArrayStartState( reader );
+ assertArrayEndState( reader );
+ assertJsonException( reader, "Expecting , ]" );
+ }
+
+ private void read_arrayStart_objectStart_objectEnd_colon() throws IOException {
+ final JsonReader reader = getJsonReader( "[{}:" );
+ assertArrayStartState( reader );
+ assertObjectStartState( reader );
+ assertObjectEndState( reader );
+ assertJsonException( reader, "Expecting , ]" );
+ }
+
private void read_arrayStart_string_colon() throws IOException {
final JsonReader reader = getJsonReader( "[\"\":" );
assertArrayStartState( reader );
@@ -486,7 +519,7 @@ private void read_objectStart_string_string_boolean() throws IOException {
assertStringState( reader, "" );
assertStringState( reader, "" );
assertJsonException( reader, "Expecting } STRING" );
- reader = getJsonReader( "{\"\":\"\",false" ); // TODO: is there a test without , ?
+ reader = getJsonReader( "{\"\":\"\",false" );
assertObjectStartState( reader );
assertStringState( reader, "" );
assertStringState( reader, "" );
@@ -634,9 +667,4 @@ private void read_objectStart_string_null_string_arrayEnd() throws IOException {
assertStringState( reader, "2" );
assertJsonException( reader, "Expecting :" );
}
-
- private JsonReader getJsonReader( final String data ) throws IOException {
- final ByteArrayInputStream bais = new ByteArrayInputStream( data.getBytes() );
- return JsonFactory.newInstance().newJsonReader( new InputStreamReader( bais ) );
- }
}
View
10 tests/src/test/java/test/fossnova/json/InvalidJsonWriterTestCase.java
@@ -22,21 +22,18 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.fossnova.json.JsonException;
-import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonWriter;
import org.junit.Test;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
-public final class InvalidJsonWriterTestCase {
+public final class InvalidJsonWriterTestCase extends AbstractJsonTestCase {
@Test
public void emptyState() throws IOException {
@@ -1395,9 +1392,4 @@ private void write_objectStart_string_null_string_arrayEnd() throws IOException
assertEquals( "Expecting :", e.getMessage() );
}
}
-
- private JsonWriter getJsonWriter() throws IOException {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- return JsonFactory.newInstance().newJsonWriter( new OutputStreamWriter( baos ) );
- }
}
View
10 tests/src/test/java/test/fossnova/json/ValidJsonReaderTestCase.java
@@ -19,14 +19,10 @@
*/
package test.fossnova.json;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
-import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonReader;
import org.junit.Test;
@@ -141,7 +137,7 @@ public void moreComplexArray() throws IOException {
@Test
public void theMostComplexArray() throws IOException {
- final JsonReader reader = getJsonReader( "[\"0\",{\"String\":\"s\",\"boolean\":false},\"1\",[null,true,7,{}[[]]]]" );
+ final JsonReader reader = getJsonReader( "[\"0\",{\"String\":\"s\",\"boolean\":false},\"1\",[null,true,7,{},[[]]]]" );
assertArrayStartState( reader );
assertStringState( reader, "0" );
assertObjectStartState( reader );
@@ -202,8 +198,4 @@ public void controlsEncoding() throws IOException {
assertArrayEndState( reader );
reader.close();
}
-
- private static JsonReader getJsonReader( final String data ) throws UnsupportedEncodingException {
- return JsonFactory.newInstance().newJsonReader( new InputStreamReader( new ByteArrayInputStream( data.getBytes( "UTF-8" ) ) ) );
- }
}
View
2  tests/src/test/java/test/fossnova/json/ValidJsonWriterTestCase.java
@@ -119,7 +119,7 @@ public void theMostComplexArray() throws IOException {
writer.writeArrayEnd();
writer.flush();
writer.close();
- Assert.assertEquals( "[\"0\",{\"String\":\"s\",\"boolean\":false},\"1\",[null,true,7,{}[[]]]]", getWriterOutput() );
+ Assert.assertEquals( "[\"0\",{\"String\":\"s\",\"boolean\":false},\"1\",[null,true,7,{},[[]]]]", getWriterOutput() );
}
@Test
Please sign in to comment.
Something went wrong with that request. Please try again.