-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support ContextValueFilter, for issue #484
- Loading branch information
Showing
9 changed files
with
295 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
core/src/main/java/com/alibaba/fastjson2/filter/BeanContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.alibaba.fastjson2.filter; | ||
|
||
import com.alibaba.fastjson2.codec.FieldInfo; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
|
||
public class BeanContext { | ||
private final Class beanClass; | ||
|
||
private final Method method; | ||
|
||
private final Field field; | ||
|
||
private final String name; | ||
|
||
private final String label; | ||
|
||
private final Class fieldClass; | ||
private final Type fieldType; | ||
|
||
private final long features; | ||
|
||
private final String format; | ||
|
||
public BeanContext( | ||
Class beanClass, | ||
Method method, | ||
Field field, | ||
String name, | ||
String label, | ||
Class fieldClass, | ||
Type fieldType, | ||
long features, | ||
String format) { | ||
this.beanClass = beanClass; | ||
this.method = method; | ||
this.field = field; | ||
this.name = name; | ||
this.label = label; | ||
this.fieldClass = fieldClass; | ||
this.fieldType = fieldType; | ||
this.features = features; | ||
this.format = format; | ||
} | ||
|
||
public Class<?> getBeanClass() { | ||
return beanClass; | ||
} | ||
|
||
public Method getMethod() { | ||
return method; | ||
} | ||
|
||
public Field getField() { | ||
return field; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
public Class<?> getFieldClass() { | ||
return fieldClass; | ||
} | ||
|
||
public Type getFieldType() { | ||
return fieldType; | ||
} | ||
|
||
public long getFeatures() { | ||
return features; | ||
} | ||
|
||
public boolean isJsonDirect() { | ||
return (features & FieldInfo.RAW_VALUE_MASK) != 0; | ||
} | ||
|
||
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { | ||
T annotatition = null; | ||
if (method != null) { | ||
annotatition = method.getAnnotation(annotationClass); | ||
} | ||
|
||
if (annotatition == null && field != null) { | ||
annotatition = field.getAnnotation(annotationClass); | ||
} | ||
|
||
return annotatition; | ||
} | ||
|
||
public String getFormat() { | ||
return format; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
core/src/main/java/com/alibaba/fastjson2/filter/ContextValueFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.alibaba.fastjson2.filter; | ||
|
||
public interface ContextValueFilter | ||
extends Filter { | ||
Object process(BeanContext context, Object object, String name, Object value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
core/src/test/java/com/alibaba/fastjson2/filter/ContextValueFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.alibaba.fastjson2.filter; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.annotation.JSONField; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ContextValueFilterTest { | ||
@Test | ||
public void test() throws Exception { | ||
Bean bean = new Bean(); | ||
bean.id = 10; | ||
|
||
AtomicReference<BeanContext> contextReference = new AtomicReference<>(); | ||
|
||
ContextValueFilter filter = (BeanContext context, Object object, String name, Object value) -> { | ||
contextReference.set(context); | ||
return value; | ||
}; | ||
|
||
JSON.toJSONString(bean, filter); | ||
|
||
BeanContext context = contextReference.get(); | ||
assertEquals(Bean.class, context.getBeanClass()); | ||
assertEquals(int.class, context.getFieldClass()); | ||
assertEquals(int.class, context.getFieldType()); | ||
assertEquals(Bean.class.getField("id"), context.getField()); | ||
assertEquals("userId", context.getAnnotation(JSONField.class).name()); | ||
assertEquals(null, context.getFormat()); | ||
assertEquals(null, context.getLabel()); | ||
assertEquals(0, context.getFeatures()); | ||
} | ||
|
||
public static class Bean { | ||
@JSONField(name = "userId") | ||
public int id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/BeanContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.alibaba.fastjson.serializer; | ||
|
||
public class BeanContext | ||
extends com.alibaba.fastjson2.filter.BeanContext { | ||
public BeanContext(com.alibaba.fastjson2.filter.BeanContext ctx) { | ||
super( | ||
ctx.getBeanClass(), | ||
ctx.getMethod(), | ||
ctx.getField(), | ||
ctx.getName(), | ||
ctx.getLabel(), | ||
ctx.getFieldClass(), | ||
ctx.getFieldType(), | ||
ctx.getFeatures(), | ||
ctx.getFormat() | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/ContextValueFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.alibaba.fastjson.serializer; | ||
|
||
public interface ContextValueFilter | ||
extends com.alibaba.fastjson2.filter.ContextValueFilter, SerializeFilter { | ||
default Object process(com.alibaba.fastjson2.filter.BeanContext context, Object object, String name, Object value) { | ||
return process(new com.alibaba.fastjson.serializer.BeanContext(context), object, name, value); | ||
} | ||
|
||
Object process(BeanContext context, Object object, String name, Object value); | ||
} |
42 changes: 42 additions & 0 deletions
42
fastjson1-compatible/src/test/java/com/alibaba/fastjson/ContextValueFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.alibaba.fastjson; | ||
|
||
import com.alibaba.fastjson.annotation.JSONField; | ||
import com.alibaba.fastjson.serializer.BeanContext; | ||
import com.alibaba.fastjson.serializer.ContextValueFilter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ContextValueFilterTest { | ||
@Test | ||
public void test() throws Exception { | ||
Bean bean = new Bean(); | ||
bean.id = 10; | ||
|
||
AtomicReference<BeanContext> contextReference = new AtomicReference<>(); | ||
|
||
ContextValueFilter filter = (BeanContext context, Object object, String name, Object value) -> { | ||
contextReference.set(context); | ||
return value; | ||
}; | ||
|
||
JSON.toJSONString(bean, filter); | ||
|
||
BeanContext context = contextReference.get(); | ||
assertEquals(Bean.class, context.getBeanClass()); | ||
assertEquals(int.class, context.getFieldClass()); | ||
assertEquals(int.class, context.getFieldType()); | ||
assertEquals(Bean.class.getField("id"), context.getField()); | ||
assertEquals("userId", context.getAnnotation(JSONField.class).name()); | ||
assertEquals(null, context.getFormat()); | ||
assertEquals(null, context.getLabel()); | ||
assertEquals(0, context.getFeatures()); | ||
} | ||
|
||
public static class Bean { | ||
@JSONField(name = "userId") | ||
public int id; | ||
} | ||
} |