Search in sources :

Example 1 with PascalNameFilter

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

the class PascalNameFilterTest method test_0.

public void test_0() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.getNameFilters().add(new PascalNameFilter());
    VO vo = new VO();
    vo.setId(123);
    vo.setName("wenshao");
    serializer.write(vo);
    Assert.assertEquals("{\"Id\":123,\"Name\":\"wenshao\"}", serializer.toString());
    serializer.close();
}
Also used : PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 2 with PascalNameFilter

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

the class NameFilterClassLevelTest_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 PascalNameFilter());
    //
    config.addFilter(//
    ModelB.class, new NameFilter() {

        @Override
        public String process(Object object, String name, Object value) {
            return name;
        }
    });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{\"Id\":1001},{\"id\":1002}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter) NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter)

Example 3 with PascalNameFilter

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

the class PascalNameFilterTest_1 method test_0.

public void test_0() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.getNameFilters().add(new PascalNameFilter());
    Map<String, Object> vo = new LinkedHashMap<String, Object>();
    vo.put("", 123);
    vo.put(null, "wenshao");
    serializer.write(vo);
    Assert.assertEquals("{\"\":123,null:\"wenshao\"}", serializer.toString());
    serializer.close();
}
Also used : PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with PascalNameFilter

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

the class NameFilterClassLevelTest method test_0.

public void test_0() throws Exception {
    Object[] array = { new ModelA(), new ModelB() };
    SerializeConfig config = new SerializeConfig();
    //
    config.addFilter(//
    ModelA.class, new PascalNameFilter());
    //
    config.addFilter(//
    ModelB.class, new NameFilter() {

        @Override
        public String process(Object object, String name, Object value) {
            return name;
        }
    });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{\"Id\":1001},{\"id\":1002}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter) NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter)

Aggregations

PascalNameFilter (com.alibaba.fastjson.serializer.PascalNameFilter)4 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)2 NameFilter (com.alibaba.fastjson.serializer.NameFilter)2 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)2 LinkedHashMap (java.util.LinkedHashMap)1