use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.
the class BeforeFilterClassLevelTest_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 BeforeFilter() {
@Override
public void writeBefore(Object object) {
this.writeKeyValue("type", "A");
}
});
//
config.addFilter(//
ModelB.class, new BeforeFilter() {
@Override
public void writeBefore(Object object) {
this.writeKeyValue("type", "B");
}
});
String text2 = JSON.toJSONString(array, config);
Assert.assertEquals("[{\"type\":\"A\",\"id\":1001},{\"type\":\"B\",\"id\":1002}]", text2);
String text = JSON.toJSONString(array);
Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.
the class BeforeFilterTest method test_beforeFilter.
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));
}
use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.
the class BeforeFilterTest2 method test_beforeFilter2.
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\"}");
}
use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.
the class BeforeFilterTest2 method test_beforeFilter.
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}");
}
use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.
the class BeforeFilterTest3 method test_beforeFilter.
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}");
}
Aggregations