-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support export path, improved fastjson 1.x compatible, for issue #2590
- Loading branch information
Showing
6 changed files
with
205 additions
and
1 deletion.
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
77 changes: 77 additions & 0 deletions
77
core/src/test/java/com/alibaba/fastjson2/issues_2500/Issue2590.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,77 @@ | ||
package com.alibaba.fastjson2.issues_2500; | ||
|
||
import com.alibaba.fastjson.serializer.JSONSerializer; | ||
import com.alibaba.fastjson.serializer.SerialContext; | ||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import com.alibaba.fastjson2.filter.PropertyPreFilter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Issue2590 { | ||
@Test | ||
public void test() { | ||
Bean bean = new Bean(); | ||
bean.item0 = new Item(101); | ||
bean.item1 = new Item(102); | ||
bean.item2 = new Item(103); | ||
|
||
FilterDemo filter = new FilterDemo(); | ||
String json = JSON.toJSONString(bean, filter, JSONWriter.Feature.ReferenceDetection); | ||
assertEquals("{\"item0\":{\"id\":101},\"item1\":{\"id\":102},\"item2\":{}}", json); | ||
} | ||
|
||
public static class FilterDemo | ||
implements PropertyPreFilter { | ||
@Override | ||
public boolean process(JSONWriter writer, Object source, String name) { | ||
String parentPath = writer.getPath(); | ||
if (parentPath.startsWith("$.item2")) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
Bean bean = new Bean(); | ||
bean.item0 = new Item(101); | ||
bean.item1 = new Item(102); | ||
bean.item2 = new Item(103); | ||
|
||
FilterDemo1 filter = new FilterDemo1(); | ||
String json = com.alibaba.fastjson.JSON.toJSONString(bean, filter); | ||
assertEquals("{\"item0\":{\"id\":101},\"item1\":{\"id\":102},\"item2\":{}}", json); | ||
} | ||
|
||
public static class FilterDemo1 | ||
implements com.alibaba.fastjson.serializer.PropertyPreFilter { | ||
@Override | ||
public boolean apply(JSONSerializer serializer, Object object, String name) { | ||
SerialContext context = serializer.getContext(); | ||
if (context.toString().startsWith("$.item2")) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
public static class Bean { | ||
public Item item0; | ||
public Item item1; | ||
public Item item2; | ||
} | ||
|
||
public static class Item { | ||
public int id; | ||
|
||
public Item() { | ||
} | ||
|
||
public Item(int id) { | ||
this.id = 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
11 changes: 10 additions & 1 deletion
11
fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/PropertyPreFilter.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 |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package com.alibaba.fastjson.serializer; | ||
|
||
import com.alibaba.fastjson2.JSONWriter; | ||
|
||
public interface PropertyPreFilter | ||
extends SerializeFilter { | ||
extends com.alibaba.fastjson2.filter.PropertyPreFilter, SerializeFilter { | ||
default boolean process(JSONWriter writer, Object source, String name) { | ||
JSONSerializer serializer = JSONSerializer.getJSONSerializer(writer); | ||
serializer.setContext(new SerialContext(writer, serializer.context, source, name, 0, 0)); | ||
return apply(serializer, source, name); | ||
} | ||
|
||
boolean apply(JSONSerializer serializer, Object object, String name); | ||
} |
27 changes: 27 additions & 0 deletions
27
fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/SerialContext.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 |
---|---|---|
@@ -1,15 +1,42 @@ | ||
package com.alibaba.fastjson.serializer; | ||
|
||
import com.alibaba.fastjson2.JSONWriter; | ||
|
||
public class SerialContext { | ||
public final SerialContext parent; | ||
public final Object object; | ||
public final Object fieldName; | ||
public final int features; | ||
JSONWriter jsonWriter; | ||
|
||
SerialContext(JSONWriter jsonWriter, SerialContext parent, Object object, Object fieldName, int features, int fieldFeatures) { | ||
this.parent = parent; | ||
this.jsonWriter = jsonWriter; | ||
this.object = object; | ||
this.fieldName = fieldName; | ||
this.features = features; | ||
} | ||
|
||
public SerialContext(SerialContext parent, Object object, Object fieldName, int features, int fieldFeatures) { | ||
this.parent = parent; | ||
this.object = object; | ||
this.fieldName = fieldName; | ||
this.features = features; | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
public String getPath() { | ||
return toString(); | ||
} | ||
|
||
public String toString() { | ||
String path = null; | ||
if (jsonWriter != null) { | ||
path = jsonWriter.getPath(); | ||
} | ||
|
||
return path; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2590.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,52 @@ | ||
package com.alibaba.fastjson.v2issues; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.serializer.JSONSerializer; | ||
import com.alibaba.fastjson.serializer.PropertyPreFilter; | ||
import com.alibaba.fastjson.serializer.SerialContext; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Issue2590 { | ||
@Test | ||
public void test() { | ||
Bean bean = new Bean(); | ||
bean.item0 = new Item(101); | ||
bean.item1 = new Item(102); | ||
bean.item2 = new Item(103); | ||
|
||
FilterDemo filter = new FilterDemo(); | ||
String json = JSON.toJSONString(bean, filter); | ||
assertEquals("{\"item0\":{\"id\":101},\"item1\":{\"id\":102},\"item2\":{}}", json); | ||
} | ||
|
||
public static class FilterDemo | ||
implements PropertyPreFilter { | ||
@Override | ||
public boolean apply(JSONSerializer serializer, Object object, String name) { | ||
SerialContext context = serializer.getContext(); | ||
if (context.toString().startsWith("$.item2")) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
public static class Bean { | ||
public Item item0; | ||
public Item item1; | ||
public Item item2; | ||
} | ||
|
||
public static class Item { | ||
public int id; | ||
|
||
public Item() { | ||
} | ||
|
||
public Item(int id) { | ||
this.id = id; | ||
} | ||
} | ||
} |