Java Code Examples for com.alibaba.fastjson.serializer.JSONSerializer#config()

The following examples show how to use com.alibaba.fastjson.serializer.JSONSerializer#config() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonUtil.java    From dpCms with Apache License 2.0 6 votes vote down vote up
/**
 *  将一个包含Date类型数据的Object序列化成字符串
 * @param object
 * @param dateFormat
 * @param features
 * @return
 */
public static String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features) {
       SerializeWriter out = new SerializeWriter();
       try {
           SerializeConfig config = new SerializeConfig();
		JSONSerializer serializer = new JSONSerializer(out,config );
           if(dateFormat != null) {
           	serializer.setDateFormat(dateFormat);
           }
           for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
               serializer.config(feature, true);
           }
           serializer.write(object);
           return out.toString();
       } finally {
           out.close();
       }
   }
 
Example 2
Source File: JsonKit.java    From zbus-server with MIT License 6 votes vote down vote up
private static final byte[] toJSONBytes(Object object, String charsetName,
		SerializerFeature... features) {
	
	if(charsetName == null){
		charsetName = DEFAULT_ENCODING;
	}
	
	SerializeWriter out = new SerializeWriter(); 
	try {
		JSONSerializer serializer = new JSONSerializer(out);
		for (SerializerFeature feature : features) {
			serializer.config(feature, true);
		}

		serializer.write(object);

		return out.toBytes(charsetName);
	} finally {
		out.close();
	}
}
 
Example 3
Source File: FastJsonObjectOutput.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
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);
    out.close(); // for reuse SerializeWriter buf
    writer.println();
    writer.flush();
}
 
Example 4
Source File: FastJsonSerailization.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Object data) throws IOException {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.config(SerializerFeature.WriteEnumUsingToString, true);
    serializer.config(SerializerFeature.WriteClassName, true);
    serializer.write(data);
    return out.toBytes(CHARSET_UTF8);
}
 
Example 5
Source File: FastJsonObjectOutput.java    From JobX with Apache License 2.0 5 votes vote down vote up
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);
    out.close(); // for reuse SerializeWriter buf
    writer.println();
    writer.flush();
}
 
Example 6
Source File: FastJsonObjectOutput.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: FastJsonObjectOutput.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: FastJsonObjectOutput.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: JsonUtil.java    From dpCms with Apache License 2.0 5 votes vote down vote up
/**
 * @see 将一个Object序列化成字符串
 * @param object
 * @param features
 * @return
 */
public static String toJSONString(Object object, SerializerFeature... features) {
       SerializeWriter out = new SerializeWriter();
       try {
           SerializeConfig config = new SerializeConfig();
		JSONSerializer serializer = new JSONSerializer(out,config );
           for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
               serializer.config(feature, true);
           }
           serializer.write(object);
           return out.toString();
       } finally {
           out.close();
       }
   }
 
Example 10
Source File: FastjsonHttpMessageConverter.java    From java-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {
	Charset charset = getCharset(outputMessage.getHeaders());
	OutputStreamWriter out = new OutputStreamWriter(outputMessage.getBody(), charset);
	SerializeWriter writer = new SerializeWriter(out);
	JSONSerializer serializer = new JSONSerializer(writer, this.serializeConfig);
	serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);

	RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
	if (requestAttributes instanceof ServletRequestAttributes) {
		Class<?> rootClass = filterClass(obj);
		if (rootClass != null) {
			String[] paths = ((ServletRequestAttributes) requestAttributes).getRequest()
					.getParameterValues("paths");
			if (paths != null && paths.length > 0) {
				rootClass = ClassUtils.getUserClass(rootClass);
				Map<Class<?>, PropertyPreFilter> propertyFilters = new HashMap<>();
				pathVisit(propertyFilters, Sets.newHashSet(paths), rootClass);

				for (Entry<Class<?>, PropertyPreFilter> entry : propertyFilters.entrySet()) {
					serializer.getPropertyPreFilters().add(entry.getValue());
				}
			} else {

				serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER);
			}
		} else {
			serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER);
		}
	}
	serializer.write(obj);
	writer.flush();
	out.close();
}
 
Example 11
Source File: AjaxAuthenticationFilter.java    From java-platform with Apache License 2.0 5 votes vote down vote up
protected void writeObject(ServletRequest request, ServletResponse response, Object result) throws IOException {
	String characterEncoding = ((HttpServletRequest) request).getCharacterEncoding();
	Charset charset = Strings.isNullOrEmpty(characterEncoding) ? DEFAULT_CHARSET : Charset.forName(characterEncoding);
	OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), charset);
	SerializeWriter writer = new SerializeWriter(out);
	JSONSerializer serializer = new JSONSerializer(writer, SpringContext.getBean(SerializeConfig.class));
	serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
	serializer.write(result);
	writer.flush();
	out.close();
}
 
Example 12
Source File: CaptchaFilter.java    From java-platform with Apache License 2.0 5 votes vote down vote up
protected void writeObject(ServletRequest request, ServletResponse response, Object result) throws IOException {
	String characterEncoding = ((HttpServletRequest) request).getCharacterEncoding();
	Charset charset = Strings.isNullOrEmpty(characterEncoding) ? DEFAULT_CHARSET
			: Charset.forName(characterEncoding);
	OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), charset);
	SerializeWriter writer = new SerializeWriter(out);
	JSONSerializer serializer = new JSONSerializer(writer, this.serializeConfig);
	serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
	serializer.write(result);
	writer.flush();
	out.close();
}
 
Example 13
Source File: ResultMarshaller.java    From watcher with Apache License 2.0 5 votes vote down vote up
private JSONSerializer getJsonSerializer(SerializeWriter out) {
	JSONSerializer serializer = new JSONSerializer(out);
	serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
	serializer.setDateFormat("yyyy-MM-dd HH:mm:ss");
	serializer.config(SerializerFeature.QuoteFieldNames, true);
	serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
	return serializer;
}
 
Example 14
Source File: FastJsonObjectOutput.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 15
Source File: FastJsonProvider.java    From metrics with Apache License 2.0 4 votes vote down vote up
public String toJSONString(Object object, SerializeFilter filter, SerializerFeature[] features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out, fastJsonConfig.serializeConfig);
        if (features != null) {
            for (SerializerFeature feature : features) {
                serializer.config(feature, true);
            }
        }

        if (filter != null) {
            if (filter instanceof PropertyPreFilter) {
                serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
            }

            if (filter instanceof NameFilter) {
                serializer.getNameFilters().add((NameFilter) filter);
            }

            if (filter instanceof ValueFilter) {
                serializer.getValueFilters().add((ValueFilter) filter);
            }

            if (filter instanceof PropertyFilter) {
                serializer.getPropertyFilters().add((PropertyFilter) filter);
            }

            if (filter instanceof BeforeFilter) {
                serializer.getBeforeFilters().add((BeforeFilter) filter);
            }

            if (filter instanceof AfterFilter) {
                serializer.getAfterFilters().add((AfterFilter) filter);
            }
        }

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}