Search in sources :

Example 1 with BeforeFilter

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

Example 2 with BeforeFilter

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

Example 3 with BeforeFilter

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\"}");
}
Also used : BeforeFilter(com.alibaba.fastjson.serializer.BeforeFilter)

Example 4 with BeforeFilter

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

Example 5 with BeforeFilter

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

Aggregations

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