本文整理汇总了Java中com.alibaba.fastjson.serializer.JSONSerializer.write方法的典型用法代码示例。如果您正苦于以下问题:Java JSONSerializer.write方法的具体用法?Java JSONSerializer.write怎么用?Java JSONSerializer.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.serializer.JSONSerializer
的用法示例。
在下文中一共展示了JSONSerializer.write方法的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_3_s
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
public void test_3_s() throws Exception {
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
StringBuilder sb = new StringBuilder();
sb.append('[');
int len = 1000;
int[] array = new int[len];
for (int i = 0; i < array.length; ++i) {
array[i] = i;
if (i != 0) {
sb.append(',');
}
sb.append(i);
}
sb.append(']');
serializer.write(array);
Assert.assertEquals(sb.toString(), out.toString());
}
示例3: test_valuefilter_1
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的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_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.name = "chennp2008";
serializer.write(a);
String text = out.toString();
Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
示例5: 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);
}
示例6: test_1
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的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 Boolean);
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\":false}", text);
}
示例7: 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);
}
示例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_7
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
public void test_7() throws Exception {
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.config(SerializerFeature.UseISO8601DateFormat, true);
Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.UseISO8601DateFormat));
serializer.write(new Date(1294506000000L));
Assert.assertEquals("\"2011-01-09T01:00:00+08:00\"", out.toString());
}
示例10: ser
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void ser(Blackhole bh) throws IOException {
for (int i = 0; i < 1000; i++) {
byteArrayOutputStream.reset();
SerializeWriter writer = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(writer, serializeConfig);
serializer.write(testObject);
writer.writeToEx(byteArrayOutputStream, IOUtils.UTF8);
serializer.close();
bh.consume(byteArrayOutputStream);
}
}
示例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 (value != null) {
Assert.assertTrue(value instanceof Byte);
}
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}", text);
}
示例12: test_2
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
public void test_2() throws Exception {
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.config(SerializerFeature.UseISO8601DateFormat, true);
Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.UseISO8601DateFormat));
serializer.write(new Date(1294552193000L));
Assert.assertEquals("\"2011-01-09T13:49:53+08:00\"", out.toString());
}
示例13: test_0
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
public void test_0() throws Exception {
JSONSerializer serializer = new JSONSerializer();
serializer.write(new C());
Assert.assertEquals("[]", serializer.getWriter().toString());
}
示例14: write
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
serializer.write(((ResultCode) object).value);
}
示例15: test_1_s
import com.alibaba.fastjson.serializer.JSONSerializer; //导入方法依赖的package包/类
public void test_1_s() throws Exception {
JSONSerializer serializer = new JSONSerializer();
serializer.write(Collections.singletonList(1));
Assert.assertEquals("[1]", serializer.getWriter().toString());
}