View Javadoc
1   /*
2    * Copyright © 2025 rosestack.github.io
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package io.github.rose.core.json;
17  
18  import com.fasterxml.jackson.databind.Module;
19  import com.fasterxml.jackson.databind.module.SimpleModule;
20  import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
21  import com.fasterxml.jackson.datatype.jsr310.deser.*;
22  import com.fasterxml.jackson.datatype.jsr310.ser.*;
23  import io.github.rose.core.util.date.DatePattern;
24  import io.github.rose.processor.AutoService;
25  
26  import java.time.*;
27  
28  /**
29   * @author <a href="mailto:ichensoul@gmail.com">chensoul</a>
30   */
31  @AutoService(Module.class)
32  public class Java8TimeModule extends SimpleModule {
33  
34      public Java8TimeModule() {
35          super(PackageVersion.VERSION);
36  
37          this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMATTER));
38          this.addSerializer(LocalDate.class, new LocalDateSerializer(DatePattern.NORM_DATE_FORMATTER));
39          this.addSerializer(LocalTime.class, new LocalTimeSerializer(DatePattern.NORM_TIME_FORMATTER));
40          this.addSerializer(Instant.class, InstantSerializer.INSTANCE);
41          this.addSerializer(Duration.class, DurationSerializer.INSTANCE);
42  
43          this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DatePattern.NORM_DATETIME_FORMATTER));
44          this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DatePattern.NORM_DATE_FORMATTER));
45          this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DatePattern.NORM_TIME_FORMATTER));
46          this.addDeserializer(Instant.class, InstantDeserializer.INSTANT);
47          this.addDeserializer(Duration.class, DurationDeserializer.INSTANCE);
48      }
49  }