本文整理汇总了Java中com.alibaba.fastjson.serializer.SerializeWriter类的典型用法代码示例。如果您正苦于以下问题:Java SerializeWriter类的具体用法?Java SerializeWriter怎么用?Java SerializeWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SerializeWriter类属于com.alibaba.fastjson.serializer包,在下文中一共展示了SerializeWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toJSONString
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的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_4
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_4() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("name".equals(name)) {
return false;
}
return true;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getPropertyFilters().add(filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 3);
map.put("name", "chennp2008");
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"id\":3}", text);
}
示例3: test_valuefilter_1
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_valuefilter_1() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("name")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
Bean a = new Bean();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":false,\"name\":\"AAA\"}", text);
}
示例4: test_namefilter_1
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的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", (byte) 0);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"ID\":0}", text);
}
示例5: test_bean_3
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_bean_3() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
PO vo = TypeUtils.castToJavaBean(map, PO.class);
Assert.assertEquals(123, vo.id);
SerializeWriter out = new SerializeWriter();
try {
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", (String) null)));
serializer.write(vo);
Assert.assertEquals("{}", out.toString());
} finally {
out.close();
}
}
示例6: test_0
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的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);
}
示例7: test_namefilter_1
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的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")) {
Assert.assertTrue(value instanceof Short);
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", (short) 0);
serializer.write(map);
String text = out.toString();
Assert.assertEquals("{\"ID\":0}", text);
}
示例8: test_1
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_1() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("id".equals(name)) {
Assert.assertTrue(value instanceof Short);
return true;
}
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("{\"id\":0}", text);
}
示例9: test_0
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_0() throws Exception {
StringWriter out = new StringWriter();
SerializeWriter writer = new SerializeWriter(out, 10);
Assert.assertEquals(10, writer.getBufferLength());
for (int ch = 'a'; ch <= 'z'; ++ch) {
writer.write(ch);
}
writer.close();
String text = out.toString();
Assert.assertEquals(26, text.length());
for (int i = 0; i < 26; ++i) {
Assert.assertEquals(text.charAt(i), (char)('a' + i));
}
}
示例10: test_1
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_1() throws Exception {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("id".equals(name)) {
Assert.assertTrue(value instanceof Float);
return true;
}
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("{\"id\":0.0}", text);
}
示例11: test_0
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_0() throws Exception {
StringWriter out = new StringWriter();
SerializeWriter writer = new SerializeWriter(out, 10);
Assert.assertEquals(10, writer.getBufferLength());
for (char ch = 'a'; ch <= 'z'; ++ch) {
char[] chars = new char[] { ch, ch, ch };
writer.write(chars, 0, chars.length);
}
writer.close();
String text = out.toString();
Assert.assertEquals(26 * 3, text.length());
for (int i = 0; i < 26; ++i) {
Assert.assertEquals(text.charAt(i * 3), (char) ('a' + i));
Assert.assertEquals(text.charAt(i * 3 + 1), (char) ('a' + i));
Assert.assertEquals(text.charAt(i * 3 + 2), (char) ('a' + i));
}
}
示例12: test_namefilter_1
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的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);
}
示例13: test_valuefilter
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public void test_valuefilter() throws Exception {
ValueFilter filter = new ValueFilter() {
public Object process(Object source, String name, Object value) {
if (name.equals("id")) {
return "AAA";
}
return value;
}
};
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);
A a = new A();
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
示例14: test_2
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的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);
}
示例15: toJSONBytes
import com.alibaba.fastjson.serializer.SerializeWriter; //导入依赖的package包/类
public static final byte[] toJSONBytes(Object object, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
try {
JSONSerializer serializer = new JSONSerializer(out);
for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
serializer.config(feature, true);
}
serializer.write(object);
return out.toBytes("UTF-8");
} finally {
out.close();
}
}