本文整理汇总了Java中com.alibaba.fastjson.JSON.writeJSONString方法的典型用法代码示例。如果您正苦于以下问题:Java JSON.writeJSONString方法的具体用法?Java JSON.writeJSONString怎么用?Java JSON.writeJSONString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.JSON
的用法示例。
在下文中一共展示了JSON.writeJSONString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_1_writer
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_1_writer() throws Exception {
Model[] array = new Model[2048];
for (int i = 0; i < array.length; ++i) {
array[i] = new Model();
array[i].value = Type.A;
}
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, array);
String text = writer.toString();
Model[] array2 = JSON.parseObject(text, Model[].class);
Assert.assertEquals(array.length, array2.length);
for (int i = 0; i < array.length; ++i) {
Assert.assertEquals(array[i].value, array2[i].value);
}
}
示例2: test_special_browsecue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_special_browsecue() throws Exception {
Model model = new Model();
StringBuilder buf = new StringBuilder();
for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
buf.append((char) i);
}
model.name = buf.toString();
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model, SerializerFeature.BrowserSecure);
String text = writer.toString();
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
Model model2 = JSON.parseObject(text, Model.class);
assertEquals(model.name, model2.name);
}
示例3: test_array_writer_2
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_array_writer_2() throws Exception {
Random random = new Random();
long[] values = new long[2048];
for (int i = 0; i < values.length; ++i) {
values[i] = random.nextLong();
}
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, values, SerializerFeature.BrowserCompatible);
String text = writer.toString();
long[] values_2 = JSON.parseObject(text, long[].class);
Assert.assertEquals(values_2.length, values.length);
for (int i = 0; i < values.length; ++i) {
Assert.assertEquals(values[i], values_2[i]);
}
}
示例4: renderMergedOutputModel
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
protected void renderMergedOutputModel(Map<String, Object> model, //
HttpServletRequest request, //
HttpServletResponse response) throws Exception {
Object value = filterModel(model);
String jsonpParameterValue = getJsonpParameterValue(request);
if(jsonpParameterValue != null) {
JSONPObject jsonpObject = new JSONPObject(jsonpParameterValue);
jsonpObject.addParameter(value);
value = jsonpObject;
}
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
int len = JSON.writeJSONString(outnew, //
fastJsonConfig.getCharset(), //
value, //
fastJsonConfig.getSerializeConfig(), //
fastJsonConfig.getSerializeFilters(), //
fastJsonConfig.getDateFormat(), //
JSON.DEFAULT_GENERATE_FEATURE, //
fastJsonConfig.getSerializerFeatures());
if (this.updateContentLength) {
// Write content length (determined via byte array).
response.setContentLength(len);
}
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
outnew.writeTo(out);
outnew.close();
out.flush();
}
示例5: test_special
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_special() throws Exception {
Model model = new Model();
StringBuilder buf = new StringBuilder();
for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
buf.append((char) i);
}
model.name = buf.toString();
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model);
String json = writer.toString();
Model model2 = JSON.parseObject(json, Model.class);
Assert.assertEquals(model.name, model2.name);
}
示例6: test_special_browsecompatible
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_special_browsecompatible() throws Exception {
Model model = new Model();
StringBuilder buf = new StringBuilder();
for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
buf.append((char) i);
}
model.name = buf.toString();
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model, SerializerFeature.BrowserCompatible);
Model model2 = JSON.parseObject(writer.toString(), Model.class);
Assert.assertEquals(model.name, model2.name);
}
示例7: test_special
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_special() throws Exception {
Model model = new Model();
StringBuilder buf = new StringBuilder();
for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
buf.append((char) i);
}
model.name = buf.toString();
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model);
Model model2 = JSON.parseObject(writer.toString(), Model.class);
Assert.assertEquals(model.name, model2.name);
}
示例8: test_special_browsecue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_special_browsecue() throws Exception {
Model model = new Model();
StringBuilder buf = new StringBuilder();
for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
buf.append((char) i);
}
model.name = buf.toString();
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model, SerializerFeature.UseSingleQuotes);
Model model2 = JSON.parseObject(writer.toString(), Model.class);
Assert.assertEquals(model.name, model2.name);
}
示例9: test_special_browsecompatible
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_special_browsecompatible() throws Exception {
Model model = new Model();
StringBuilder buf = new StringBuilder();
for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; ++i) {
buf.append((char) i);
}
model.name = buf.toString();
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model, SerializerFeature.UseSingleQuotes);
Model model2 = JSON.parseObject(writer.toString(), Model.class);
Assert.assertEquals(model.name, model2.name);
}
示例10: test_array_writer
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_array_writer() throws Exception {
long[] values = new long[] {Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE};
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, values, SerializerFeature.BrowserCompatible);
String text = writer.toString();
long[] values_2 = JSON.parseObject(text, long[].class);
Assert.assertEquals(values_2.length, values.length);
for (int i = 0; i < values.length; ++i) {
Assert.assertEquals(values[i], values_2[i]);
}
}
示例11: test_writeJSONStringTo
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_writeJSONStringTo() throws Exception {
Model model = new Model();
model.id = 1001;
model.name = "中文名称";
ByteArrayOutputStream os = new ByteArrayOutputStream();
JSON.writeJSONString(os, model);
os.close();
byte[] bytes = os.toByteArray();
String text = new String(bytes, "UTF-8");
Assert.assertEquals("{\"id\":1001,\"name\":\"中文名称\"}", text);
}
示例12: writeInternal
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
try {
HttpHeaders headers = outputMessage.getHeaders();
//获取全局配置的filter
SerializeFilter[] globalFilters = fastJsonConfig.getSerializeFilters();
List<SerializeFilter> allFilters = new ArrayList<SerializeFilter>(Arrays.asList(globalFilters));
boolean isJsonp = false;
//不知道为什么会有这行代码, 但是为了保持和原来的行为一致,还是保留下来
Object value = strangeCodeForJackson(object);
if (value instanceof FastJsonContainer) {
FastJsonContainer fastJsonContainer = (FastJsonContainer) value;
PropertyPreFilters filters = fastJsonContainer.getFilters();
allFilters.addAll(filters.getFilters());
value = fastJsonContainer.getValue();
}
//revise 2017-10-23 ,
// 保持原有的MappingFastJsonValue对象的contentType不做修改 保持旧版兼容。
// 但是新的JSONPObject将返回标准的contentType:application/javascript ,不对是否有function进行判断
if (value instanceof MappingFastJsonValue) {
if(!StringUtils.isEmpty(((MappingFastJsonValue) value).getJsonpFunction())){
isJsonp = true;
}
} else if (value instanceof JSONPObject) {
isJsonp = true;
}
int len = JSON.writeJSONString(outnew, //
fastJsonConfig.getCharset(), //
value, //
fastJsonConfig.getSerializeConfig(), //
//fastJsonConfig.getSerializeFilters(), //
allFilters.toArray(new SerializeFilter[allFilters.size()]),
fastJsonConfig.getDateFormat(), //
JSON.DEFAULT_GENERATE_FEATURE, //
fastJsonConfig.getSerializerFeatures());
if (isJsonp) {
headers.setContentType(APPLICATION_JAVASCRIPT);
}
if (fastJsonConfig.isWriteContentLength()) {
headers.setContentLength(len);
}
outnew.writeTo(outputMessage.getBody());
} catch (JSONException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
} finally {
outnew.close();
}
}
示例13: writeTo
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
/**
* Method that JAX-RS container calls to serialize given value.
*/
public void writeTo(Object obj, //
Class<?> type, //
Type genericType, //
Annotation[] annotations, //
MediaType mediaType, //
MultivaluedMap<String, Object> httpHeaders, //
OutputStream entityStream //
) throws IOException, WebApplicationException {
FastJsonConfig fastJsonConfig = locateConfigProvider(type, mediaType);
SerializerFeature[] serializerFeatures = fastJsonConfig.getSerializerFeatures();
if (pretty) {
if (serializerFeatures == null)
serializerFeatures = new SerializerFeature[]{SerializerFeature.PrettyFormat};
else {
List<SerializerFeature> featureList = new ArrayList<SerializerFeature>(Arrays
.asList(serializerFeatures));
featureList.add(SerializerFeature.PrettyFormat);
serializerFeatures = featureList.toArray(serializerFeatures);
}
fastJsonConfig.setSerializerFeatures(serializerFeatures);
}
try {
int len = JSON.writeJSONString(entityStream, //
fastJsonConfig.getCharset(), //
obj, //
fastJsonConfig.getSerializeConfig(), //
fastJsonConfig.getSerializeFilters(), //
fastJsonConfig.getDateFormat(), //
JSON.DEFAULT_GENERATE_FEATURE, //
fastJsonConfig.getSerializerFeatures());
// // add Content-Length
// if (fastJsonConfig.isWriteContentLength()) {
// httpHeaders.add("Content-Length", String.valueOf(len));
// }
entityStream.flush();
} catch (JSONException ex) {
throw new WebApplicationException("Could not write JSON: " + ex.getMessage(), ex);
}
}
示例14: encode
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public void encode(OutputStream out, Object object) throws Exception {
JSON.writeJSONString(out, object, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.BeanToArray);
}
示例15: encode
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public void encode(OutputStream out, Object object) throws Exception {
JSON.writeJSONString(out, object, SerializerFeature.DisableCircularReferenceDetect);
}