use of com.alibaba.fastjson.serializer.AfterFilter in project fastjson by alibaba.
the class AfterFilterTest2 method test_afterFilter2.
public void test_afterFilter2() throws Exception {
AfterFilter filter = new AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("id", 123);
this.writeKeyValue("name", "wenshao");
}
};
Assert.assertEquals("{\"id\":123,\"name\":\"wenshao\"}", JSON.toJSONString(new VO(), filter));
}
use of com.alibaba.fastjson.serializer.AfterFilter in project fastjson by alibaba.
the class AfterFilterClassLevelTest method test_0.
public void test_0() throws Exception {
Object[] array = { new ModelA(), new ModelB() };
SerializeConfig config = new SerializeConfig();
//
config.addFilter(//
ModelA.class, new AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("type", "A");
}
});
//
config.addFilter(//
ModelB.class, new AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("type", "B");
}
});
String text2 = JSON.toJSONString(array, config);
Assert.assertEquals("[{\"id\":1001,\"type\":\"A\"},{\"id\":1002,\"type\":\"B\"}]", text2);
String text = JSON.toJSONString(array);
Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
use of com.alibaba.fastjson.serializer.AfterFilter in project fastjson by alibaba.
the class AfterFilterClassLevelTest_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 AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("type", "A");
}
});
//
config.addFilter(//
ModelB.class, new AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("type", "B");
}
});
String text2 = JSON.toJSONString(array, config);
Assert.assertEquals("[{\"id\":1001,\"type\":\"A\"},{\"id\":1002,\"type\":\"B\"}]", text2);
String text = JSON.toJSONString(array);
Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
use of com.alibaba.fastjson.serializer.AfterFilter in project fastjson by alibaba.
the class AfterFilterTest method test_afterFilter.
public void test_afterFilter() throws Exception {
AfterFilter filter = new AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("id", 123);
}
};
Assert.assertEquals("{\"id\":123}", JSON.toJSONString(new VO(), filter));
}
use of com.alibaba.fastjson.serializer.AfterFilter in project fastjson by alibaba.
the class AfterFilterTest method test_afterFilter2.
public void test_afterFilter2() throws Exception {
AfterFilter filter = new AfterFilter() {
@Override
public void writeAfter(Object object) {
this.writeKeyValue("id", 123);
this.writeKeyValue("name", "wenshao");
}
};
Assert.assertEquals("{\"id\":123,\"name\":\"wenshao\"}", JSON.toJSONString(new VO(), filter));
}
Aggregations