當前位置: 首頁>>代碼示例>>Java>>正文


Java JSON.toJSONBytes方法代碼示例

本文整理匯總了Java中com.alibaba.fastjson.JSON.toJSONBytes方法的典型用法代碼示例。如果您正苦於以下問題:Java JSON.toJSONBytes方法的具體用法?Java JSON.toJSONBytes怎麽用?Java JSON.toJSONBytes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.alibaba.fastjson.JSON的用法示例。


在下文中一共展示了JSON.toJSONBytes方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test_codec

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_codec() throws Exception {
    int len = (Character.MAX_VALUE - Character.MIN_VALUE) + 1;
    char[] chars = new char[len];
    for (int i = 0; i < len; ++i) {
        char ch = (char) ((int) Character.MAX_VALUE + i);
        if (ch >= 55296 && ch <= 57344) {
            continue;
        }
        chars[i] = ch;
    }
    
    String text = new String(chars);
    
    byte[] bytes = JSON.toJSONBytes(text);
    String text2 = (String) JSON.parse(bytes);
    
    Assert.assertEquals(text.length(), text2.length());
    for (int i = 0; i < len; ++i) {
        char c1 = text.charAt(i);
        char c2 = text2.charAt(i);
        
        Assert.assertEquals(c1, c2);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:25,代碼來源:JSONBytesTest.java

示例2: convert

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public RequestBody convert(T value) throws IOException {
    byte[] content;
    if (serializeConfig != null) {
        if (serializerFeatures != null) {
            content = JSON.toJSONBytes(value, serializeConfig, serializerFeatures);
        } else {
            content = JSON.toJSONBytes(value, serializeConfig);
        }
    } else {
        if (serializerFeatures != null) {
            content = JSON.toJSONBytes(value, serializerFeatures);
        } else {
            content = JSON.toJSONBytes(value);
        }
    }
    return RequestBody.create(MEDIA_TYPE, content);
}
 
開發者ID:hushengjun,項目名稱:FastAndroid,代碼行數:19,代碼來源:FastJsonRequestBodyConverter.java

示例3: exceptionHandler

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
/** 異常處理 */
@ExceptionHandler(Exception.class)
public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
        throws Exception {
    logger.error(Constants.Exception_Head, ex);
    OperationResult result=new OperationResult();
    if (ex instanceof AbstractException) {
        ((AbstractException) ex).handler(result);
    } /*else if (ex instanceof IllegalArgumentException) {
        new IllegalParameterException(ex.getMessage()).handler(modelMap);
    } else if (ex instanceof UnauthorizedException) {
        modelMap.put("httpCode", HttpCode.FORBIDDEN.value());
        modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
    } */else {
        result.setCode(HttpCode.INTERNAL_SERVER_ERROR.value());
        String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
        result.setMessage(msg.length() > 100 ? "係統走神了,請稍候再試." : msg);
    }
    response.setContentType("application/json;charset=UTF-8");
    logger.info(JSON.toJSONString(result));
    byte[] bytes = JSON.toJSONBytes(result, SerializerFeature.DisableCircularReferenceDetect);
    response.getOutputStream().write(bytes);
}
 
開發者ID:liuxx001,項目名稱:bird-java,代碼行數:24,代碼來源:AbstractController.java

示例4: serialize

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public byte[] serialize(T t) throws SerializationException {
    if (t == null) {
        return new byte[0];
    }
    try {
        return JSON.toJSONBytes(t, fastJsonConfig.getSerializeConfig(), fastJsonConfig.getSerializerFeatures());
    } catch (Exception ex) {
        throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:FastJsonRedisSerializer.java

示例5: serialize

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public byte[] serialize(Object object) throws SerializationException {
    if (object == null) {
        return new byte[0];
    }
    try {
        return JSON.toJSONBytes(object, SerializerFeature.WriteClassName);
    } catch (Exception ex) {
        throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:GenericFastJsonRedisSerializer.java

示例6: convert

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public RequestBody convert(T value) throws IOException {
    byte[] content = JSON.toJSONBytes(value
                , serializeConfig == null
                    ? SerializeConfig.globalInstance
                    : serializeConfig
                , serializerFeatures == null
                    ? SerializerFeature.EMPTY
                    : serializerFeatures
    );

    return RequestBody.create(MEDIA_TYPE, content);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:13,代碼來源:Retrofit2ConverterFactory.java

示例7: test_0

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_0() throws Exception {
    String text = "\t\t\t\t\t\t \u00A020:00-21:30</span><br />\r\n\r\n</p>\r\n<p>\r\n\t\r\n</p>\r\n<p>\r\n\t<br />\r\n</p>\r\n\t\t\t";
    Product e = new Product();
    e.setIntro(text);
    byte[] r = JSON.toJSONBytes(e);
    JSON.parseObject(r, Product.class);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:8,代碼來源:Issue213Test.java

示例8: objectToBytes

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
/**
 *@description 將對象序列化成字節數組
 *@time 創建時間:2017年8月10日下午12:41:16
 *@param o
 *@return
 *@author dzn
 */
public static byte[] objectToBytes(Object o){
    String str = objectToString(o);
    if(str == o){
        return null;
    }else{
        byte[] bytes = JSON.toJSONBytes(str, SerializerFeature.PrettyFormat);
        return bytes;
    }
}
 
開發者ID:SnailFastGo,項目名稱:netty_op,代碼行數:17,代碼來源:FastjsonSerialize.java

示例9: apply

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public byte[] apply(Object value) {
    try {
        byte[] bs1 = JSON.toJSONBytes(value, SerializerFeature.WriteClassName);
        byte[] bs2 = new byte[bs1.length + 4];
        writeHeader(bs2, IDENTITY_NUMBER);
        System.arraycopy(bs1, 0, bs2, 4, bs1.length);
        return bs2;
    } catch (Exception e) {
        StringBuilder sb = new StringBuilder("Fastjson Encode error. ");
        sb.append("msg=").append(e.getMessage());
        throw new CacheEncodeException(sb.toString(), e);
    }
}
 
開發者ID:alibaba,項目名稱:jetcache,代碼行數:15,代碼來源:FastjsonValueEncoder.java

示例10: f_testF

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void f_testF() {
    Generic<String> q = new Generic<String>();
    byte[] text = JSON.toJSONBytes(q, SerializerFeature.WriteClassName);
    JSON.parseObject(text, Generic.class);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:6,代碼來源:TestFastJson.java

示例11: encodeToBytes

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public byte[] encodeToBytes(Object object) throws Exception {
    return JSON.toJSONBytes(object, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.BeanToArray);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:5,代碼來源:FastjsonArrayMappingCodec.java

示例12: encodeToBytes

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public byte[] encodeToBytes(Object object) throws Exception {
    return JSON.toJSONBytes(object, SerializerFeature.DisableCircularReferenceDetect);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:5,代碼來源:FastjsonCodec.java

示例13: toBytes

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public static byte[] toBytes(final Object obj) {
    return JSON.toJSONBytes(obj, features);
}
 
開發者ID:jiumao-org,項目名稱:wechat-mall,代碼行數:4,代碼來源:JsonUtil.java

示例14: preHandle

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,OPTIONS,DELETE");
    response.setHeader("Access-Control-Allow-Headers",
        "x-requested-with,Access-Control-Allow-Origin,EX-SysAuthToken,EX-JSESSIONID");

    String url = request.getServletPath();
    if (url.endsWith("/unauthorized") || url.endsWith("/forbidden") || isWhiteReq(url.toLowerCase())) {
        return super.preHandle(request, response, handler);
    }
    if (containsParamter) {
        url += JSON.toJSONString(WebUtil.getParameterMap(request));
    }
    Object userId = WebUtil.getCurrentUser(request);
    String user = userId != null ? userId.toString() : WebUtil.getHost(request) + request.getHeader("USER-AGENT");
    String preRequest = (String)CacheUtil.getCache().getFire(Constants.PREREQUEST + user);
    Long preRequestTime = (Long)CacheUtil.getCache().getFire(Constants.PREREQUEST_TIME + user);
    int seconds = minRequestIntervalTime / 500;
    if (preRequestTime != null && preRequest != null) { // 過濾頻繁操作
        if ((url.equals(preRequest) || allRequest)
            && System.currentTimeMillis() - preRequestTime < minRequestIntervalTime) {
            Integer maliciousRequestTimes = (Integer)CacheUtil.getCache()
                .getFire(Constants.MALICIOUS_REQUEST_TIMES + user);
            if (maliciousRequestTimes == null) {
                maliciousRequestTimes = 1;
            } else {
                maliciousRequestTimes++;
            }
            CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, maliciousRequestTimes, seconds);
            if (maliciousRequestTimes > maxMaliciousTimes) {
                CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, 0, seconds);
                logger.warn("To intercept a malicious request : {}", url);
                ModelMap modelMap = new ModelMap();
                modelMap.put("code", HttpCode.MULTI_STATUS.value().toString());
                modelMap.put("msg", HttpCode.MULTI_STATUS.msg());
                modelMap.put("timestamp", System.currentTimeMillis());
                logger.info("RESPONSE : " + JSON.toJSON(modelMap));
                byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
                response.getOutputStream().write(bytes);
                return false;
            }
        } else {
            CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, 0, seconds);
        }
    }
    CacheUtil.getCache().set(Constants.PREREQUEST + user, url, seconds);
    CacheUtil.getCache().set(Constants.PREREQUEST_TIME + user, System.currentTimeMillis(), seconds);
    return super.preHandle(request, response, handler);
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:51,代碼來源:MaliciousRequestInterceptor.java

示例15: getJoinPackage

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
static byte[] getJoinPackage(int roomId) {
    byte[] jsonBytes = JSON.toJSONBytes(new JoinEntity(roomId));
    int packageLength = PACKAGE_HEAD_BYTES_LENGTH + jsonBytes.length;
    return ByteBuffer.allocate(packageLength).putInt(packageLength).put(JOIN_PACKAGE_PROTOCOL_BYTES).put(jsonBytes).array();
}
 
開發者ID:czp3009,項目名稱:bilibili-live-api,代碼行數:6,代碼來源:PackageRepository.java


注:本文中的com.alibaba.fastjson.JSON.toJSONBytes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。