Skip to content

Commit

Permalink
bug fixed for jsoncreator primive type default values. issue #802
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 5, 2016
1 parent 65e96fe commit a358331
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 2 deletions.
Expand Up @@ -602,7 +602,26 @@ protected <T> T deserialze(DefaultJSONParser parser, //
Object[] params = new Object[size];
for (int i = 0; i < size; ++i) {
FieldInfo fieldInfo = fieldInfoList[i];
params[i] = fieldValues.get(fieldInfo.name);
Object param = fieldValues.get(fieldInfo.name);
if (param == null) {
Type fieldType = fieldInfo.fieldType;
if (fieldType == byte.class) {
param = (byte) 0;
} else if (fieldType == short.class) {
param = (short) 0;
} else if (fieldType == int.class) {
param = 0;
} else if (fieldType == long.class) {
param = 0L;
} else if (fieldType == float.class) {
param = 0F;
} else if (fieldType == double.class) {
param = 0D;
} else if (fieldType == boolean.class) {
param = Boolean.FALSE;
}
}
params[i] = param;
}

if (beanInfo.creatorConstructor != null) {
Expand Down
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_boolean extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertFalse(model.id);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final boolean id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") boolean id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_byte extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertEquals(0, model.id);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final byte id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") byte id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_double extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertTrue(model.id == 0);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final double id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") double id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_float extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertTrue(model.id == 0);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final float id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") float id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_int extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertEquals(0, model.id);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final int id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") int id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_long extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertEquals(0, model.id);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final long id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") long id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser.creator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Assert;

public class JSONCreatorTest_default_short extends TestCase {

public void test_create() throws Exception {
Model model = JSON.parseObject("{\"name\":\"wenshao\"}", Model.class);
Assert.assertEquals(0, model.id);
Assert.assertEquals("wenshao", model.name);
}


public static class Model {

private final short id;
private final String name;

@JSONCreator
public Model(@JSONField(name="id") short id, @JSONField(name="name") String name) {
this.id = id;
this.name = name;
}
}

public static class Value {

}
}
2 changes: 1 addition & 1 deletion src/test/java/com/alibaba/json/bvt/ref/RefTest13.java
Expand Up @@ -45,7 +45,7 @@ public Child getChild() {
}

public String toString() {
return "Entity-" + id;
return "Model-" + id;
}
}

Expand Down

0 comments on commit a358331

Please sign in to comment.