FastJson配置全局LocalDateTime序列化

<div id="content_views" class="markdown_views prism-atelier-sulphurpool-light"> <!-- flowchart 箭頭圖標 勿刪 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path> </svg> <p>最近工做中使用到了JDK1.8 time包,逐步取代之前的Date類,相比之前,time包的API的確好用多了,相關內容在網上有不少,就不累贅了。</p> <p>可是有點蛋疼的地方在於LocalDateTime支持ISO8601標準,即[2018-10-10T05:51:31.924Z]。在MVC中,習慣性用[yyyy-MM-dd HH:mm:ss]。<br> <strong>好在Spring帶有註解。如:</strong></p> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token annotation punctuation">@DateTimeFormat</span><span class="token punctuation">(</span>pattern <span class="token operator">=</span> "yyyy<span class="token operator">-</span>MM<span class="token operator">-</span>dd HH<span class="token operator">:</span>mm<span class="token operator">:</span>ss<span class="token punctuation">)</span> <span class="token keyword">private</span> LocalDateTime ldt<span class="token punctuation">;</span>前端

<span class="token annotation punctuation">@RequestParam</span><span class="token punctuation">(</span>required <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token annotation punctuation">@DateTimeFormat</span><span class="token punctuation">(</span>pattern <span class="token operator">=</span> <span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span> LocalDate datejava

<div class="hljs-button signin" data-title="登陸後複製"></div></code></pre> <p>參數在Param上,可使用@DateTimeFormat在參數或者參數實體的屬性上。對於參數在body上,則能夠經過自定義拓展Formatter或者Converter&lt;String, LocalDateTime&gt;,網上也有不少,這裏就提供一下代碼</p> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token annotation punctuation">@Configuration</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LocalDateTimeFormatConfig</span> <span class="token punctuation">{</span>web

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> Formatter<span class="token generics function"><span class="token punctuation">&lt;</span>LocalDate<span class="token punctuation">&gt;</span></span> <span class="token function">localDateFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Formatter</span><span class="token generics function"><span class="token punctuation">&lt;</span>LocalDate<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token annotation punctuation">@Nullable</span> String <span class="token function">print</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Nullable</span> LocalDate object<span class="token punctuation">,</span> <span class="token annotation punctuation">@Nullable</span> Locale locale<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>Objects<span class="token punctuation">.</span><span class="token function">isNull</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> object<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token annotation punctuation">@Nullable</span> LocalDate <span class="token function">parse</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Nullable</span> String text<span class="token punctuation">,</span> <span class="token annotation punctuation">@Nullable</span> Locale locale<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>StringUtils<span class="token punctuation">.</span><span class="token function">hasText</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> LocalDate<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>text<span class="token punctuation">,</span> DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> Formatter<span class="token generics function"><span class="token punctuation">&lt;</span>LocalDateTime<span class="token punctuation">&gt;</span></span> <span class="token function">localDateTimeFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Formatter</span><span class="token generics function"><span class="token punctuation">&lt;</span>LocalDateTime<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token annotation punctuation">@Nullable</span> String <span class="token function">print</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Nullable</span> LocalDateTime object<span class="token punctuation">,</span> <span class="token annotation punctuation">@Nullable</span> Locale locale<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>Objects<span class="token punctuation">.</span><span class="token function">isNull</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> object<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token annotation punctuation">@Nullable</span> LocalDateTime <span class="token function">parse</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Nullable</span> String text<span class="token punctuation">,</span> <span class="token annotation punctuation">@Nullable</span> Locale locale<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>StringUtils<span class="token punctuation">.</span><span class="token function">hasText</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> LocalDateTime<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>text<span class="token punctuation">,</span> DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> Formatter<span class="token generics function"><span class="token punctuation">&lt;</span>LocalTime<span class="token punctuation">&gt;</span></span> <span class="token function">localTimeFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Formatter</span><span class="token generics function"><span class="token punctuation">&lt;</span>LocalTime<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token annotation punctuation">@Nullable</span> String <span class="token function">print</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Nullable</span> LocalTime object<span class="token punctuation">,</span> <span class="token annotation punctuation">@Nullable</span> Locale locale<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>Objects<span class="token punctuation">.</span><span class="token function">isNull</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> object<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token annotation punctuation">@Nullable</span> LocalTime <span class="token function">parse</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Nullable</span> String text<span class="token punctuation">,</span> <span class="token annotation punctuation">@Nullable</span> Locale locale<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>StringUtils<span class="token punctuation">.</span><span class="token function">hasText</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> LocalTime<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>text<span class="token punctuation">,</span> DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">}</span>markdown

<div class="hljs-button signin" data-title="登陸後複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li><li style="color: rgb(153, 153, 153);">56</li><li style="color: rgb(153, 153, 153);">57</li><li style="color: rgb(153, 153, 153);">58</li><li style="color: rgb(153, 153, 153);">59</li><li style="color: rgb(153, 153, 153);">60</li><li style="color: rgb(153, 153, 153);">61</li><li style="color: rgb(153, 153, 153);">62</li><li style="color: rgb(153, 153, 153);">63</li><li style="color: rgb(153, 153, 153);">64</li><li style="color: rgb(153, 153, 153);">65</li><li style="color: rgb(153, 153, 153);">66</li><li style="color: rgb(153, 153, 153);">67</li></ul></pre> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LocalDateTimeConvertConfig</span> <span class="token punctuation">{</span>app

<span class="token keyword">private</span> String dateTimePattern <span class="token operator">=</span> <span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">;</span>
<span class="token keyword">private</span> String datePattern <span class="token operator">=</span> <span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> Converter<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> LocalDateTime<span class="token punctuation">&gt;</span></span> <span class="token function">LocalDateTimeConvert</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> source <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        DateTimeFormatter df <span class="token operator">=</span> DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span>dateTimePattern<span class="token punctuation">)</span><span class="token punctuation">;</span>
        LocalDateTime date <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            date <span class="token operator">=</span> LocalDateTime<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>source<span class="token punctuation">,</span> df<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> date<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> Converter<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> LocalDate<span class="token punctuation">&gt;</span></span> <span class="token function">localDateConverter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> source <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        DateTimeFormatter df <span class="token operator">=</span> DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span>datePattern<span class="token punctuation">)</span><span class="token punctuation">;</span>
        LocalDate date <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            date <span class="token operator">=</span> LocalDate<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>source<span class="token punctuation">,</span>df<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> date<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 這裏是使用JackSon全局序列化LocalDateTime,而我使用是FastJso,蛋疼</span>
