Search in sources :

Example 1 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project dubbo by alibaba.

the class FastJsonObjectOutput method writeObject.

public void writeObject(Object obj) throws IOException {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.config(SerializerFeature.WriteEnumUsingToString, true);
    serializer.write(obj);
    out.writeTo(writer);
    writer.println();
    writer.flush();
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 2 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class JSON method writeJSONString.

public static final // 
int writeJSONString(// 
OutputStream os, // 
Charset charset, // 
Object object, //
SerializeConfig config, //
SerializeFilter[] filters, //
String dateFormat, //
int defaultFeatures, SerializerFeature... features) throws IOException {
    SerializeWriter writer = new SerializeWriter(null, defaultFeatures, features);
    try {
        JSONSerializer serializer = new JSONSerializer(writer, config);
        if (dateFormat != null && dateFormat.length() != 0) {
            serializer.setDateFormat(dateFormat);
            serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
        }
        if (filters != null) {
            for (SerializeFilter filter : filters) {
                serializer.addFilter(filter);
            }
        }
        serializer.write(object);
        int len = writer.writeToEx(os, charset);
        return len;
    } finally {
        writer.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 3 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class JSON method writeJSONString.

public void writeJSONString(Appendable appendable) {
    SerializeWriter out = new SerializeWriter();
    try {
        new JSONSerializer(out).write(this);
        appendable.append(out.toString());
    } catch (IOException e) {
        throw new JSONException(e.getMessage(), e);
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) IOException(java.io.IOException) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 4 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class JSON method toJSONString.

/**
     * @since 1.2.11
     */
public static String toJSONString(Object object, int defaultFeatures, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter((Writer) null, defaultFeatures, features);
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.write(object);
        return out.toString();
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 5 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class JSON method writeJSONString.

/**
     * @since 1.2.11 
     */
public static void writeJSONString(Writer writer, Object object, int defaultFeatures, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter(writer, defaultFeatures, features);
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.write(object);
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)344 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)205 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)54 HashMap (java.util.HashMap)47 StringWriter (java.io.StringWriter)38 NameFilter (com.alibaba.fastjson.serializer.NameFilter)36 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)19 ArrayList (java.util.ArrayList)12 ListSerializer (com.alibaba.fastjson.serializer.ListSerializer)11 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)8 Date (java.util.Date)8 Map (java.util.Map)8 CollectionCodec (com.alibaba.fastjson.serializer.CollectionCodec)6 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)6 LinkedHashMap (java.util.LinkedHashMap)6 JSONException (com.alibaba.fastjson.JSONException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)4 JSONObject (com.alibaba.fastjson.JSONObject)3