FastJSON指南( 四 )


package com.ivan.frame.dto.common;public final class RequestDTO extends BaseDTO {private static final long serialVersionUID = -2780042604928728379L;/*** 调用方的名称*/private Stringcaller;/*** 请求参数*/private Tparam;public String getCaller() {return caller;}public void setCaller(String caller) {this.caller = caller;}/*** 获取请求参数*/public T getParam() {return param;}/*** 设置请求参数* * @param param 请求参数*/public void setParam(T param) {this.param = param;}}
定义一个具体的业务对象 ,  代码如下:
package com.ivan.frame.dto;import com.ivan.frame.dto.common.BaseDTO;public class PersonDTO extends BaseDTO {private static final long serialVersionUID = 4637634512292751986L;private int id;private int age;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
通过JSON.传入对象进行泛型转换 , 代码如下:
package com.ivan.json;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.TypeReference;import com.ivan.frame.dto.PersonDTO;import com.ivan.frame.dto.common.RequestDTO;public class GenericTest {public static void main(String[] args) {RequestDTO requestDTO = new RequestDTO();requestDTO.setCaller("callerId");PersonDTO personDTO = new PersonDTO();personDTO.setAge(11);personDTO.setName("张三");requestDTO.setParam(personDTO);String jsonString = JSON.toJSONString(requestDTO);System.out.println(jsonString);//这行是关键代码requestDTO = JSON.parseObject(jsonString, new TypeReference(){});System.out.println(requestDTO.getParam().getName());}}
各种概念
序列化相关的概念
public static String toJSONString(Object object, // SerializeConfig config, // SerializeFilter[] filters, // String dateFormat, //int defaultFeatures, // SerializerFeature... features) {SerializeWriter out = new SerializeWriter(null, defaultFeatures, features);try {JSONSerializer serializer = new JSONSerializer(out, config);if (dateFormat != null && dateFormat.length() != 0) {serializer.setDateFormat(dateFormat);serializer.config(SerializerFeature.WriteDateUseDateFormat, true);}if (filters != null) {for (SerializeFilter filter : filters) {serializer.addFilter(filter);}}serializer.write(object);return out.toString();} finally {out.close();}}
反序列化相关的概念
@SuppressWarnings("unchecked")public static T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor,int featureValues, Feature... features) {if (input == null) {return null;}if (features != null) {for (Feature feature : features) {featureValues |= feature.mask;}}DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);if (processor != null) {if (processor instanceof ExtraTypeProvider) {parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);}if (processor instanceof ExtraProcessor) {parser.getExtraProcessors().add((ExtraProcessor) processor);}if (processor instanceof FieldTypeResolver) {parser.setFieldTypeResolver((FieldTypeResolver) processor);}}T value = http://www.kingceram.com/post/(T) parser.parseObject(clazz, null);parser.handleResovleTask(value);parser.close();return (T) value;}
与 MVC整合
提供了用于将 mvc里的body数据(必须是json格式)转成里的请求参数或者将输出的对象转成json格式的数据 。mvc里的核心配置如下:
text/html;charset=UTF-8application/json;charset=UTF-8WriteMapNullValueWriteNullStringAsEmpty