fastjson 序列化
************************
相关类
JSON
public abstract class JSON implements JSONStreamAware, JSONAware {
*************
序列化为 String
public static String toJSONString(Object object)
public static String toJSONString(Object object, SerializerFeature... features) {
public static String toJSONString(Object object, int defaultFeatures, SerializerFeature... features) {
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {
public static String toJSONString(Object object, SerializeConfig config, SerializerFeature... features) {
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) {
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features) {
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {
public static String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features) {
public static String toJSONString(Object object, boolean prettyFormat) {
public static void writeJSONString(Writer writer, Object object, SerializerFeature... features) {
public static void writeJSONString(Writer writer, Object object, int defaultFeatures, SerializerFeature... features) {
public static final int writeJSONString(OutputStream os, Object object, SerializerFeature... features) throws IOException {
public static final int writeJSONString(OutputStream os, Object object, int defaultFeatures, SerializerFeature... features) throws IOException {
public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializerFeature... features) throws IOException {
public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException {
public static final int writeJSONStringWithFastJsonConfig(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException {
*************
序列化为 byte[]
public static byte[] toJSONBytes(Object object, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, int defaultFeatures, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeFilter filter, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeFilter[] filters, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeConfig config, int defaultFeatures, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, int defaultFeatures, SerializerFeature... features) {
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {
public static byte[] toJSONBytes(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {
public static byte[] toJSONBytesWithFastJsonConfig(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {
*************
反序列化为 Object
public static Object parse(String text) {
public static Object parse(String text, ParserConfig config) {
public static Object parse(String text, ParserConfig config, Feature... features) {
public static Object parse(String text, ParserConfig config, int features) {
public static Object parse(String text, int features) {
public static Object parse(String text, Feature... features) {
public static Object parse(byte[] input, Feature... features) {
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Feature... features) {
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {
*************
反序列化为 JSONObject
public static JSONObject parseObject(String text) {
public static JSONObject parseObject(String text, Feature... features) {
*************
反序列化为 T
public static <T> T parseObject(String text, Class<T> clazz) {
public static <T> T parseObject(String text, TypeReference<T> type, Feature... features) {
public static <T> T parseObject(String text, Class<T> clazz, ParseProcess processor, Feature... features) {
public static <T> T parseObject(String json, Class<T> clazz, Feature... features) {
public static <T> T parseObject(String json, Type type, Feature... features) {
public static <T> T parseObject(String input, Type clazz, ParseProcess processor, Feature... features) {
public static <T> T parseObject(String input, Type clazz, int featureValues, Feature... features) {
public static <T> T parseObject(String input, Type clazz, ParserConfig config, Feature... features) {
public static <T> T parseObject(String input, Type clazz, ParserConfig config, int featureValues,
public static <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor,
public static <T> T parseObject(byte[] bytes, Type clazz, Feature... features) {
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features) {
public static <T> T parseObject(byte[] bytes, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
public static <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features) {
public static <T> T parseObject(char[] input, int length, Type clazz, Feature... features) {
public static <T> T parseObject(InputStream is, Type type, Feature... features) throws IOException {
public static <T> T parseObject(InputStream is, Charset charset, Type type, Feature... features) throws IOException {
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, Feature... features) throws IOException {
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) throws IOException {
*************
反序列化为 JSONArray
public static JSONArray parseArray(String text) {
public static JSONArray parseArray(String text, ParserConfig parserConfig) {
*************
反序列化为 List
public static <T> List<T> parseArray(String text, Class<T> clazz) {
public static <T> List<T> parseArray(String text, Class<T> clazz, ParserConfig config) {
public static List<Object> parseArray(String text, Type[] types) {
public static List<Object> parseArray(String text, Type[] types, ParserConfig config) {
*************
其他方法
public static Object toJSON(Object javaObject) {
public static Object toJSON(Object javaObject, SerializeConfig config) {
public static <T> T toJavaObject(JSON json, Class<T> clazz) {
public <T> T toJavaObject(Class<T> clazz) {
public <T> T toJavaObject(Type type) {
public <T> T toJavaObject(TypeReference typeReference) {
public void writeJSONString(Appendable appendable) {
public String toString() {
public String toJSONString() {
public String toString(SerializerFeature... features) {
*******************
示例
User
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
*******************
对象序列化、反序列化
public class JsonTest {
public static void main(String[] args){
User user=new User();
user.setId(12);
user.setName("瓜田李下");
user.setAge(18);
byte[] bytes= JSON.toJSONBytes(user); //对象序列化成字节数组
System.out.println(Arrays.toString(bytes));
User u=JSON.parseObject(bytes,User.class); //字节数组反序列化成对象
System.out.println(u);
}
}
*******************
控制台输出
[123, 34, 97, 103, 101, 34, 58, 49, 56, 44, 34, 105, 100, 34, 58, 49, 50, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 34, 125]
User(id=12, name=瓜田李下, age=18)
*******************
列表序列化、反序列化
public class JsonTest {
public static void main(String[] args){
List<User> list=new ArrayList<>();
for(int i=0;i<10;i++){
User u1=new User();
u1.setId(i+1);
u1.setName("瓜田李下"+i);
u1.setAge(23);
list.add(u1);
}
byte[] b1=JSON.toJSONBytes(list); //对象列表序列化成字节数组
System.out.println(Arrays.toString(b1));
List<User> l=JSON.parseArray(new String(b1),User.class);
//字节数组反序列化成对象列表
l.foreach(System.out::println);
}
}
*******************
控制台输出
[91, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 49, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 48, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 50, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 49, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 51, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 50, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 52, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 51, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 53, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 52, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 54, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 53, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 55, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 54, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 56, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 55, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 57, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 56, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 49, 48, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 57, 34, 125, 93]
User(id=1, name=瓜田李下0, age=23)
User(id=2, name=瓜田李下1, age=23)
User(id=3, name=瓜田李下2, age=23)
User(id=4, name=瓜田李下3, age=23)
User(id=5, name=瓜田李下4, age=23)
User(id=6, name=瓜田李下5, age=23)
User(id=7, name=瓜田李下6, age=23)
User(id=8, name=瓜田李下7, age=23)
User(id=9, name=瓜田李下8, age=23)
User(id=10, name=瓜田李下9, age=23)