use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class FieldSerializerTest3 method toJSONString.
public static final String toJSONString(Object object, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
try {
JSONSerializer serializer = new JSONSerializer(out);
serializer.getPropertyFilters().add(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("id".equals(name)) {
return false;
}
return true;
}
});
serializer.getNameFilters().add(new NameFilter() {
public String process(Object source, String name, Object value) {
return name;
}
});
serializer.getValueFilters().add(new ValueFilter() {
public Object process(Object source, String name, Object value) {
if ("v".endsWith(name)) {
return "xxx";
}
return value;
}
});
for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
serializer.config(feature, true);
}
serializer.write(object);
return out.toString();
} catch (StackOverflowError e) {
throw new JSONException("maybe circular references", e);
} finally {
out.close();
}
}
use of com.alibaba.fastjson.serializer.ValueFilter in project vip by guangdada.
the class FastjsonConfig method fastJsonHttpMessageConverter.
@Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.BrowserCompatible);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
ValueFilter valueFilter = new ValueFilter() {
public Object process(Object o, String s, Object o1) {
if (null == o1) {
o1 = "";
}
return o1;
}
};
fastJsonConfig.setSerializeFilters(valueFilter);
converter.setFastJsonConfig(fastJsonConfig);
return converter;
}
use of com.alibaba.fastjson.serializer.ValueFilter in project vip by guangdada.
the class FastjsonConfig method fastJsonHttpMessageConverter.
@Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.BrowserCompatible);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
ValueFilter valueFilter = new ValueFilter() {
public Object process(Object o, String s, Object o1) {
if (null == o1) {
o1 = "";
}
return o1;
}
};
fastJsonConfig.setSerializeFilters(valueFilter);
converter.setFastJsonConfig(fastJsonConfig);
return converter;
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class Bug_for_issue_572_private method test_for_issue_1.
public void test_for_issue_1() throws Exception {
Model model = new Model();
model.id = 1001;
model.name = "wenshao";
ValueFilter valueFilter = new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
if (value instanceof Number) {
return null;
}
return value;
}
};
String text = JSON.toJSONString(model, valueFilter, SerializerFeature.WriteNonStringValueAsString);
Assert.assertEquals("{\"id\":\"1001\",\"name\":\"wenshao\"}", text);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueClassLevelTest method test_0.
public void test_0() throws Exception {
Object[] array = { new ModelA(), new ModelB() };
SerializeConfig config = new SerializeConfig();
//
config.addFilter(//
ModelA.class, new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
return 30001;
}
});
//
config.addFilter(//
ModelB.class, new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
return 20001;
}
});
String text2 = JSON.toJSONString(array, config);
Assert.assertEquals("[{\"id\":30001},{\"id\":20001}]", text2);
String text = JSON.toJSONString(array);
Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Aggregations