Java Code Examples for com.alibaba.fastjson.JSON.writeJSONString()

The following are Jave code examples for showing how to use writeJSONString() of the com.alibaba.fastjson.JSON class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: GitHub   File: EnumFieldTest3.java   Source Code and License Vote up 6 votes
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);
    }
}
 
Example 2
Project: GitHub   File: StringFieldTest_special_3.java   Source Code and License Vote up 6 votes
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("&lt;", "<");
    text = text.replaceAll("&gt;", ">");

    Model model2 = JSON.parseObject(text, Model.class);
    assertEquals(model.name, model2.name);
}
 
Example 3
Project: GitHub   File: LongTest_browserCompatible.java   Source Code and License Vote up 6 votes
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]);
    }
}
 
Example 4
Project: GitHub   File: EnumFieldTest3_private.java   Source Code and License Vote up 6 votes
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);
    }
}
 
Example 5
Project: GitHub   File: FastJsonJsonView.java   Source Code and License Vote up 5 votes
@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();
}
 
Example 6
Project: GitHub   File: StringFieldTest_special_3.java   Source Code and License Vote up 5 votes
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);
}
 
Example 7
Project: GitHub   File: StringFieldTest_special_3.java   Source Code and License Vote up 5 votes
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);
}
 
Example 8
Project: GitHub   File: StringFieldTest_special_singquote.java   Source Code and License Vote up 5 votes
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);
}
 
Example 9
Project: GitHub   File: StringFieldTest_special_singquote.java   Source Code and License Vote up 5 votes
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);
}
 
Example 10
Project: GitHub   File: StringFieldTest_special_singquote.java   Source Code and License Vote up 5 votes
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);
}
 
Example 11
Project: GitHub   File: LongTest_browserCompatible.java   Source Code and License Vote up 5 votes
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]);
    }
}
 
Example 12
Project: GitHub   File: writeJSONStringToTest.java   Source Code and License Vote up 5 votes
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);
}
 
Example 13
Project: GitHub   File: FastJsonHttpMessageConverter.java   Source Code and License Vote up 4 votes
@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();
    }
}
 
Example 14
Project: GitHub   File: FastJsonProvider.java   Source Code and License Vote up 4 votes
/**
     * 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);
        }
    }
 
Example 15
Project: GitHub   File: FastjsonArrayMappingCodec.java   Source Code and License Vote up 4 votes
@Override
public void encode(OutputStream out, Object object) throws Exception {
   JSON.writeJSONString(out, object, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.BeanToArray);
}
 
Example 16
Project: GitHub   File: FastjsonCodec.java   Source Code and License Vote up 4 votes
@Override
public void encode(OutputStream out, Object object) throws Exception {
   JSON.writeJSONString(out, object, SerializerFeature.DisableCircularReferenceDetect);
}
 
Example 17
Project: GitHub   File: Issue1140.java   Source Code and License Vote up 3 votes
public void test_for_issue() throws Exception {
    String s = "\uD83C\uDDEB\uD83C\uDDF7";

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    JSON.writeJSONString(out, s);
}