Skip to content

Commit

Permalink
Optimize Jaxrs and Spring support.
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorZeng committed Dec 4, 2018
1 parent ff7f397 commit 7742a53
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
32 changes: 29 additions & 3 deletions src/main/java/com/alibaba/fastjson/JSON.java
Expand Up @@ -493,10 +493,36 @@ public static <T> T parseObject(InputStream is, //
Charset charset, //
Type type, //
Feature... features) throws IOException {
return (T) parseObject(is, charset, type, ParserConfig.global, features);
}

/**
* @since 1.2.55
*/
@SuppressWarnings("unchecked")
public static <T> T parseObject(InputStream is, //
Charset charset, //
Type type, //
ParserConfig config, //
Feature... features) throws IOException {
return (T) parseObject(is, charset, type, config, null, DEFAULT_PARSER_FEATURE, features);
}

/**
* @since 1.2.55
*/
@SuppressWarnings("unchecked")
public static <T> T parseObject(InputStream is, //
Charset charset, //
Type type, //
ParserConfig config, //
ParseProcess processor, //
int featureValues, //
Feature... features) throws IOException {
if (charset == null) {
charset = IOUtils.UTF8;
}

byte[] bytes = allocateBytes(1024 * 64);
int offset = 0;
for (;;) {
Expand All @@ -511,8 +537,8 @@ public static <T> T parseObject(InputStream is, //
bytes = newBytes;
}
}
return (T) parseObject(bytes, 0, offset, charset, type, features);

return (T) parseObject(bytes, 0, offset, charset, type, config, processor, featureValues, features);
}

public static <T> T parseObject(String text, Class<T> clazz) {
Expand Down
Expand Up @@ -242,12 +242,6 @@ protected boolean hasMatchingMediaType(MediaType mediaType) {
return true;
}

/*
* /********************************************************** /* Partial
* MessageBodyWriter impl
* /**********************************************************
*/

/**
* Method that JAX-RS container calls to try to check whether given value
* (of specified type) can be serialized by this provider.
Expand Down Expand Up @@ -293,20 +287,20 @@ public void writeTo(Object obj, //
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));
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, //
JSON.writeJSONString(entityStream, //
fastJsonConfig.getCharset(), //
obj, //
fastJsonConfig.getSerializeConfig(), //
Expand All @@ -315,11 +309,6 @@ public void writeTo(Object obj, //
JSON.DEFAULT_GENERATE_FEATURE, //
fastJsonConfig.getSerializerFeatures());

// // add Content-Length
// if (fastJsonConfig.isWriteContentLength()) {
// httpHeaders.add("Content-Length", String.valueOf(len));
// }

entityStream.flush();

} catch (JSONException ex) {
Expand All @@ -328,12 +317,6 @@ public void writeTo(Object obj, //
}
}

/*
* /********************************************************** /*
* MessageBodyReader impl
* /**********************************************************
*/

/**
* Method that JAX-RS container calls to try to check whether values of
* given type (and media type) can be deserialized by this provider.
Expand Down Expand Up @@ -366,7 +349,13 @@ public Object readFrom(Class<Object> type, //
try {
FastJsonConfig fastJsonConfig = locateConfigProvider(type, mediaType);

return JSON.parseObject(entityStream, fastJsonConfig.getCharset(), genericType, fastJsonConfig.getFeatures());
return JSON.parseObject(entityStream,
fastJsonConfig.getCharset(),
genericType,
fastJsonConfig.getParserConfig(),
fastJsonConfig.getParseProcess(),
JSON.DEFAULT_PARSER_FEATURE,
fastJsonConfig.getFeatures());

} catch (JSONException ex) {

Expand Down
Expand Up @@ -6,7 +6,6 @@
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.util.IOUtils;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
Expand Down Expand Up @@ -59,8 +58,6 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O

public static final MediaType APPLICATION_JAVASCRIPT = new MediaType("application", "javascript");

private Charset charset = Charset.forName("UTF-8");

@Deprecated
protected SerializerFeature[] features = new SerializerFeature[0];

Expand Down Expand Up @@ -196,11 +193,17 @@ protected Object readInternal(Class<? extends Object> clazz, //
return readType(getType(clazz, null), inputMessage);
}

private Object readType(Type type, HttpInputMessage inputMessage) throws IOException {
private Object readType(Type type, HttpInputMessage inputMessage) {

try {
InputStream in = inputMessage.getBody();
return JSON.parseObject(in, fastJsonConfig.getCharset(), type, fastJsonConfig.getFeatures());
return JSON.parseObject(in,
fastJsonConfig.getCharset(),
type,
fastJsonConfig.getParserConfig(),
fastJsonConfig.getParseProcess(),
JSON.DEFAULT_PARSER_FEATURE,
fastJsonConfig.getFeatures());
} catch (JSONException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex);
} catch (IOException ex) {
Expand Down Expand Up @@ -235,7 +238,7 @@ protected void writeInternal(Object object, HttpOutputMessage outputMessage) thr
// 保持原有的MappingFastJsonValue对象的contentType不做修改 保持旧版兼容。
// 但是新的JSONPObject将返回标准的contentType:application/javascript ,不对是否有function进行判断
if (value instanceof MappingFastJsonValue) {
if(!StringUtils.isEmpty(((MappingFastJsonValue) value).getJsonpFunction())){
if (!StringUtils.isEmpty(((MappingFastJsonValue) value).getJsonpFunction())) {
isJsonp = true;
}
} else if (value instanceof JSONPObject) {
Expand Down
Expand Up @@ -66,9 +66,11 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, O
Object obj = null;
if (payload instanceof byte[]) {
obj = JSON.parseObject((byte[]) payload, 0, ((byte[]) payload).length,
fastJsonConfig.getCharset(), targetClass, fastJsonConfig.getFeatures());
}else if(payload instanceof String) {
obj = JSON.parseObject((String) payload,targetClass,fastJsonConfig.getFeatures());
fastJsonConfig.getCharset(), targetClass, fastJsonConfig.getParserConfig(),
fastJsonConfig.getParseProcess(), JSON.DEFAULT_PARSER_FEATURE, fastJsonConfig.getFeatures());
} else if (payload instanceof String) {
obj = JSON.parseObject((String) payload, targetClass, fastJsonConfig.getParserConfig(),
fastJsonConfig.getParseProcess(), JSON.DEFAULT_PARSER_FEATURE, fastJsonConfig.getFeatures());
}

return obj;
Expand All @@ -77,8 +79,7 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, O
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
// encode payload to json string
return JSON.toJSONString(payload,fastJsonConfig.getSerializeConfig(),
fastJsonConfig.getSerializeFilters(),fastJsonConfig.getSerializerFeatures());

return JSON.toJSONString(payload, fastJsonConfig.getSerializeConfig(), fastJsonConfig.getSerializeFilters(),
fastJsonConfig.getDateFormat(), JSON.DEFAULT_GENERATE_FEATURE, fastJsonConfig.getSerializerFeatures());
}
}

0 comments on commit 7742a53

Please sign in to comment.