use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueClassLevelTest_private method test_0.
public void test_0() throws Exception {
Object[] array = { new ModelA(), new ModelB() };
SerializeConfig config = new SerializeConfig();
//
config.addFilter(//
ModelA.class, new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
return 30001;
}
});
//
config.addFilter(//
ModelB.class, new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
return 20001;
}
});
String text2 = JSON.toJSONString(array, config);
Assert.assertEquals("[{\"id\":30001},{\"id\":20001}]", text2);
String text = JSON.toJSONString(array);
Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_IntegerKey method test_namefilter.
public void test_namefilter() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("1001")) {
return "wenshao";
}
return value;
}
};
Map map = new HashMap();
map.put(1001, 0);
String text = JSON.toJSONString(map, filter);
Assert.assertEquals("{1001:\"wenshao\"}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field_boolean 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);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":false,\"name\":\"AAA\"}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field_boolean 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 Bean(), filter));
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field_long method test_valuefilter.
public void test_valuefilter() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
Aggregations