Skip to content

Commit

Permalink
bug fixed for JSONCreator. for issue #802 #1160 #1161
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 23, 2017
1 parent aa98069 commit 0f7dc97
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,26 @@ private <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName,
for (int i = 0; i < size; ++i) {
FieldInfo fieldInfo = fieldDeserializers[i].fieldInfo;
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.parser;

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 {

}
}

0 comments on commit 0f7dc97

Please sign in to comment.