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

Commit

Permalink
bug fixed for issue #1647 & #1635
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 1, 2018
1 parent 5501d48 commit 4a32a0e
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/alibaba/fastjson/annotation/JSONType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

Expand Down Expand Up @@ -39,4 +40,10 @@
* @since 1.2.11 backport to 1.1.52.android
*/
Class<?>[] seeAlso() default{};

/**
*
* @return
*/
PropertyNamingStrategy naming() default PropertyNamingStrategy.CamelCase;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
Expand Down Expand Up @@ -128,6 +122,13 @@ public JavaBeanSerializer(Class<?> clazz, //
typeKey = null;
}
}

if (propertyNamingStrategy == null) {
PropertyNamingStrategy typeNaming = jsonType.naming();
if (typeNaming != PropertyNamingStrategy.CamelCase) {
propertyNamingStrategy = typeNaming;
}
}
}
this.typeName = typeName;
this.typeKey = typeKey;
Expand Down Expand Up @@ -430,9 +431,42 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
}
}

if (propertyValueGot && propertyValue == null && !writeAsArray) {
if ((!fieldSerializer.writeNull)
&& ((features | out.features) & SerializerFeature.WriteMapNullValue.mask) == 0) {
if (propertyValueGot && propertyValue == null) {
int serialzeFeatures = fieldInfo.serialzeFeatures | features | out.features;
// beanInfo.jsonType
if (fieldClass == Boolean.class) {
int defaultMask = SerializerFeature.WriteNullBooleanAsFalse.mask;
final int mask = defaultMask | SerializerFeature.WriteMapNullValue.mask;
if ((!writeAsArray) && (serialzeFeatures & mask) == 0 && (out.features & mask) == 0) {
continue;
} else if ((serialzeFeatures & defaultMask) != 0 || (out.features & defaultMask) != 0) {
propertyValue = false;
}
} else if (fieldClass == String.class) {
int defaultMask = SerializerFeature.WriteNullStringAsEmpty.mask;
final int mask = defaultMask | SerializerFeature.WriteMapNullValue.mask;
if ((!writeAsArray) && (serialzeFeatures & mask) == 0 && (out.features & mask) == 0) {
continue;
} else if ((serialzeFeatures & defaultMask) != 0 || (out.features & defaultMask) != 0) {
propertyValue = "";
}
} else if (Number.class.isAssignableFrom(fieldClass)) {
int defaultMask = SerializerFeature.WriteNullNumberAsZero.mask;
final int mask = defaultMask | SerializerFeature.WriteMapNullValue.mask;
if ((!writeAsArray) && (serialzeFeatures & mask) == 0 && (out.features & mask) == 0) {
continue;
} else if ((serialzeFeatures & defaultMask) != 0 || (out.features & defaultMask) != 0) {
propertyValue = 0;
}
} else if (Collection.class.isAssignableFrom(fieldClass)) {
int defaultMask = SerializerFeature.WriteNullListAsEmpty.mask;
final int mask = defaultMask | SerializerFeature.WriteMapNullValue.mask;
if ((!writeAsArray) && (serialzeFeatures & mask) == 0 && (out.features & mask) == 0) {
continue;
} else if ((serialzeFeatures & defaultMask) != 0 || (out.features & defaultMask) != 0) {
propertyValue = Collections.emptyList();
}
} else if ((!writeAsArray) && (!fieldSerializer.writeNull) && !out.isEnabled(SerializerFeature.WriteMapNullValue)){
continue;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class FieldInfo implements Comparable<FieldInfo> {

public final String[] alternateNames;

public final int serialzeFeatures;

public FieldInfo(String name, //
Class<?> declaringClass, //
Class<?> fieldClass, //
Expand All @@ -55,6 +57,7 @@ public FieldInfo(String name, //
this.method = null;
this.field = field;
this.ordinal = ordinal;
this.serialzeFeatures = serialzeFeatures;

isEnum = fieldClass.isEnum() && !JSONAware.class.isAssignableFrom(fieldClass);

Expand Down Expand Up @@ -99,6 +102,7 @@ public FieldInfo(String name, //
this.ordinal = ordinal;
this.methodAnnotation = methodAnnotation;
this.fieldAnnotation = fieldAnnotation;
this.serialzeFeatures = serialzeFeatures;

JSONField annotation = getAnnotation();
String format = null;
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1600/Issue1635.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.alibaba.json.bvt.issue_1600;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;

import java.util.List;

public class Issue1635 extends TestCase {
public static class Foo {
public String name;
public Integer BarCount;
public Boolean flag;
public List list;

public Foo(String name, Integer barCount) {
this.name = name;
BarCount = barCount;
}
}

public void test_issue() throws Exception {
SerializeConfig config = new SerializeConfig();
Foo foo = new Foo(null, null);
String json = JSON.toJSONString(foo
, config, new PascalNameFilter()
, SerializerFeature.WriteNullBooleanAsFalse
, SerializerFeature.WriteNullNumberAsZero
, SerializerFeature.WriteNullStringAsEmpty
, SerializerFeature.WriteNullListAsEmpty
);
assertEquals("{\"BarCount\":0,\"Flag\":false,\"List\":[],\"Name\":\"\"}", json);
}

public void test_issue_1() throws Exception {
SerializeConfig config = new SerializeConfig();
Foo foo = new Foo(null, null);
String json = JSON.toJSONString(foo
, config, new PascalNameFilter()
, SerializerFeature.WriteNullBooleanAsFalse
, SerializerFeature.WriteNullNumberAsZero
, SerializerFeature.WriteNullStringAsEmpty
, SerializerFeature.WriteNullListAsEmpty
, SerializerFeature.BeanToArray
);
assertEquals("[0,false,[],\"\"]", json);
}

public class PascalNameFilter implements NameFilter {

public String process(Object source, String name, Object value) {
if (name == null || name.length() == 0) {
return name;
}

char[] chars = name.toCharArray();
chars[0]= Character.toUpperCase(chars[0]);

String pascalName = new String(chars);
return pascalName;
}
}
}
50 changes: 50 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1600/Issue1647.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.alibaba.json.bvt.issue_1600;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONType;
import junit.framework.TestCase;

import java.util.Arrays;
import java.util.List;

public class Issue1647 extends TestCase {
public void test_for_issue() throws Exception {
Params params = new Params()
.setVerificationIds(Arrays.asList(new String[]{"a", "b"}))
.setWithFields(true);

String json = JSON.toJSONString(params);
System.out.println(json);
params = JSON.parseObject(json, Params.class);
assertEquals("{\"verification_ids\":[\"a\",\"b\"],\"with_fields\":true}", JSON.toJSONString(params));
}

@JSONType(naming = PropertyNamingStrategy.SnakeCase)
public static class Params {

private boolean withFields;

private List<String> verificationIds;

public boolean isWithFields() {
return withFields;
}

public Params setWithFields(boolean withFields) {
this.withFields = withFields;
return this;
}

public List<String> getVerificationIds() {
return verificationIds;
}

public Params setVerificationIds(List<String> verificationIds) {
this.verificationIds = verificationIds;
return this;
}
}


}

0 comments on commit 4a32a0e

Please sign in to comment.