use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field_long method test_toJSONString.
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));
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest_field_long method test_valuefilter_3.
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);
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class MTopFilterTest method test_0.
public void test_0() throws Exception {
Model model = new Model();
model.id = 1001;
model.name = "yongbo";
model.user = new Person();
model.user.personId = 2002;
final HashMap<String, Object> values = new HashMap<String, Object>();
ValueFilter valueFilter = new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
values.put(name, value);
return value;
}
};
String jsonString = JSON.toJSONString(model, valueFilter);
Assert.assertEquals("{\"id\":1001,\"name\":\"yongbo\",\"user\":{\"personId\":2002}}", jsonString);
Assert.assertEquals(4, values.size());
Assert.assertEquals(model.id, values.get("id"));
Assert.assertSame(model.name, values.get("name"));
Assert.assertEquals(model.user, values.get("user"));
Assert.assertEquals(model.user.personId, values.get("personId"));
}
use of com.alibaba.fastjson.serializer.ValueFilter in project fastjson by alibaba.
the class ValueFilterTest method test_valuefilter_3.
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);
}
use of com.alibaba.fastjson.serializer.ValueFilter 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();
}
}
Aggregations