Skip to content

Commit

Permalink
add testcase & bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 29, 2016
1 parent ec6185e commit 2405363
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 27 deletions.
68 changes: 41 additions & 27 deletions src/main/java/com/alibaba/fastjson/parser/JSONLexerBase.java
Expand Up @@ -1276,7 +1276,6 @@ public String scanString(char expectNextChar) {
return stringDefaultValue(); return stringDefaultValue();
} }


boolean hasSpecial = false;
final String strVal; final String strVal;
{ {
int startIndex = bp + 1; int startIndex = bp + 1;
Expand Down Expand Up @@ -1308,12 +1307,6 @@ public String scanString(char expectNextChar) {
stringVal = readString(chars, chars_len); stringVal = readString(chars, chars_len);
} }


if (hasSpecial) {
matchStat = NOT_MATCH;

return stringDefaultValue();
}

offset += (endIndex - (bp + 1) + 1); offset += (endIndex - (bp + 1) + 1);
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
strVal = stringVal; strVal = stringVal;
Expand Down Expand Up @@ -1524,30 +1517,51 @@ public Collection<String> scanFieldStringArray(char[] fieldName, Class<?> type)
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));


for (;;) { for (;;) {
if (chLocal != '"') {
matchStat = NOT_MATCH;
return null;
}

String strVal;
// int start = index; // int start = index;
int startOffset = offset; if (chLocal == '"') {
for (;;) { int startIndex = bp + offset;
chLocal = charAt(bp + (offset++)); int endIndex = indexOf('"', startIndex);
if (chLocal == '\"') { if (endIndex == -1) {
int start = bp + startOffset; throw new JSONException("unclosed str");
int len = bp + offset - start - 1;
strVal = subString(start, len);
list.add(strVal);

chLocal = charAt(bp + (offset++));
break;
} }


if (chLocal == '\\') { int startIndex2 = bp + offset; // must re compute
matchStat = NOT_MATCH; String stringVal = subString(startIndex2, endIndex - startIndex2);
return null; if (stringVal.indexOf('\\') != -1) {
for (;;) {
int slashCount = 0;
for (int i = endIndex - 1; i >= 0; --i) {
if (charAt(i) == '\\') {
slashCount++;
} else {
break;
}
}
if (slashCount % 2 == 0) {
break;
}
endIndex = indexOf('"', endIndex + 1);
}

int chars_len = endIndex - (bp + offset);
char[] chars = sub_chars(bp + offset, chars_len);

stringVal = readString(chars, chars_len);
} }

offset += (endIndex - (bp + offset) + 1);
chLocal = charAt(bp + (offset++));

list.add(stringVal);
} else if (chLocal == 'n' && charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
offset += 3;
chLocal = charAt(bp + (offset++));
list.add(null);
} else if (chLocal == ']' && list.size() == 0) {
chLocal = charAt(bp + (offset++));
break;
} else {
throw new JSONException("illega str");
} }


if (chLocal == ',') { if (chLocal == ',') {
Expand Down
@@ -0,0 +1,143 @@
package com.alibaba.json.bvt.parser.deser.list;

import java.io.StringReader;
import java.util.List;
import java.util.Map;

import org.junit.Assert;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.TypeReference;

import junit.framework.TestCase;

public class ListStringFieldTest_stream extends TestCase {

public void test_list() throws Exception {
String text = "{\"values\":[\"a\",null,\"b\",\"ab\\\\c\"]}";

JSONReader reader = new JSONReader(new StringReader(text));
Model model = reader.readObject(Model.class);
Assert.assertEquals(4, model.values.size());
Assert.assertEquals("a", model.values.get(0));
Assert.assertEquals(null, model.values.get(1));
Assert.assertEquals("b", model.values.get(2));
Assert.assertEquals("ab\\c", model.values.get(3));
}

public void test_null() throws Exception {
String text = "{\"values\":null}";
JSONReader reader = new JSONReader(new StringReader(text));
Model model = reader.readObject(Model.class);
Assert.assertNull(model.values);
}

public void test_empty() throws Exception {
String text = "{\"values\":[]}";
JSONReader reader = new JSONReader(new StringReader(text));
Model model = reader.readObject(Model.class);
Assert.assertEquals(0, model.values.size());
}

public void test_map_empty() throws Exception {
String text = "{\"model\":{\"values\":[]}}";
JSONReader reader = new JSONReader(new StringReader(text));
Map<String, Model> map = reader.readObject(new TypeReference<Map<String, Model>>() {
});
Model model = (Model) map.get("model");
Assert.assertEquals(0, model.values.size());
}

public void test_notMatch() throws Exception {
String text = "{\"value\":[]}";
JSONReader reader = new JSONReader(new StringReader(text));
Model model = reader.readObject(Model.class);
Assert.assertNull(model.values);
}

public void test_error() throws Exception {
String text = "{\"values\":[1";
JSONReader reader = new JSONReader(new StringReader(text));

Exception error = null;
try {
reader.readObject(Model.class);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error_1() throws Exception {
String text = "{\"values\":[\"b\"[";
JSONReader reader = new JSONReader(new StringReader(text));

Exception error = null;
try {
reader.readObject(Model.class);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error_2() throws Exception {
String text = "{\"model\":{\"values\":[][";
JSONReader reader = new JSONReader(new StringReader(text));


Exception error = null;
try {
reader.readObject(new TypeReference<Map<String, Model>>() {
});
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error_3() throws Exception {
String text = "{\"model\":{\"values\":[]}[";
JSONReader reader = new JSONReader(new StringReader(text));


Exception error = null;
try {
reader.readObject(new TypeReference<Map<String, Model>>() {
});
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error_4() throws Exception {
String text = "{\"model\":{\"values\":[]}}";
JSONReader reader = new JSONReader(new StringReader(text));


Exception error = null;
try {
reader.readObject(new TypeReference<Map<String, Model>>() {
});
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public static class Model {

private List<String> values;

public List<String> getValues() {
return values;
}

public void setValues(List<String> values) {
this.values = values;
}

}
}

0 comments on commit 2405363

Please sign in to comment.