use of com.alibaba.fastjson.serializer.SerializeWriter 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.SerializeWriter 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.SerializeWriter in project fastjson by alibaba.
the class DateFormatSerializerTest method test_date_4.
public void test_date_4() throws Exception {
SerializeWriter out = new SerializeWriter();
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
SimpleDateFormat format = new SimpleDateFormat("yyyy");
format.setTimeZone(JSON.defaultTimeZone);
serializer.write(new VO(format));
Assert.assertEquals("{\"format\":\"yyyy\"}", out.toString());
JSON.parseObject(out.toString(), VO.class);
}
use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.
the class DateFormatSerializerTest method test_date_2.
public void test_date_2() throws Exception {
SerializeWriter out = new SerializeWriter();
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
serializer.config(SerializerFeature.WriteMapNullValue, true);
serializer.write(new VO());
Assert.assertEquals("{\"format\":null}", out.toString());
}
use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.
the class JSONSerializerTest1 method test_0.
public void test_0() throws Exception {
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
Assert.assertEquals(0, serializer.getNameFilters().size());
Assert.assertEquals(0, serializer.getNameFilters().size());
Assert.assertEquals(0, serializer.getValueFilters().size());
Assert.assertEquals(0, serializer.getValueFilters().size());
Assert.assertEquals(0, serializer.getPropertyFilters().size());
Assert.assertEquals(0, serializer.getPropertyFilters().size());
serializer.writeWithFormat("123", null);
}
Aggregations