Search in sources :

Example 11 with JSONPObject

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;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONPObject(com.alibaba.fastjson.JSONPObject) JSONPObject(com.alibaba.fastjson.JSONPObject)

Example 12 with JSONPObject

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();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ArrayList(java.util.ArrayList) JSONException(com.alibaba.fastjson.JSONException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter) JSONPObject(com.alibaba.fastjson.JSONPObject) JSONPObject(com.alibaba.fastjson.JSONPObject)

Example 13 with JSONPObject

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();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) JSONPObject(com.alibaba.fastjson.JSONPObject) JSONPObject(com.alibaba.fastjson.JSONPObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with JSONPObject

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JSONPObject(com.alibaba.fastjson.JSONPObject) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP)

Example 15 with 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;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONPObject(com.alibaba.fastjson.JSONPObject) JSONPObject(com.alibaba.fastjson.JSONPObject)

Aggregations

JSONPObject (com.alibaba.fastjson.JSONPObject)17 JSONException (com.alibaba.fastjson.JSONException)4 JSONObject (com.alibaba.fastjson.JSONObject)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 SerializeFilter (com.alibaba.fastjson.serializer.SerializeFilter)2 ResponseJSONP (com.alibaba.fastjson.support.spring.annotation.ResponseJSONP)2 ArrayList (java.util.ArrayList)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 E3Result (cn.e3mall.common.pojo.E3Result)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1