本文整理匯總了Java中com.alibaba.fastjson.serializer.JSONSerializer類的典型用法代碼示例。如果您正苦於以下問題:Java JSONSerializer類的具體用法?Java JSONSerializer怎麽用?Java JSONSerializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JSONSerializer類屬於com.alibaba.fastjson.serializer包,在下文中一共展示了JSONSerializer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: toJSONString
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
try {
JSONSerializer serializer = new JSONSerializer(out);
for (SerializerFeature feature : features) {
serializer.config(feature, true);
}
serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
setFilter(serializer, filter);
serializer.write(object);
String serializeWriter = out.toString();
return serializeWriter;
} finally {
out.close();
}
}
示例2: test_valuefilter_3
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的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);
}
示例3: test_3
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_3() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("name".equals(name)) {
return true;
}
return false;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getPropertyFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "chennp2008");
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
示例4: test_0
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_0() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
return false;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getPropertyFilters().add(filter);
A a = new A();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{}", text);
}
示例5: toJSONString
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter filter,
SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
try {
JSONSerializer serializer = new JSONSerializer(out, config);
for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
serializer.config(feature, true);
}
setFilter(serializer, filter);
serializer.write(object);
return out.toString();
} finally {
out.close();
}
}
示例6: test_direct
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_direct() throws Exception {
SerializeWriter out = new SerializeWriter(6);
out.config(SerializerFeature.QuoteFieldNames, true);
try {
JSONSerializer serializer = new JSONSerializer(out);
VO vo = new VO();
vo.setValue(123456789);
serializer.write(vo);
Assert.assertEquals("{\"value\":123456789}", out.toString());
} finally {
out.close();
}
}
示例7: test_point
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_point() throws Exception {
JSONSerializer serializer = new JSONSerializer();
Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Point.class).getClass());
Point point = new Point(3, 4);
String text = JSON.toJSONString(point, SerializerFeature.WriteClassName);
System.out.println(text);
Object obj = JSON.parse(text);
Point point2 = (Point) obj;
Assert.assertEquals(point, point2);
Point point3 = (Point) JSON.parseObject(text, Point.class);
Assert.assertEquals(point, point3);
}
示例8: test_2
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_2() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("name".equals(name)) {
return true;
}
return false;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getPropertyFilters().add(filter);
A a = new A();
a.setName("chennp2008");
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
示例9: test_namefilter_1
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter_1() throws Exception {
NameFilter filter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
return "ID";
}
return name;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 0);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"ID\":0}", text);
}
示例10: test_namefilter_1
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter_1() throws Exception {
NameFilter filter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
return "ID";
}
return name;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", true);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"ID\":true}", text);
}
示例11: test_namefilter
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter() throws Exception {
NameFilter filter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
return "ID";
}
return name;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"ID\":0.0}", text);
}
示例12: test_namefilter
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter() throws Exception {
NameFilter filter = new NameFilter() {
public String process(Object source, String name, Object value) {
if (name.equals("id")) {
Assert.assertTrue(value instanceof Character);
return "ID";
}
return name;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"ID\":\"0\"}", text);
}
示例13: test_writer_1
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_writer_1() throws Exception {
SerializeWriter out = new SerializeWriter(14);
out.config(SerializerFeature.QuoteFieldNames, true);
try {
JSONSerializer serializer = new JSONSerializer(out);
VO vo = new VO();
vo.setValue("#");
serializer.write(vo);
Assert.assertEquals("{\"value\":\"#\"}", out.toString());
} finally {
out.close();
}
}
示例14: test_2
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_2() throws Exception {
SerializeWriter out = new SerializeWriter();
ListSerializer listSerializer = new ListSerializer();
LinkedList<Object> list = new LinkedList<Object>();
for (int i = 0; i < 100000; i++) {
list.add(i);
}
long start = System.currentTimeMillis();
listSerializer.write(new JSONSerializer(out), list, null, null, 0);
long end = System.currentTimeMillis();
System.out.println("linkedList time: " + (end - start));
}
示例15: test_0
import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_0() throws Exception {
JSONSerializer serializer = new JSONSerializer();
serializer.setDateFormat("yyyy");
Assert.assertEquals("yyyy", ((SimpleDateFormat) serializer.getDateFormat()).toPattern());
Assert.assertEquals("yyyy", serializer.getDateFormatPattern());
serializer.setDateFormat("yyyy-MM");
Assert.assertEquals("yyyy-MM", ((SimpleDateFormat) serializer.getDateFormat()).toPattern());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(JSON.defaultTimeZone);
serializer.setDateFormat(format);
Assert.assertEquals("yyyy-MM-dd", serializer.getDateFormatPattern());
serializer.close();
}