use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest method test_valuefilter_2.
public void test_valuefilter_2() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", null);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"name\":\"AAA\"}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest method test_toJSONString.
public void test_toJSONString() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
Assert.assertEquals("{\"id\":\"AAA\"}", JSON.toJSONString(new A(), filter));
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field method test_valuefilter_3.
public void test_valuefilter_3() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return null;
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "AA");
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field method test_valuefilter_1.
public void test_valuefilter_1() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
A a = new A();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":0,\"name\":\"AAA\"}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field method test_toJSONString.
public void test_toJSONString() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
Assert.assertEquals("{\"id\":\"AAA\"}", JSON.toJSONString(new A(), filter));
}
Aggregations