use of com.alibaba.fastjson.serializer.SerializerFeature 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.SerializerFeature 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.SerializerFeature in project fastjson by alibaba.
the class ByteArrayTest method test_bytes.
public void test_bytes() throws Exception {
VO vo = new VO();
SerializeConfig mapping = new SerializeConfig();
mapping.setAsmEnable(false);
SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty };
String text1 = JSON.toJSONString(vo, mapping, features);
Assert.assertEquals("{\"value\":[]}", text1);
String text2 = JSON.toJSONString(vo, features);
Assert.assertEquals("{\"value\":[]}", text2);
}
use of com.alibaba.fastjson.serializer.SerializerFeature in project fastjson by alibaba.
the class EnumTest2 method test_enum_noasm.
public void test_enum_noasm() throws Exception {
SerializeConfig mapping = new SerializeConfig();
mapping.setAsmEnable(false);
Date date = new Date(1308841916550L);
// 1308841916550
Assert.assertEquals("1308841916550", JSON.toJSONString(date, mapping));
// "2011-06-23T23:11:56.550"
Assert.assertEquals("\"2011-06-23T23:11:56.550+08:00\"", JSON.toJSONString(date, mapping, SerializerFeature.UseISO8601DateFormat));
SerializerFeature[] features = { SerializerFeature.UseISO8601DateFormat, SerializerFeature.UseSingleQuotes };
// '2011-06-23T23:11:56.550'
Assert.assertEquals("'2011-06-23T23:11:56.550+08:00'", JSON.toJSONString(date, mapping, features));
}
use of com.alibaba.fastjson.serializer.SerializerFeature in project fastjson by alibaba.
the class ByteArrayTest method test_bytes_1.
public void test_bytes_1() throws Exception {
VO vo = new VO();
vo.setValue(new byte[] { 1, 2, 3 });
SerializeConfig mapping = new SerializeConfig();
mapping.setAsmEnable(false);
SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty };
String text1 = JSON.toJSONString(vo, mapping, features);
Assert.assertEquals("{\"value\":\"AQID\"}", text1);
String text2 = JSON.toJSONString(vo, features);
Assert.assertEquals("{\"value\":\"AQID\"}", text2);
}
Aggregations