本文整理汇总了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);
}
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
示例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);
}
示例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;
}
}
示例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);
}
}
示例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);
}
示例11: encodeToBytes
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public byte[] encodeToBytes(Object object) throws Exception {
return JSON.toJSONBytes(object, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.BeanToArray);
}
示例12: encodeToBytes
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public byte[] encodeToBytes(Object object) throws Exception {
return JSON.toJSONBytes(object, SerializerFeature.DisableCircularReferenceDetect);
}
示例13: toBytes
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public static byte[] toBytes(final Object obj) {
return JSON.toJSONBytes(obj, features);
}
示例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);
}
示例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();
}