提交 c4632d1a 编写于 作者: W wenshao

bug fixed for TypeReference

上级 78f213b5
......@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.31-SNAPSHOT</version>
<version>1.1.31</version>
<packaging>jar</packaging>
<name>fastjson</name>
......
......@@ -523,7 +523,7 @@ public class DefaultJSONParser extends AbstractJSONParser {
continue;
}
}
if (lexer.token() == JSONToken.RBRACKET) {
break;
}
......
......@@ -1033,7 +1033,7 @@ public final class JSONScanner implements JSONLexer {
}
return null;
}
public String scanFieldSymbol(char[] fieldName, final SymbolTable symbolTable) {
matchStat = UNKOWN;
......@@ -2534,16 +2534,10 @@ public final class JSONScanner implements JSONLexer {
}
public boolean isEOF() {
switch (token) {
case JSONToken.EOF:
return true;
case JSONToken.ERROR:
return false;
case JSONToken.RBRACE:
return false;
default:
return false;
}
if (token == JSONToken.EOF) {
return true;
}
return false;
}
public void close() {
......
......@@ -251,11 +251,12 @@ public class DefaultObjectDeserializer implements ObjectDeserializer {
parser.setContext(context, value, key);
if (lexer.token() == JSONToken.EOF) {
final int tok = lexer.token();
if (tok == JSONToken.EOF || tok == JSONToken.RBRACKET) {
return map;
}
if (lexer.token() == JSONToken.RBRACE) {
if (tok == JSONToken.RBRACE) {
lexer.nextToken();
return map;
}
......
package com.alibaba.json.bvt;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import data.media.MediaContent;
public class ListFieldTest3 extends TestCase {
public void test_typeRef() throws Exception {
String text = "{\"images\":[],\"media\":{\"width\":640}}";
MediaContent object = JSON.parseObject(text, MediaContent.class);
}
public static class Root {
private List<Image> images = new ArrayList<Image>();
private Entity media;
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
public Entity getMedia() {
return media;
}
public void setMedia(Entity media) {
this.media = media;
}
}
public static class Image {
public int width;
}
public static class Entity {
public String title; // Can be null
public int width;
public int height;
public Size size;
}
public enum Size {
SMALL, LARGE
}
}
package com.alibaba.json.bvt;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class TypeReferenceTest6 extends TestCase {
public void test_typeRef() throws Exception {
TypeReference<Map<String, Entity>> typeRef = new TypeReference<Map<String, Entity>>() {
};
Map<String, Entity> map = JSON.parseObject(
"{\"value\":{\"id\":\"abc\",\"list\":[{\"id\":123}]}}", typeRef);
Entity entity = map.get("value");
Assert.assertNotNull(entity);
Assert.assertEquals("abc", entity.getId());
Assert.assertEquals(1, entity.getList().size());
Assert.assertEquals(123, entity.getList().get(0).getId());
}
public static class Entity {
private String id;
private List<A> list = new ArrayList<A>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<A> getList() {
return list;
}
public void setList(List<A> list) {
this.list = list;
}
}
public static class A {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
}
package com.alibaba.json.bvt;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class TypeReferenceTest7 extends TestCase {
public void test_typeRef() throws Exception {
TypeReference<Map<String, Entity>> typeRef = new TypeReference<Map<String, Entity>>() {
};
Map<String, Entity> map = JSON
.parseObject(
"{\"value\":{\"id\":\"abc\",\"a\":{\"id\":123}}}",
typeRef);
Entity entity = map.get("value");
Assert.assertNotNull(entity);
Assert.assertEquals("abc", entity.getId());
Assert.assertEquals(123, entity.getA().getId());
}
public static class Entity {
private String id;
private A a;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
public static class A {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
}
package com.alibaba.json.bvt;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class TypeReferenceTest8 extends TestCase {
public void test_typeRef() throws Exception {
TypeReference<Map<String, Entity>> typeRef = new TypeReference<Map<String, Entity>>() {
};
Map<String, Entity> map = JSON.parseObject(
"{\"value\":{\"id\":\"abc\",\"list\":[{\"id\":123}]}}", typeRef);
Entity entity = map.get("value");
Assert.assertNotNull(entity);
Assert.assertEquals("abc", entity.getId());
Assert.assertEquals(1, entity.getList().length);
Assert.assertEquals(123, entity.getList()[0].getId());
}
public static class Entity {
private String id;
private A[] list;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public A[] getList() {
return list;
}
public void setList(A[] list) {
this.list = list;
}
}
public static class A {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
}
package com.alibaba.json.bvt;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class TypeReferenceTest9 extends TestCase {
public void test_typeRef() throws Exception {
TypeReference<Map<String, Entity>> typeRef = new TypeReference<Map<String, Entity>>() {
};
Map<String, Entity> map = JSON
.parseObject(
"{\"value\":{\"id\":\"abc\",\"list\":[{\"id\":123,\"type\":\"A\"}]}}",
typeRef);
Entity entity = map.get("value");
Assert.assertNotNull(entity);
Assert.assertEquals("abc", entity.getId());
Assert.assertEquals(1, entity.getList().length);
Assert.assertEquals(123, entity.getList()[0].getId());
}
public static class Entity {
private String id;
private A[] list;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public A[] getList() {
return list;
}
public void setList(A[] list) {
this.list = list;
}
}
public static class A {
private int id;
private Type type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
public static enum Type {
A
}
}
......@@ -12,78 +12,81 @@ import com.alibaba.fastjson.TypeReference;
public class Bug_for_rendong extends TestCase {
public void test_0() throws Exception {
String text = "{\"BX-20110613-1739\":{\"repairNum\":\"BX-20110613-1739\",\"set\":[{\"employNum\":\"a1027\",\"isConfirm\":false,\"isReceive\":false,\"state\":11}]},\"BX-20110613-1749\":{\"repairNum\":\"BX-20110613-1749\",\"set\":[{\"employNum\":\"a1027\",\"isConfirm\":false,\"isReceive\":true,\"state\":1}]}}";
Map<String, TaskMobileStatusBean> map = JSON.parseObject(text, new TypeReference<Map<String, TaskMobileStatusBean>>() {});
Assert.assertEquals(2, map.size());
//System.out.println(JSON.toJSONString(map, SerializerFeature.PrettyFormat));
}
public void test_0() throws Exception {
String text = "{\"BX-20110613-1739\":{\"repairNum\":\"BX-20110613-1739\",\"set\":[{\"employNum\":\"a1027\",\"isConfirm\":false,\"isReceive\":false,\"state\":11}]},\"BX-20110613-1749\":{\"repairNum\":\"BX-20110613-1749\",\"set\":[{\"employNum\":\"a1027\",\"isConfirm\":false,\"isReceive\":true,\"state\":1}]}}";
public static class TaskMobileStatusBean {
Map<String, TaskMobileStatusBean> map = JSON.parseObject(text,
new TypeReference<Map<String, TaskMobileStatusBean>>() {
});
private String repairNum;
Assert.assertEquals(2, map.size());
// System.out.println(JSON.toJSONString(map,
// SerializerFeature.PrettyFormat));
}
private Set<PeopleTaskMobileStatusBean> set = new HashSet<PeopleTaskMobileStatusBean>();
public static class TaskMobileStatusBean {
public String getRepairNum() {
return repairNum;
}
private String repairNum;
public void setRepairNum(String repairNum) {
this.repairNum = repairNum;
}
private Set<PeopleTaskMobileStatusBean> set = new HashSet<PeopleTaskMobileStatusBean>();
public Set<PeopleTaskMobileStatusBean> getSet() {
return set;
}
public String getRepairNum() {
return repairNum;
}
public void setSet(Set<PeopleTaskMobileStatusBean> set) {
this.set = set;
}
public void setRepairNum(String repairNum) {
this.repairNum = repairNum;
}
}
public Set<PeopleTaskMobileStatusBean> getSet() {
return set;
}
public static class PeopleTaskMobileStatusBean {
public void setSet(Set<PeopleTaskMobileStatusBean> set) {
this.set = set;
}
private String employNum;
private Boolean isConfirm;
private Boolean isReceive;
private int state;
}
public String getEmployNum() {
return employNum;
}
public static class PeopleTaskMobileStatusBean {
public void setEmployNum(String employNum) {
this.employNum = employNum;
}
private String employNum;
private Boolean isConfirm;
private Boolean isReceive;
private int state;
public Boolean getIsConfirm() {
return isConfirm;
}
public String getEmployNum() {
return employNum;
}
public void setIsConfirm(Boolean isConfirm) {
this.isConfirm = isConfirm;
}
public void setEmployNum(String employNum) {
this.employNum = employNum;
}
public Boolean getIsReceive() {
return isReceive;
}
public Boolean getIsConfirm() {
return isConfirm;
}
public void setIsReceive(Boolean isReceive) {
this.isReceive = isReceive;
}
public void setIsConfirm(Boolean isConfirm) {
this.isConfirm = isConfirm;
}
public int getState() {
return state;
}
public Boolean getIsReceive() {
return isReceive;
}
public void setState(int state) {
this.state = state;
}
public void setIsReceive(Boolean isReceive) {
this.isReceive = isReceive;
}
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
}
package com.alibaba.json.bvt.parser.deser;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class IntegerFieldDeserializerTest2 extends TestCase {
protected void setUp() throws Exception {
// ParserConfig.getGlobalInstance().setAsmEnable(false);
}
public void test_integer() throws Exception {
String text = "{\"value\":{\"column1\":\"aa\",\"column2\":\"bb\"}}";
String text = "{\"value\":{\"column1\":\"aa\"}}";
Map<String, Entity> map = JSON.parseObject(text, new TypeReference<Map<String, Entity>>(){});
Assert.assertNotNull(map);
Assert.assertNotNull(map.get("value"));
Assert.assertNotNull("aa", map.get("value").getColumn1());
Assert.assertNotNull("bb", map.get("value").getColumn2());
}
public void f_test_integer_2() throws Exception {
String text = "[{\"value\":{\"column1\":\"aa\"}}]";
List<Map<String, Entity>> mapList = JSON.parseObject(text, new TypeReference<List<Map<String, Entity>>>(){});
Map<String, Entity> map = mapList.get(0);
Assert.assertNotNull(map);
Assert.assertNotNull(map.get("value"));
Assert.assertNotNull("aa", map.get("value").getColumn1());
}
public static class Entity implements Serializable {
private static final long serialVersionUID = 1L;
private String column1;
private String column2;
private Integer column3;
public String getColumn1() {
......@@ -35,14 +47,6 @@ public class IntegerFieldDeserializerTest2 extends TestCase {
this.column1 = column1;
}
public String getColumn2() {
return column2;
}
public void setColumn2(String column2) {
this.column2 = column2;
}
public Integer getColumn3() {
return column3;
}
......@@ -51,4 +55,8 @@ public class IntegerFieldDeserializerTest2 extends TestCase {
this.column3 = column3;
}
}
public static class Value {
}
}
package com.alibaba.json.bvt.parser.deser;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class LongFieldDeserializerTest2 extends TestCase {
protected void setUp() throws Exception {
// ParserConfig.getGlobalInstance().setAsmEnable(false);
}
public void test_integer() throws Exception {
String text = "{\"value\":{\"column1\":\"aa\"}}";
Map<String, Entity> map = JSON.parseObject(text, new TypeReference<Map<String, Entity>>(){});
Assert.assertNotNull(map);
Assert.assertNotNull(map.get("value"));
Assert.assertNotNull("aa", map.get("value").getColumn1());
}
public void test_integer_2() throws Exception {
String text = "[{\"value\":{\"column1\":\"aa\"}}]";
List<Map<String, Entity>> mapList = JSON.parseObject(text, new TypeReference<List<Map<String, Entity>>>(){});
Map<String, Entity> map = mapList.get(0);
Assert.assertNotNull(map);
Assert.assertNotNull(map.get("value"));
Assert.assertNotNull("aa", map.get("value").getColumn1());
}
public static class Entity implements Serializable {
private static final long serialVersionUID = 1L;
private String column1;
private Long column3;
public String getColumn1() {
return column1;
}
public void setColumn1(String column1) {
this.column1 = column1;
}
public Long getColumn3() {
return column3;
}
public void setColumn3(Long column3) {
this.column3 = column3;
}
}
}
package com.alibaba.json.bvt.parser.deser;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class ShortFieldDeserializerTest extends TestCase {
protected void setUp() throws Exception {
// ParserConfig.getGlobalInstance().setAsmEnable(false);
}
public void f_test_integer() throws Exception {
String text = "{\"value\":{\"column1\":\"aa\"}}";
Map<String, Entity> map = JSON.parseObject(text, new TypeReference<Map<String, Entity>>(){});
Assert.assertNotNull(map);
Assert.assertNotNull(map.get("value"));
Assert.assertNotNull("aa", map.get("value").getColumn1());
}
public void test_integer_2() throws Exception {
String text = "[{\"value\":{\"column1\":\"aa\"}}]";
List<Map<String, Entity>> mapList = JSON.parseObject(text, new TypeReference<List<Map<String, Entity>>>(){});
Map<String, Entity> map = mapList.get(0);
Assert.assertNotNull(map);
Assert.assertNotNull(map.get("value"));
Assert.assertNotNull("aa", map.get("value").getColumn1());
}
public static class Entity implements Serializable {
private static final long serialVersionUID = 1L;
private String column1;
private Short column3;
public String getColumn1() {
return column1;
}
public void setColumn1(String column1) {
this.column1 = column1;
}
public Short getColumn3() {
return column3;
}
public void setColumn3(Short column3) {
this.column3 = column3;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册