Search in sources :

Example 1 with AfterFilter

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

Example 2 with AfterFilter

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

Example 3 with AfterFilter

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

Example 4 with AfterFilter

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

Example 5 with AfterFilter

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

Aggregations

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