Skip to content

Commit

Permalink
improved fastjson 1.x compatible, support Feature.IgnoreNoneSerializa…
Browse files Browse the repository at this point in the history
…ble. for issue #733
  • Loading branch information
wenshao committed Sep 5, 2022
1 parent f5f5d9b commit 787e24a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
10 changes: 8 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public boolean hasFilter() {
|| context.afterFilter != null
|| context.labelFilter != null
|| context.contextValueFilter != null
|| context.contextNameFilter != null;
|| context.contextNameFilter != null
|| (context.features & JSONWriter.Feature.IgnoreNonFieldGetter.mask) != 0;
}

public boolean isWriteNulls() {
Expand Down Expand Up @@ -1580,7 +1581,12 @@ public enum Feature {
/**
* @since 2.0.13
*/
WriteByteArrayAsBase64(1L << 31);
WriteByteArrayAsBase64(1L << 31),

/**
* @since 2.0.13
*/
IgnoreNonFieldGetter(1L << 32);

public final long mask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.alibaba.fastjson2.util.Fnv;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -368,6 +369,7 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
}

JSONWriter.Context context = jsonWriter.getContext();
boolean ignoreNonFieldGetter = ((context.getFeatures() | features) & JSONWriter.Feature.IgnoreNonFieldGetter.mask) != 0;

BeforeFilter beforeFilter = context.getBeforeFilter();
if (beforeFilter != null) {
Expand All @@ -386,6 +388,11 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
for (int i = 0, size = fieldWriters.size(); i < size; ++i) {
FieldWriter fieldWriter = fieldWriters.get(i);

Field field = fieldWriter.getField();
if (ignoreNonFieldGetter && fieldWriter.getMethod() != null && field == null) {
continue;
}

// pre property filter
final String fieldWriterFieldName = fieldWriter.getFieldName();
if (propertyPreFilter != null
Expand Down Expand Up @@ -425,7 +432,7 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
beanContext = new BeanContext(
objectType,
fieldWriter.getMethod(),
fieldWriter.getField(),
field,
fieldWriter.getFieldName(),
fieldWriter.getLabel(),
fieldWriter.getFieldClass(),
Expand Down Expand Up @@ -453,7 +460,7 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
beanContext = new BeanContext(
objectType,
fieldWriter.getMethod(),
fieldWriter.getField(),
field,
fieldWriter.getFieldName(),
fieldWriter.getLabel(),
fieldWriter.getFieldClass(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.fastjson2.features;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class IgnoreNonFieldGetterTest {
@Test
public void test() {
Bean bean = new Bean();
assertEquals("{\"id\":123}", JSON.toJSONString(bean));
assertEquals("{}", JSON.toJSONString(bean, JSONWriter.Feature.IgnoreNonFieldGetter));
}

public static class Bean {
public int getId() {
return 123;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,9 @@ static void config(JSONWriter.Context context, SerializerFeature feature) {
case WriteNonStringKeyAsString:
context.config(JSONWriter.Feature.WriteNonStringKeyAsString);
break;
case IgnoreNonFieldGetter:
context.config(JSONWriter.Feature.IgnoreNonFieldGetter);
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.fastjson.features;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class IgnoreNonFieldGetterTest {
@Test
public void test() {
Bean bean = new Bean();
assertEquals("{\"id\":123}", JSON.toJSONString(bean));
assertEquals("{}", JSON.toJSONString(bean, SerializerFeature.IgnoreNonFieldGetter));
}

public static class Bean {
public int getId() {
return 123;
}
}
}

0 comments on commit 787e24a

Please sign in to comment.