<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> LocalDateTimeSerializer <span class="token function">localDateTimeDeserializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">LocalDateTimeSerializer</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span>dateTimePattern<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> Jackson2ObjectMapperBuilderCustomizer <span class="token function">jackson2ObjectMapperBuilderCustomizer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> builder <span class="token operator">-</span><span class="token operator">&gt;</span> builder<span class="token punctuation">.</span><span class="token function">serializerByType</span><span class="token punctuation">(</span>LocalDateTime<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token function">localDateTimeDeserializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">}</span>ide

<div class="hljs-button signin" data-title="登陸後複製"></div></code></pre> <p>這裏要說的問題就是如今工做項目中遇到的問題,使用的FastJson,而不是MVC默認的JackSon,在序列化LocalDateTime輸出到前端時候,是[2018-10-10T05:51:31.924Z],而想要輸出的是yyyy-MM-dd HH:mm:ss格式,至此咱們須要定製LocalDateTime類的序列化。咱們同樣可使用註解解決,使用FastJson的@JSONField(format = 「yyyy-MM-dd HH:mm:ss」),在須要特殊序列化的成員屬性上。</p> <p>爲了減小注解的使用,嘗試是否可讓FastJson默認根據咱們的規則去序列化LocalDateTime類。<br> <strong>首先祭出常規的FastJsonConfig:</strong></p> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;"> FastJsonConfig fastJsonConfig <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FastJsonConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fastJsonConfig<span class="token punctuation">.</span><span class="token function">setSerializerFeatures</span><span class="token punctuation">(</span> SerializerFeature<span class="token punctuation">.</span>PrettyFormat<span class="token punctuation">,</span> SerializerFeature<span class="token punctuation">.</span>WriteMapNullValue<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> SerializerFeature<span class="token punctuation">.</span>WriteNullStringAsEmpty<span class="token punctuation">)</span><span class="token punctuation">;</span> fastJsonConfig<span class="token punctuation">.</span><span class="token function">setCharset</span><span class="token punctuation">(</span>Charset<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <div class="hljs-button signin" data-title="登陸後複製"></div></code></pre> <p><strong>使用ValueFilter:</strong></p> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;">ValueFilter valueFilter <span class="token operator">=</span> <span class="token punctuation">(</span>o<span class="token punctuation">,</span> s<span class="token punctuation">,</span> o1<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>null <span class="token operator">==</span> o1<span class="token punctuation">)</span> <span class="token punctuation">{</span> o1 <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>o1 <span class="token keyword">instanceof</span> <span class="token class-name">LocalDateTime</span><span class="token punctuation">)</span><span class="token punctuation">{</span> o1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>LocalDateTime<span class="token punctuation">)</span> o1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> o1<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> fastJsonConfig<span class="token punctuation">.</span><span class="token function">setSerializeFilters</span><span class="token punctuation">(</span>valueFilter<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 缺點,這是過濾級別的序列化,序列化的結果取決於o1的值,如將LocalDateTime類型的o1轉換爲String類型的o1,後續將不會調用LocalDateTime的序列化器,由於已經不是LocalDateTime類型了</span> <div class="hljs-button signin" data-title="登陸後複製"></div></code></pre> <p><strong>指定fastJsonConfig的DateFormat:</strong></p> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;">fastJsonConfig<span class="token punctuation">.</span><span class="token function">setDateFormat</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">// 缺點,指定後,將不會使用@JSONField註解上的format屬性,包括並不限於Date類,LocalDateTime類,LocalDate類。(慎用)</span> <div class="hljs-button signin" data-title="登陸後複製"></div></code></pre> <p><strong>自定義LocalDateTimeSerializer實現ObjectSerializer接口,重寫write方法:</strong></p> <pre class="prettyprint"><code class="prism language-java has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LocalDateTimeSerializer</span> <span class="token keyword">implements</span> <span class="token class-name">ObjectSerializer</span> <span class="token punctuation">{</span>svg

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> LocalDateTimeSerializer instance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LocalDateTimeSerializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> String defaultPattern <span class="token operator">=</span> <span class="token string">"yyyy-MM-dd HH:mm:ss"</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token function">LocalDateTimeSerializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span>JSONSerializer serializer<span class="token punctuation">,</span> Object object<span class="token punctuation">,</span> Object fieldName<span class="token punctuation">,</span> Type fieldType<span class="token punctuation">,</span> <span class="token keyword">int</span> features<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    SerializeWriter out <span class="token operator">=</span> serializer<span class="token punctuation">.</span>out<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>object <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        out<span class="token punctuation">.</span><span class="token function">writeNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        LocalDateTime result <span class="token operator">=</span> <span class="token punctuation">(</span>LocalDateTime<span class="token punctuation">)</span> object<span class="token punctuation">;</span>
        out<span class="token punctuation">.</span><span class="token function">writeString</span><span class="token punctuation">(</span>result<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span>defaultPattern<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token punctuation">}</span>ui

SerializeConfig serializeConfig <span class="token operator">=</span> SerializeConfig<span class="token punctuation">.</span>globalInstance<span class="token punctuation">;</span> serializeConfig<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>LocalDateTime<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> LocalDateTimeSerializer<span class="token punctuation">.</span>instance<span class="token punctuation">)</span><span class="token punctuation">;</span> fastJsonConfig<span class="token punctuation">.</span><span class="token function">setSerializeConfig</span><span class="token punctuation">(</span>serializeConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>spa

<span class="token comment">// 缺點,@JSONField註解同樣會失效,僅限於LocalDateTime類</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>.net

<div class="hljs-button signin" data-title="登陸後複製"></div></code></pre> <p>若是使用的是JackSon,只須要在實例化LocalDateTimeSerializer的時候,指定一個DateTimeFormatter便可,方便不少,FastJson完敗,有多是我學藝不精,畢竟我是菜雞。</p> 原文地址:https://blog.csdn.net/luomin369433047/article/details/88972543 </div>

相關文章
相關標籤/搜索