Search in sources :

Example 6 with BeforeFilter

use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.

the class BeforeFilterClassLevelTest 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);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) BeforeFilter(com.alibaba.fastjson.serializer.BeforeFilter)

Example 7 with BeforeFilter

use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.

the class BeforeFilterTest 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("{\"id\":123,\"name\":\"wenshao\"}", JSON.toJSONString(new VO(), filter));
}
Also used : BeforeFilter(com.alibaba.fastjson.serializer.BeforeFilter)

Example 8 with BeforeFilter

use of com.alibaba.fastjson.serializer.BeforeFilter in project fastjson by alibaba.

the class BeforeFilterTest3 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\",\"value\":1001}");
}
Also used : BeforeFilter(com.alibaba.fastjson.serializer.BeforeFilter)

Aggregations

BeforeFilter (com.alibaba.fastjson.serializer.BeforeFilter)8 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)2