use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class EnumFieldsTest7 method test_enum.
public void test_enum() throws Exception {
Model model = new Model();
model.t1 = Type.A;
model.t2 = null;
ValueFilter filter = new ValueFilter() {
public Object process(Object object, String name, Object value) {
return null;
}
};
String text = JSON.toJSONString(model, filter, SerializerFeature.WriteMapNullValue);
Assert.assertEquals("{\"t1\":null,\"t2\":null}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class EnumFieldsTest8 method test_enum.
public void test_enum() throws Exception {
Model model = new Model();
model.t1 = Type.A;
model.t2 = null;
ValueFilter valueFilter = new ValueFilter() {
public Object process(Object object, String name, Object value) {
return value;
}
};
SerializeFilter[] filters = { valueFilter };
String text = //
JSON.toJSONString(//
model, //
SerializeConfig.getGlobalInstance(), filters, null, //
0, //
SerializerFeature.QuoteFieldNames, //
SerializerFeature.BrowserCompatible, SerializerFeature.WriteEnumUsingName);
Assert.assertEquals("{\"t1\":\"A\"}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class FilterDemo method test_secure.
public void test_secure() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "WSJ";
}
return value;
}
};
NameFilter nameFilter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
return "ID";
}
return name;
}
};
String text = "{\"id\":123,\"name\":\"WJH\"}";
Object object = JSON.parse(text);
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
serializer.getNameFilters().add(nameFilter);
serializer.write(object);
String outText = out.toString();
System.out.println(outText);
}
Aggregations