Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 29, 2016
1 parent 4fb82dd commit 02e002e
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/JSONReader.java
Expand Up @@ -160,7 +160,7 @@ public int peek() {
} }


public void close() { public void close() {
IOUtils.close(parser); parser.close();
} }


public Integer readInteger() { public Integer readInteger() {
Expand Down
48 changes: 31 additions & 17 deletions src/main/java/com/alibaba/fastjson/parser/JSONLexerBase.java
Expand Up @@ -1659,31 +1659,45 @@ && charAt(bp + offset + 2) == 'l') {
offset += 3; offset += 3;
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
list.add(null); list.add(null);
} else if (chLocal == ']' && list.size() == 0) {
chLocal = charAt(bp + (offset++));
break;
} else if (chLocal != '"') { } else if (chLocal != '"') {
matchStat = NOT_MATCH; matchStat = NOT_MATCH;
return null; return null;
} else { } else {
int startIndex = bp + offset;
int endIndex = indexOf('"', startIndex);
if (endIndex == -1) {
throw new JSONException("unclosed str");
}


String strVal; String stringVal = subString(bp + offset, endIndex - startIndex);
// int start = index; if (stringVal.indexOf('\\') != -1) {
int startOffset = offset; for (;;) {
for (;;) { int slashCount = 0;
chLocal = charAt(bp + (offset++)); for (int i = endIndex - 1; i >= 0; --i) {
if (chLocal == '\"') { if (charAt(i) == '\\') {
int start = bp + startOffset; slashCount++;
int len = bp + offset - start - 1; } else {
strVal = subString(start, len); break;
list.add(strVal); }

}
chLocal = charAt(bp + (offset++)); if (slashCount % 2 == 0) {
break; break;
}
endIndex = indexOf('"', endIndex + 1);
} }


if (chLocal == '\\') { int chars_len = endIndex - startIndex;
matchStat = NOT_MATCH; char[] chars = sub_chars(bp + offset, chars_len);
return null;
} stringVal = readString(chars, chars_len);
} }

offset += (endIndex - (bp + offset) + 1);
chLocal = charAt(bp + (offset++));
list.add(stringVal);
} }


if (chLocal == ',') { if (chLocal == ',') {
Expand Down
Expand Up @@ -21,7 +21,6 @@ public void test_0() throws Exception {
} }
Assert.assertNotNull(error); Assert.assertNotNull(error);


reader.close();
} }


public void test_1() throws Exception { public void test_1() throws Exception {
Expand All @@ -37,6 +36,5 @@ public void test_1() throws Exception {
error = e; error = e;
} }
Assert.assertNotNull(error); Assert.assertNotNull(error);
reader.close();
} }
} }
@@ -0,0 +1,126 @@
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 com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.parser.Feature;

import junit.framework.TestCase;

public class ListStringFieldTest_stream_array extends TestCase {

public void test_list() throws Exception {
String text = "[[\"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 = "[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 = "[[]]}";
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\":[[]]}";
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_error() throws Exception {
String text = "[[1{1,}";
JSONReader reader = new JSONReader(new StringReader(text));

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

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

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

public void test_error_2() throws Exception {
String text = "{\"model\":[[][";
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\":[[]}[";
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);
}

@JSONType(parseFeatures = Feature.SupportArrayToBean)
public static class Model {

private List<String> values;

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

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

}
}
Expand Up @@ -22,6 +22,5 @@ public void test_read_Long() throws Exception {
error = ex; error = ex;
} }
Assert.assertNotNull(error); Assert.assertNotNull(error);
reader.close();
} }
} }
Expand Up @@ -14,7 +14,6 @@ public void test_read_Long() throws Exception {
String text = "1001"; String text = "1001";
JSONReader reader = new JSONReader(new MyReader(text)); JSONReader reader = new JSONReader(new MyReader(text));


reader.close();
} }


public static class MyReader extends BufferedReader { public static class MyReader extends BufferedReader {
Expand Down
Expand Up @@ -45,6 +45,5 @@ public void test_read() throws Exception {
} }
Assert.assertNotNull(error); Assert.assertNotNull(error);
} }
reader.close();
} }
} }
Expand Up @@ -38,7 +38,6 @@ public void test_read() throws Exception {
} }
Assert.assertNotNull(error); Assert.assertNotNull(error);
} }
reader.close();
} }


public static class VO { public static class VO {
Expand Down

0 comments on commit 02e002e

Please sign in to comment.