use of com.alibaba.fastjson.serializer.NameFilter in project fastjson by alibaba.
the class NameFilterTest_short_field method test_namefilter_1.
public void test_namefilter_1() throws Exception {
NameFilter filter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
Assert.assertTrue(value instanceof Short);
return "ID";
}
return name;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", (short) 0);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"ID\":0}", text);
}
use of com.alibaba.fastjson.serializer.NameFilter in project fastjson by alibaba.
the class ClassNameFilterTest method test_filter.
public void test_filter() throws Exception {
NameFilter upcaseNameFilter = new NameFilter() {
@Override
public String process(Object object, String name, Object value) {
return name.toUpperCase();
}
};
//
SerializeConfig.getGlobalInstance().addFilter(A.class, upcaseNameFilter);
Assert.assertEquals("{\"ID\":0}", JSON.toJSONString(new A()));
Assert.assertEquals("{\"id\":0}", JSON.toJSONString(new B()));
}
use of com.alibaba.fastjson.serializer.NameFilter in project fastjson by alibaba.
the class ClassNameFilterTest_private method test_filter.
public void test_filter() throws Exception {
NameFilter upcaseNameFilter = new NameFilter() {
@Override
public String process(Object object, String name, Object value) {
return name.toUpperCase();
}
};
//
SerializeConfig.getGlobalInstance().addFilter(A.class, upcaseNameFilter);
Assert.assertEquals("{\"ID\":0}", JSON.toJSONString(new A()));
Assert.assertEquals("{\"id\":0}", JSON.toJSONString(new B()));
}
use of com.alibaba.fastjson.serializer.NameFilter 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);
}
use of com.alibaba.fastjson.serializer.NameFilter in project fastjson by alibaba.
the class NameFilterTest_boolean method test_namefilter_1.
public void test_namefilter_1() throws Exception {
NameFilter filter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
return "ID";
}
return name;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", true);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"ID\":true}", text);
}
Aggregations