| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package com.uas.ps.message.util;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.parser.Feature;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import java.util.List;
- /**
- * alibaba fastjson工具类封装
- * @author yingp
- * @see JSON
- *
- */
- public class FastjsonUtils {
- public static Feature DEFAULT_PARSER_FEATURE = Feature.DisableCircularReferenceDetect;
- public static SerializerFeature DEFAULT_SERIAL_FEATURE = SerializerFeature.DisableCircularReferenceDetect;
- /**
- * 把JSON文本parse为JSONObject或者JSONArray
- *
- * @param text text
- * @return
- */
- public static Object parse(String text) {
- return JSON.parse(text, DEFAULT_PARSER_FEATURE);
- }
- /**
- * 把JSON文本parse成JSONObject
- *
- * @param text text
- * @return
- */
- public static final JSONObject parseObject(String text) {
- return JSON.parseObject(text, DEFAULT_PARSER_FEATURE);
- }
- /**
- * 把JSON文本parse为JavaBean
- *
- * @param text text
- * @param clazz clazz
- * @return
- */
- public static final <T> T fromJson(String text, Class<T> clazz) {
- return JSON.parseObject(text, clazz, DEFAULT_PARSER_FEATURE);
- }
- /**
- * 把JSON文本parse成JSONArray
- *
- * @param text text
- * @return
- */
- public static final JSONArray fromJsonArray(String text) {
- return JSON.parseArray(text);
- }
- /**
- * 把JSON文本parse成JavaBean集合
- *
- * @param text text
- * @param clazz clazz
- * @return
- */
- public static final <T> List<T> fromJsonArray(String text, Class<T> clazz) {
- return JSON.parseArray(text, clazz);
- }
- /**
- * 将JavaBean序列化为JSON文本
- *
- * @param object object
- * @return
- */
- public static final String toJson(Object object) {
- return JSON.toJSONString(object, DEFAULT_SERIAL_FEATURE);
- }
- /**
- * 将JavaBean转换为JSONObject或者JSONArray。
- *
- * @param javaObject javaObject
- * @return
- */
- public static final Object toObject(Object javaObject) {
- return JSON.toJSON(javaObject);
- }
- }
|