use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.
the class WriteNullStringAsEmptyTest method test_features.
public void test_features() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object object, String name, Object value) {
if (value == null && object instanceof Model && "id".equals(name)) {
return false;
}
return true;
}
};
Model model = new Model();
String json = JSON.toJSONString(model, filter, SerializerFeature.WriteNullStringAsEmpty);
System.out.println(json);
}
use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.
the class FieldSerializerTest2 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) {
if ("v".equals(name)) {
return "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.PropertyFilter 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.PropertyFilter in project fastjson by alibaba.
the class PropertyFilterClassLevelTest method test_0.
public void test_0() throws Exception {
Object[] array = { new ModelA(), new ModelB() };
SerializeConfig config = new SerializeConfig();
//
config.addFilter(//
ModelA.class, new PropertyFilter() {
@Override
public boolean apply(Object object, String name, Object value) {
return false;
}
});
//
config.addFilter(//
ModelB.class, new PropertyFilter() {
@Override
public boolean apply(Object object, String name, Object value) {
return true;
}
});
String text2 = JSON.toJSONString(array, config);
Assert.assertEquals("[{},{\"id\":1002}]", text2);
String text = JSON.toJSONString(array);
Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.
the class PropertyFilterTest method test_toJSONString.
public void test_toJSONString() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
return false;
}
};
Assert.assertEquals("{}", JSON.toJSONString(new A(), filter));
}
Aggregations