本文整理汇总了Java中com.alibaba.fastjson.serializer.ValueFilter类的典型用法代码示例。如果您正苦于以下问题:Java ValueFilter类的具体用法?Java ValueFilter怎么用?Java ValueFilter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ValueFilter类属于com.alibaba.fastjson.serializer包,在下文中一共展示了ValueFilter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_valuefilter
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
A a = new A();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
示例2: test_valuefilter_1
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter_1() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
A a = new A();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":0,\"name\":\"AAA\"}", text);
}
示例3: test_valuefilter_2
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter_2() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", null);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"name\":\"AAA\"}", text);
}
示例4: test_valuefilter_3
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter_3() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return null;
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "AA");
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{}", text);
}
示例5: test_valuefilter
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
示例6: test_valuefilter_1
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter_1() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":false,\"name\":\"AAA\"}", text);
}
示例7: test_valuefilter_1
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_valuefilter_1() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":0,\"name\":\"AAA\"}", text);
}
示例8: test_enum
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_enum() throws Exception {
Model model = new Model();
model.t1 = Type.A;
model.t2 = null;
ValueFilter valueFilter = new ValueFilter() {
public Object process(Object object, String name, Object value) {
return value;
}
};
SerializeFilter[] filters = {valueFilter};
String text = JSON.toJSONString(model, SerializeConfig.getGlobalInstance(), //
filters,
null,
0, //
SerializerFeature.QuoteFieldNames, //
SerializerFeature.BrowserCompatible, //
SerializerFeature.WriteEnumUsingName);
Assert.assertEquals("{\"t1\":\"A\"}", text);
}
示例9: fastJsonHttpMessageConverter
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
private FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
SerializerFeature.WriteEnumUsingToString,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
fastJsonConfig.setSerializeFilters(new ValueFilter() {
@Override
public Object process(Object o, String s, Object source) {
if (source == null) {
return "";
}
if (source instanceof Date) {
return ((Date) source).getTime();
}
return source;
}
});
httpMessageConverter.setFastJsonConfig(fastJsonConfig);
return httpMessageConverter;
}
示例10: setFilter
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) {
if (filter != null) {
if (filter instanceof PropertyPreFilter) {
serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
}
if (filter instanceof NameFilter) {
serializer.getNameFilters().add((NameFilter) filter);
}
if (filter instanceof ValueFilter) {
serializer.getValueFilters().add((ValueFilter) filter);
}
if (filter instanceof PropertyFilter) {
serializer.getPropertyFilters().add((PropertyFilter) filter);
}
if (filter instanceof BeforeFilter) {
serializer.getBeforeFilters().add((BeforeFilter) filter);
}
if (filter instanceof AfterFilter) {
serializer.getAfterFilters().add((AfterFilter) filter);
}
}
}
示例11: createFirstValueFilter
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
private ValueFilter createFirstValueFilter() {
return new ValueFilter() {
@Override
public Object process(Object source, String name, Object value) {
if (!(value instanceof String)) return value;
String strValue = (String) value;
if (bytesLen(strValue) <= 3) return value;
String mappedValue = innerValueMapping.get(strValue);
if (mappedValue != null) return mappedValue;
valueBag.add(strValue);
return value;
}
};
}
示例12: createSecondValueFilter
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public ValueFilter createSecondValueFilter() {
// 第一遍过滤器,如果启动了值映射分析,则还需要第二个过滤器,在第一次过滤器中进行值映射分析。
return new ValueFilter() {
@Override
public Object process(Object source, String name, Object value) {
if (!(value instanceof String)) return value;
val key = (String) value;
if (bytesLen(key) <= 3) return key;
String valueCode = innerValueMapping.get(key);
if (valueCode != null) return '@' + valueCode;
if (valueBag.count(key) <= 2) return value;
valueCode = BaseX.base62(innerValueMapping.size());
innerValueMapping.put(key, valueCode);
return '@' + valueCode;
}
};
}
示例13: test_toJSONString
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_toJSONString() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
Assert.assertEquals("{\"id\":\"AAA\"}", JSON.toJSONString(new A(), filter));
}
示例14: test_toJSONString
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_toJSONString() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
Assert.assertEquals("{\"id\":\"AAA\"}", JSON.toJSONString(new Bean(), filter));
}
示例15: test_enum
import com.alibaba.fastjson.serializer.ValueFilter; //导入依赖的package包/类
public void test_enum() throws Exception {
Model model = new Model();
model.t1 = Type.A;
model.t2 = null;
ValueFilter filter = new ValueFilter() {
public Object process(Object object, String name, Object value) {
return null;
}
};
String text = JSON.toJSONString(model, filter, SerializerFeature.WriteMapNullValue);
Assert.assertEquals("{\"t1\":null,\"t2\":null}", text);
}