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_beforeFilter() throws Exception { BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); } }; Assert.assertEquals(JSON.toJSONString(new VO(), filter), "{\"id\":123,\"value\":1001}"); }
public void test_beforeFilter2() throws Exception { BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); this.writeKeyValue("name", "wenshao"); } }; Assert.assertEquals(JSON.toJSONString(new VO(), filter), "{\"id\":123,\"name\":\"wenshao\",\"value\":1001}"); }
public void test_beforeFilter() throws Exception { BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); } }; Assert.assertEquals("{\"id\":123}",JSON.toJSONString( new VO(), filter)); }
public void test_beforeFilter2() throws Exception { BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); this.writeKeyValue("name", "wenshao"); } }; Assert.assertEquals("{\"id\":123,\"name\":\"wenshao\"}", JSON.toJSONString(new VO(), filter)); }
public void test_beforeFilter() throws Exception { BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); } }; Assert.assertEquals(JSON.toJSONString(new VO(), filter), "{\"id\":123}"); }
public void test_beforeFilter2() throws Exception { BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); this.writeKeyValue("name", "wenshao"); } }; Assert.assertEquals(JSON.toJSONString(new VO(), filter), "{\"id\":123,\"name\":\"wenshao\"}"); }
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(); } }