Skip to content

Commit

Permalink
improved BeanToArray parse & add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 29, 2016
1 parent 84e4d52 commit ec6185e
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/alibaba/fastjson/parser/JSONLexer.java
Expand Up @@ -78,10 +78,10 @@ public interface JSONLexer {

float floatValue();

long scanLong(char expectNextChar);

int scanInt(char expectNext);

long scanLong(char expectNextChar);
float scanFloat(char seperator);
double scanDouble(char seperator);
boolean scanBoolean(char expectNext);

String scanString(char expectNextChar);
Expand Down
Expand Up @@ -171,6 +171,12 @@ public <T> T deserialzeArrayMapping(DefaultJSONParser parser, Type type, Object
} else if (fieldClass == boolean.class) {
boolean value = lexer.scanBoolean(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == float.class) {
float value = lexer.scanFloat(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == double.class) {
double value = lexer.scanDouble(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == java.util.Date.class && lexer.getCurrent() == '1') {
long longValue = lexer.scanLong(seperator);
fieldDeser.setValue(object, new java.util.Date(longValue));
Expand Down
@@ -1,13 +1,12 @@
package com.alibaba.json.bvt.writeAsArray;

import junit.framework.TestCase;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.json.bvt.writeAsArray.WriteAsArray_enum_public.VO;

import junit.framework.TestCase;

public class WriteAsArray_Object_2_public extends TestCase {

Expand Down
@@ -1,13 +1,12 @@
package com.alibaba.json.bvt.writeAsArray;

import junit.framework.TestCase;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.json.bvt.writeAsArray.WriteAsArray_enum_public.VO;

import junit.framework.TestCase;

public class WriteAsArray_Object_public extends TestCase {

Expand Down
@@ -0,0 +1,76 @@
package com.alibaba.json.bvt.writeAsArray;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import junit.framework.TestCase;

public class WriteAsArray_double_private extends TestCase {
public void test_0 () throws Exception {
VO vo = new VO();
vo.setId(123D);
vo.setName("wenshao");

String text = JSON.toJSONString(vo, SerializerFeature.BeanToArray);
Assert.assertEquals("[123,\"wenshao\"]", text);

VO vo2 = JSON.parseObject(text, VO.class, Feature.SupportArrayToBean);
Assert.assertTrue(vo.id == vo2.id);
Assert.assertEquals(vo.name, vo2.name);
}

public void test_error() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123.A,\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error1() throws Exception {
Exception error = null;
try {
JSON.parseObject("[\"A\",\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error2() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123:\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public static class VO {
private double id;
private String name;

public double getId() {
return id;
}

public void setId(double id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
@@ -1,12 +1,14 @@
package com.alibaba.json.bvt.writeAsArray;

import junit.framework.TestCase;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import junit.framework.TestCase;

public class WriteAsArray_double_public extends TestCase {
public void test_0 () throws Exception {
VO vo = new VO();
Expand All @@ -15,9 +17,43 @@ public void test_0 () throws Exception {

String text = JSON.toJSONString(vo, SerializerFeature.BeanToArray);
Assert.assertEquals("[123,\"wenshao\"]", text);

VO vo2 = JSON.parseObject(text, VO.class, Feature.SupportArrayToBean);
Assert.assertTrue(vo.id == vo2.id);
Assert.assertEquals(vo.name, vo2.name);
}

public void test_error() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123.A,\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error1() throws Exception {
Exception error = null;
try {
JSON.parseObject("[\"A\",\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error2() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123:\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public static class VO {
private static class VO {
private double id;
private String name;

Expand Down
@@ -0,0 +1,76 @@
package com.alibaba.json.bvt.writeAsArray;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import junit.framework.TestCase;

public class WriteAsArray_float2_private extends TestCase {
public void test_0 () throws Exception {
VO vo = new VO();
vo.setId(123F);
vo.setName("wenshao");

String text = JSON.toJSONString(vo, SerializerFeature.BeanToArray);
Assert.assertEquals("[123,\"wenshao\"]", text);

VO vo2 = JSON.parseObject(text, VO.class, Feature.SupportArrayToBean);
Assert.assertTrue(vo.id == vo2.id);
Assert.assertEquals(vo.name, vo2.name);
}

public void test_error() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123.A,\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error1() throws Exception {
Exception error = null;
try {
JSON.parseObject("[\"A\",\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error2() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123:\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

private static class VO {
private float id;
private String name;

public float getId() {
return id;
}

public void setId(float id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
@@ -0,0 +1,76 @@
package com.alibaba.json.bvt.writeAsArray;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import junit.framework.TestCase;

public class WriteAsArray_float2_public extends TestCase {
public void test_0 () throws Exception {
VO vo = new VO();
vo.setId(123F);
vo.setName("wenshao");

String text = JSON.toJSONString(vo, SerializerFeature.BeanToArray);
Assert.assertEquals("[123,\"wenshao\"]", text);

VO vo2 = JSON.parseObject(text, VO.class, Feature.SupportArrayToBean);
Assert.assertTrue(vo.id == vo2.id);
Assert.assertEquals(vo.name, vo2.name);
}

public void test_error() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123.A,\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error1() throws Exception {
Exception error = null;
try {
JSON.parseObject("[\"A\",\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public void test_error2() throws Exception {
Exception error = null;
try {
JSON.parseObject("[123:\"wenshao\"]", VO.class, Feature.SupportArrayToBean);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}

public static class VO {
private float id;
private String name;

public float getId() {
return id;
}

public void setId(float id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}

0 comments on commit ec6185e

Please sign in to comment.