FastJson 序列化與反序列化一些說明

最近所屬的組須要對接一些徵信結構,就涉及到不少中的數據格式,而springmvc中使用的是jackson做爲@ResponseBody的依賴jar css

可是我的認爲fastkson的性能要高於jackson因此,但願返回的json格式是採用fastjson而非jacksonhtml

特此記錄:web

  首先請參照:SerializerFeature屬性spring

名稱 含義 備註
QuoteFieldNames 輸出key時是否使用雙引號,默認爲true  
UseSingleQuotes 使用單引號而不是雙引號,默認爲false  
WriteMapNullValue 是否輸出值爲null的字段,默認爲false  
WriteEnumUsingToString Enum輸出name()或者original,默認爲false  
UseISO8601DateFormat Date使用ISO8601格式輸出,默認爲false  
WriteNullListAsEmpty List字段若是爲null,輸出爲[],而非null  
WriteNullStringAsEmpty 字符類型字段若是爲null,輸出爲」「,而非null  
WriteNullNumberAsZero 數值字段若是爲null,輸出爲0,而非null  
WriteNullBooleanAsFalse Boolean字段若是爲null,輸出爲false,而非null  
SkipTransientField 若是是true,類中的Get方法對應的Field是transient,序列化時將會被忽略。默認爲true  
SortField 按字段名稱排序後輸出。默認爲false  
WriteTabAsSpecial 把\t作轉義輸出,默認爲false 不推薦
PrettyFormat 結果是否格式化,默認爲false  
WriteClassName 序列化時寫入類型信息,默認爲false。反序列化是需用到  
DisableCircularReferenceDetect 消除對同一對象循環引用的問題,默認爲false  
WriteSlashAsSpecial 對斜槓’/’進行轉義  
BrowserCompatible 將中文都會序列化爲\uXXXX格式,字節數會多一些,可是能兼容IE 6,默認爲false  
WriteDateUseDateFormat 全局修改日期格式,默認爲false。JSON.DEFFAULT_DATE_FORMAT = 「yyyy-MM-dd」;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);  
DisableCheckSpecialChar 一個對象的字符串屬性中若是有特殊字符如雙引號,將會在轉成json時帶有反斜槓轉移符。若是不須要轉義,可使用這個屬性。默認爲false  
NotWriteRootClassName 含義  
BeanToArray 將對象轉爲array輸出  
WriteNonStringKeyAsString 含義  
NotWriteDefaultValue 含義  
BrowserSecure 含義  
IgnoreNonFieldGetter 含義  
WriteEnumUsingName 含義  

 

而後記錄你須要的名稱(也就是第一列),並修改你的spring-mvc文件:下圖中加粗部分 爲新加入部分json

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 激活自動代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!--自動掃描,註解控制器 -->
    <context:component-scan base-package="com.xxxxx.credit.controller"/>

    <!--註解驅動 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- @ResponseBody亂碼問題,將StringHttpMessageConverter的默認編碼設爲UTF-8 -->
            <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <beans:constructor-arg value="UTF-8"/>
            </beans:bean>
            <!-- 配置Fastjson支持 -->
            <beans:bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <beans:property name="charset" value="UTF-8"/>
                <beans:property name="supportedMediaTypes">
                    <beans:list>
                        <beans:value>application/json</beans:value>
                        <beans:value>text/html;charset=UTF-8</beans:value>
                    </beans:list>
                </beans:property>
                <beans:property name="features">
                    <beans:list>
                        <!-- 是否輸出值爲null的字段,默認爲false-->
                        <beans:value>WriteMapNullValue</beans:value>
                        <beans:value>QuoteFieldNames</beans:value>
                        <beans:value>WriteDateUseDateFormat</beans:value>
                        <beans:value>WriteEnumUsingToString</beans:value>
                    </beans:list>
                </beans:property>
            </beans:bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


    <mvc:interceptors>
        <!--  使用bean定義一個Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截全部的請求   -->
        <!-- <bean class="com.bybo.aca.web.interceptor.Login"/> -->
        <mvc:interceptor>
            <!-- 進行攔截:/**表示攔截全部controller -->
            <mvc:mapping path="/**" />
            <bean class="com.xxxxx.credit.interceptor.WebInterceptor"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <!-- 進行攔截:/**表示攔截全部controller -->
            <mvc:mapping path="/**" />
            <bean class="com.xxxxx.credit.interceptor.AccessLogInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <!--靜態資源映射-->
    <!--本項目把靜態資源放在了WEB-INF的statics目錄下,資源映射以下-->
    <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
    <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
    <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>

    <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加先後綴(若是最後一個仍是表示文件夾,則最後的斜槓不要漏了) 使用JSP-->
    <!-- 默認的視圖解析器 在上邊的解析錯誤時使用 (默認使用html)- -->
    <bean id="defaultViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/><!--設置JSP文件的目錄位置-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- springMvc文件上傳須要配置的節點-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="20971500"/><!--設置JSP文件的目錄位置-->
        <property name="defaultEncoding" value="UTF-8"/><!--設置默認編碼-->
        <property name="resolveLazily" value="true"/><!--啓用是爲了推遲文件解析,以便捕獲文件大小異常-->
    </bean>




</beans>

-------------------------------------------------------------------------------------------------------分割線-------------------------------------------------------------------------------------------------------------------------spring-mvc

另外 在解析對方響應的數據時碰到相似這種狀況:mvc

{"a_id":"111","b_id":"2222"} 若是你實體類中定義的屬性是a_id 這固然能夠解析,但若是你再將實體類 parseJson以後就會出現將類中的屬性解析爲aId,bIdapp

也就是說fastjson會將你的屬性名中的下劃線取消掉,這時若是還想包含下劃線,則應該在該類中的字段上加上:jsp

@JSONField(name="hit_rules")
private String hit_rules;

 -------------------------------------------------------------------------------2017/09/28更新 順便祝本身生日快樂---------------------------------------------------------------------------------------------------------------------------------------------------------性能

另外,一些說明是 也一樣能夠加在屬性的get set 方法上 (這樣作就不須要加在屬性上了):

加在get方法上的註解 用來解析json數據

加在set方法上的註解用來寫入數據

相關文章
相關標籤/搜索