Search in sources :

Example 16 with JSONPObject

use of com.alibaba.fastjson.JSONPObject in project fastjson by alibaba.

the class JSONPParseTest3 method test_f.

public void test_f() throws Exception {
    String text = "parent.callback ({'id':1, 'name':'ido)nans'},1,2 );   /**/ ";
    JSONPObject jsonpObject = (JSONPObject) JSON.parseObject(text, JSONPObject.class);
    assertEquals("parent.callback", jsonpObject.getFunction());
    assertEquals(3, jsonpObject.getParameters().size());
    JSONObject param = (JSONObject) jsonpObject.getParameters().get(0);
    assertEquals(1, param.get("id"));
    assertEquals("ido)nans", param.get("name"));
    String json = JSON.toJSONString(jsonpObject, SerializerFeature.BrowserSecure, SerializerFeature.MapSortField);
    assertEquals("/**/parent.callback({\"id\":1,\"name\":\"ido\\u0029nans\"},1,2)", json);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONPObject(com.alibaba.fastjson.JSONPObject)

Example 17 with JSONPObject

use of com.alibaba.fastjson.JSONPObject in project e3mall by colg-cloud.

the class TokenController method getUserByToken.

/**
 * 根据token获取user信息
 *
 * 使用 fastjson JSONPObject对象处理
 *
 * @param token
 * @param callback
 *            跨域请求
 * @return
 */
@GetMapping(value = "/user/{token}")
public Object getUserByToken(@PathVariable String token, String callback) {
    E3Result e3Result = tokenService.getUserByToken(token);
    // 响应结果之前,判断是否为jsonp请求, jsonp会自动带 callback
    if (StringUtils.isNotBlank(callback)) {
        // 创建jsonp对象
        JSONPObject jsonpObject = new JSONPObject(callback);
        jsonpObject.addParameter(e3Result);
        return jsonpObject;
    }
    return e3Result;
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) JSONPObject(com.alibaba.fastjson.JSONPObject) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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