Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
int/long/float/double field support empty string, bug fixed for issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 11, 2016
1 parent 2e8fd83 commit c2cc179
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, O
}

if (token != JSONToken.LBRACE && token != JSONToken.COMMA) {
if (lexer.isBlankInput()) {
return null;
}

StringBuffer buf = (new StringBuffer()) //
.append("syntax error, expect {, actual ") //
.append(lexer.tokenName()) //
Expand All @@ -204,6 +208,7 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, O
.append(", fieldName ") //
.append(fieldName);
}

throw new JSONException(buf.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ public void setValue(Object object, double value) {

@SuppressWarnings({ "rawtypes", "unchecked" })
public void setValue(Object object, Object value) {
if (value == null) {
Class<?> fieldClass = fieldInfo.fieldClass;
if (fieldClass == byte.class //
|| fieldClass == short.class //
|| fieldClass == int.class //
|| fieldClass == long.class //
|| fieldClass == float.class //
|| fieldClass == double.class //
|| fieldClass == boolean.class //
|| fieldClass == char.class //
) {
return;
}
}

final Field field = fieldInfo.field;
final Method method = fieldInfo.method;
if (fieldInfo.fieldAccess) {
Expand Down
90 changes: 90 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_479.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.alibaba.json.bvt.bug;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;

import junit.framework.TestCase;

public class Bug_for_issue_479 extends TestCase {

public void test_for_issue_blankinput() throws Exception {
VO vo = JSON.parseObject("", VO.class);
Assert.assertNull(vo);
}

public void test_for_issue() throws Exception {
VO vo = JSON.parseObject("{\"doubleParam\":\"\",\"floatParam\":\"\",\"intParam\":\"\",\"longParam\":\"\"}",
VO.class);
Assert.assertTrue(vo.doubleParam == 0);
Assert.assertTrue(vo.floatParam == 0);
Assert.assertTrue(vo.intParam == 0);
Assert.assertTrue(vo.longParam == 0);
}

public void test_for_issue_private() throws Exception {
V1 vo = JSON.parseObject("{\"doubleParam\":\"\",\"floatParam\":\"\",\"intParam\":\"\",\"longParam\":\"\"}",
V1.class);
Assert.assertTrue(vo.doubleParam == 0);
Assert.assertTrue(vo.floatParam == 0);
Assert.assertTrue(vo.intParam == 0);
Assert.assertTrue(vo.longParam == 0);
}

public static class VO {

public long doubleParam;
public float floatParam;
public int intParam;
public long longParam;
}

private static class V1 {

public long doubleParam;
public float floatParam;
public int intParam;
public long longParam;
}

public static class V2 {

private long doubleParam;
private float floatParam;
private int intParam;
private long longParam;

public long getDoubleParam() {
return doubleParam;
}

public void setDoubleParam(long doubleParam) {
this.doubleParam = doubleParam;
}

public float getFloatParam() {
return floatParam;
}

public void setFloatParam(float floatParam) {
this.floatParam = floatParam;
}

public int getIntParam() {
return intParam;
}

public void setIntParam(int intParam) {
this.intParam = intParam;
}

public long getLongParam() {
return longParam;
}

public void setLongParam(long longParam) {
this.longParam = longParam;
}

}
}

0 comments on commit c2cc179

Please sign in to comment.