use of com.alibaba.fastjson.JSONPObject in project fastjson by alibaba.
the class JSONPDeserializer method deserialze.
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexerBase lexer = (JSONLexerBase) parser.getLexer();
SymbolTable symbolTable = parser.getSymbolTable();
String funcName = lexer.scanSymbolUnQuoted(symbolTable);
lexer.nextToken();
int tok = lexer.token();
if (tok == JSONToken.DOT) {
String name = lexer.scanSymbolUnQuoted(parser.getSymbolTable());
funcName += ".";
funcName += name;
lexer.nextToken();
tok = lexer.token();
}
JSONPObject jsonp = new JSONPObject(funcName);
if (tok != JSONToken.LPAREN) {
throw new JSONException("illegal jsonp : " + lexer.info());
}
lexer.nextToken();
for (; ; ) {
Object arg = parser.parse();
jsonp.addParameter(arg);
tok = lexer.token();
if (tok == JSONToken.COMMA) {
lexer.nextToken();
} else if (tok == JSONToken.RPAREN) {
lexer.nextToken();
break;
} else {
throw new JSONException("illegal jsonp : " + lexer.info());
}
}
tok = lexer.token();
if (tok == JSONToken.SEMI) {
lexer.nextToken();
}
return (T) jsonp;
}
use of com.alibaba.fastjson.JSONPObject in project fastjson by alibaba.
the class FastJsonHttpMessageConverter method writeInternal.
@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();
}
// 但是新的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.writeJSONStringWithFastJsonConfig(//
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() && !setLengthError) {
try {
headers.setContentLength(len);
} catch (UnsupportedOperationException ex) {
// skip
setLengthError = true;
}
}
outnew.writeTo(outputMessage.getBody());
} catch (JSONException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
} finally {
outnew.close();
}
}
use of com.alibaba.fastjson.JSONPObject in project fastjson by alibaba.
the class FastJsonJsonView method renderMergedOutputModel.
@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.writeJSONStringWithFastJsonConfig(//
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();
}
use of com.alibaba.fastjson.JSONPObject in project fastjson by alibaba.
the class JSONPResponseBodyAdvice method beforeBodyWrite.
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class);
if (responseJsonp == null) {
responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class);
}
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
String callbackMethodName = servletRequest.getParameter(responseJsonp.callback());
if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) {
if (logger.isDebugEnabled()) {
logger.debug("Invalid jsonp parameter value:" + callbackMethodName);
}
callbackMethodName = null;
}
JSONPObject jsonpObject = new JSONPObject(callbackMethodName);
jsonpObject.addParameter(body);
beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response);
return jsonpObject;
}
use of com.alibaba.fastjson.JSONPObject in project uavstack by uavorg.
the class JSONPDeserializer method deserialze.
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexerBase lexer = (JSONLexerBase) parser.getLexer();
SymbolTable symbolTable = parser.getSymbolTable();
String funcName = lexer.scanSymbolUnQuoted(symbolTable);
lexer.nextToken();
int tok = lexer.token();
if (tok == JSONToken.DOT) {
String name = lexer.scanSymbolUnQuoted(parser.getSymbolTable());
funcName += ".";
funcName += name;
lexer.nextToken();
tok = lexer.token();
}
JSONPObject jsonp = new JSONPObject(funcName);
if (tok != JSONToken.LPAREN) {
throw new JSONException("illegal jsonp : " + lexer.info());
}
lexer.nextToken();
for (; ; ) {
Object arg = parser.parse();
jsonp.addParameter(arg);
tok = lexer.token();
if (tok == JSONToken.COMMA) {
lexer.nextToken();
} else if (tok == JSONToken.RPAREN) {
lexer.nextToken();
break;
} else {
throw new JSONException("illegal jsonp : " + lexer.info());
}
}
tok = lexer.token();
if (tok == JSONToken.SEMI) {
lexer.nextToken();
}
return (T) jsonp;
}
Aggregations