private static void setFilter(JSONSerializer serializer, SerializeFilter filter) { if (filter != null) { if (filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) filter); } if (filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) filter); } if (filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) filter); } if (filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) filter); } if (filter instanceof BeforeFilter) { serializer.getBeforeFilters().add((BeforeFilter) filter); } if (filter instanceof AfterFilter) { serializer.getAfterFilters().add((AfterFilter) filter); } } }
public void test_afterFilter() throws Exception { AfterFilter filter = new AfterFilter() { @Override public void writeAfter(Object object) { this.writeKeyValue("id", 123); } }; Assert.assertEquals(JSON.toJSONString(new VO(), filter), "{\"value\":1001,\"id\":123}"); }
public void test_afterFilter2() throws Exception { AfterFilter filter = new AfterFilter() { @Override public void writeAfter(Object object) { this.writeKeyValue("id", 123); this.writeKeyValue("name", "wenshao"); } }; Assert.assertEquals(JSON.toJSONString(new VO(), filter), "{\"value\":1001,\"id\":123,\"name\":\"wenshao\"}"); }
public void test_afterFilter() throws Exception { AfterFilter filter = new AfterFilter() { @Override public void writeAfter(Object object) { this.writeKeyValue("id", 123); } }; Assert.assertEquals("{\"id\":123}",JSON.toJSONString( new VO(), filter)); }
public void test_afterFilter2() throws Exception { AfterFilter filter = new AfterFilter() { @Override public void writeAfter(Object object) { this.writeKeyValue("id", 123); this.writeKeyValue("name", "wenshao"); } }; Assert.assertEquals("{\"id\":123,\"name\":\"wenshao\"}", JSON.toJSONString(new VO(), filter)); }
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) { if (filter == null) { return; } if (filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) filter); } if (filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) filter); } if (filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) filter); } if (filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) filter); } if (filter instanceof BeforeFilter) { serializer.getBeforeFilters().add((BeforeFilter) filter); } if (filter instanceof AfterFilter) { serializer.getAfterFilters().add((AfterFilter) filter); } }
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); if (filter != null) { if (filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) filter); } if (filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) filter); } if (filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) filter); } if (filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) filter); } if (filter instanceof BeforeFilter) { serializer.getBeforeFilters().add((BeforeFilter) filter); } if (filter instanceof AfterFilter) { serializer.getAfterFilters().add((AfterFilter) filter); } } serializer.write(object); return out.toString(); } finally { out.close(); } }