Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
FastJsonHttpMessageConverter support dateFormat, for issue 418. #418
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 13, 2016
1 parent 9121e58 commit 55bd4e3
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {

public final static Charset UTF8 = Charset.forName("UTF-8");
public final static Charset UTF8 = Charset.forName("UTF-8");

private Charset charset = UTF8;
private Charset charset = UTF8;

private SerializerFeature[] features = new SerializerFeature[0];
private SerializerFeature[] features = new SerializerFeature[0];

protected SerializeFilter[] serialzeFilters = new SerializeFilter[0];

protected String dateFormat;

public FastJsonHttpMessageConverter(){
super(new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8));
}
Expand All @@ -45,6 +48,14 @@ public void setCharset(Charset charset) {
this.charset = charset;
}

public String getDateFormat() {
return dateFormat;
}

public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}

public SerializerFeature[] getFeatures() {
return features;
}
Expand All @@ -54,8 +65,8 @@ public void setFeatures(SerializerFeature... features) {
}

@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException {
protected Object readInternal(Class<? extends Object> clazz,
HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

Expand All @@ -79,9 +90,14 @@ protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage in

@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
HttpHeaders headers=outputMessage.getHeaders();
String text = JSON.toJSONString(obj, serialzeFilters, features);
HttpMessageNotWritableException {
HttpHeaders headers = outputMessage.getHeaders();
String text = JSON.toJSONString(obj, //
SerializeConfig.globalInstance, //
serialzeFilters, //
dateFormat, //
JSON.DEFAULT_GENERATE_FEATURE, //
features);
byte[] bytes = text.getBytes(charset);
headers.setContentLength(bytes.length);
OutputStream out = outputMessage.getBody();
Expand Down

0 comments on commit 55bd4e3

Please sign in to comment.