java-time: java.time on Jackson 3 vs 2, @JsonFormat, time zones, Spring Boot 4.2 spring.jackson.*
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
This commit is contained in:
@@ -41,6 +41,7 @@ mvn -q exec:java -Dexec.mainClass=com.ankurm.jackson3.part5polymorphic.F01Serial
|
|||||||
| [Streaming API & JsonNode](https://ankurm.com/jackson-streaming-api-jsonnode/) | [`part6streaming`](src/main/java/com/ankurm/jackson3/part6streaming) | [G01](src/main/java/com/ankurm/jackson3/part6streaming/G01StreamingParserFilter.java) [G02](src/main/java/com/ankurm/jackson3/part6streaming/G02StreamingGenerator.java) [G03](src/main/java/com/ankurm/jackson3/part6streaming/G03TreeModelNavigation.java) [G04](src/main/java/com/ankurm/jackson3/part6streaming/G04ThreeApproachesMeasured.java) | [write-up](docs/part6-streaming.md) |
|
| [Streaming API & JsonNode](https://ankurm.com/jackson-streaming-api-jsonnode/) | [`part6streaming`](src/main/java/com/ankurm/jackson3/part6streaming) | [G01](src/main/java/com/ankurm/jackson3/part6streaming/G01StreamingParserFilter.java) [G02](src/main/java/com/ankurm/jackson3/part6streaming/G02StreamingGenerator.java) [G03](src/main/java/com/ankurm/jackson3/part6streaming/G03TreeModelNavigation.java) [G04](src/main/java/com/ankurm/jackson3/part6streaming/G04ThreeApproachesMeasured.java) | [write-up](docs/part6-streaming.md) |
|
||||||
| [Streaming API & JsonNode — the 1 GiB benchmark](https://ankurm.com/jackson-streaming-api-jsonnode/) | [`streaming-huge-json`](streaming-huge-json) (standalone module, JDK 25, Jackson 3.2.4) | [GenerateHugeJson](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/GenerateHugeJson.java) [Scan](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/Scan.java) [NestedTrap](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/NestedTrap.java) [TruncatedFile](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/TruncatedFile.java) [ReadLimits](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/ReadLimits.java) | [README](streaming-huge-json/README.md), [output/](streaming-huge-json/output) |
|
| [Streaming API & JsonNode — the 1 GiB benchmark](https://ankurm.com/jackson-streaming-api-jsonnode/) | [`streaming-huge-json`](streaming-huge-json) (standalone module, JDK 25, Jackson 3.2.4) | [GenerateHugeJson](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/GenerateHugeJson.java) [Scan](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/Scan.java) [NestedTrap](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/NestedTrap.java) [TruncatedFile](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/TruncatedFile.java) [ReadLimits](streaming-huge-json/src/main/java/com/ankurm/jackson3/streaminghuge/ReadLimits.java) | [README](streaming-huge-json/README.md), [output/](streaming-huge-json/output) |
|
||||||
| [ObjectMapper and JsonMapper (Jackson 2 and 3)](https://ankurm.com/jackson-objectmapper-guide/) | [`objectmapper-jsonmapper`](objectmapper-jsonmapper) (standalone module, JDK 25, Jackson 3.3.0 and 2.23.0 side by side) | [P01](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P01TwoMappersSideBySide.java) [P02](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P02ImmutableMapper.java) [P03](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P03ReadWriteCookbook.java) [P04](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P04ConfigRecipes.java) [P05](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P05JavaTime.java) [P06](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P06Exceptions.java) [P07](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P07SharedMapperThreads.java) | [README](objectmapper-jsonmapper/README.md), [output/](objectmapper-jsonmapper/output) |
|
| [ObjectMapper and JsonMapper (Jackson 2 and 3)](https://ankurm.com/jackson-objectmapper-guide/) | [`objectmapper-jsonmapper`](objectmapper-jsonmapper) (standalone module, JDK 25, Jackson 3.3.0 and 2.23.0 side by side) | [P01](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P01TwoMappersSideBySide.java) [P02](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P02ImmutableMapper.java) [P03](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P03ReadWriteCookbook.java) [P04](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P04ConfigRecipes.java) [P05](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P05JavaTime.java) [P06](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P06Exceptions.java) [P07](objectmapper-jsonmapper/src/main/java/com/ankurm/objectmapper/P07SharedMapperThreads.java) | [README](objectmapper-jsonmapper/README.md), [output/](objectmapper-jsonmapper/output) |
|
||||||
|
| [JavaTimeModule in Jackson 3](https://ankurm.com/jackson-3-javatimemodule-java-time-date-formats/) | [`java-time`](java-time) (standalone module, JDK 25, Jackson 3.3.0 and 2.23.0, plus Spring Boot 4.2.0) | [T01..T11](java-time/src/main/java/com/ankurm/javatime), [Boot app](java-time/boot) | [README](java-time/README.md), [output/](java-time/output) |
|
||||||
| [Security Best Practices](https://ankurm.com/jackson-security-best-practices/) | [`part7security`](src/main/java/com/ankurm/jackson3/part7security) | [H01](src/main/java/com/ankurm/jackson3/part7security/H01SafePolymorphismByAnnotation.java) [H02](src/main/java/com/ankurm/jackson3/part7security/H02DefaultTypingRemoved.java) [H03](src/main/java/com/ankurm/jackson3/part7security/H03PolymorphicTypeValidatorAllowlist.java) [H04](src/main/java/com/ankurm/jackson3/part7security/H04StreamReadConstraints.java) [H05](src/main/java/com/ankurm/jackson3/part7security/H05NeverDeserialiseIntoObject.java) | [write-up](docs/part7-security.md) |
|
| [Security Best Practices](https://ankurm.com/jackson-security-best-practices/) | [`part7security`](src/main/java/com/ankurm/jackson3/part7security) | [H01](src/main/java/com/ankurm/jackson3/part7security/H01SafePolymorphismByAnnotation.java) [H02](src/main/java/com/ankurm/jackson3/part7security/H02DefaultTypingRemoved.java) [H03](src/main/java/com/ankurm/jackson3/part7security/H03PolymorphicTypeValidatorAllowlist.java) [H04](src/main/java/com/ankurm/jackson3/part7security/H04StreamReadConstraints.java) [H05](src/main/java/com/ankurm/jackson3/part7security/H05NeverDeserialiseIntoObject.java) | [write-up](docs/part7-security.md) |
|
||||||
| — (beyond the posts) | [`beyond`](src/main/java/com/ankurm/jackson3/beyond) | [Y01](src/main/java/com/ankurm/jackson3/beyond/Y01UncheckedExceptions.java) [Y02](src/main/java/com/ankurm/jackson3/beyond/Y02TrailingTokens.java) [Y03](src/main/java/com/ankurm/jackson3/beyond/Y03DateTimeDefaults.java) [Y04](src/main/java/com/ankurm/jackson3/beyond/Y04ImmutableMapperAndReaders.java) [Y05](src/main/java/com/ankurm/jackson3/beyond/Y05CreatorDetection.java) [Y06](src/main/java/com/ankurm/jackson3/beyond/Y06RecyclerPoolTuning.java) | [write-up](docs/beyond.md) |
|
| — (beyond the posts) | [`beyond`](src/main/java/com/ankurm/jackson3/beyond) | [Y01](src/main/java/com/ankurm/jackson3/beyond/Y01UncheckedExceptions.java) [Y02](src/main/java/com/ankurm/jackson3/beyond/Y02TrailingTokens.java) [Y03](src/main/java/com/ankurm/jackson3/beyond/Y03DateTimeDefaults.java) [Y04](src/main/java/com/ankurm/jackson3/beyond/Y04ImmutableMapperAndReaders.java) [Y05](src/main/java/com/ankurm/jackson3/beyond/Y05CreatorDetection.java) [Y06](src/main/java/com/ankurm/jackson3/beyond/Y06RecyclerPoolTuning.java) | [write-up](docs/beyond.md) |
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# java-time: java.time on Jackson 3 (and what changed from Jackson 2)
|
||||||
|
|
||||||
|
Companion code for
|
||||||
|
[JavaTimeModule in Jackson 3: java.time Support Built In, Date Formats, Time Zones and Migration](https://ankurm.com/jackson-3-javatimemodule-java-time-date-formats/).
|
||||||
|
|
||||||
|
Verified with JDK Temurin 25.0.5.1, Maven 3.9.11, Jackson 3.3.0 and 2.23.0
|
||||||
|
(`jackson-datatype-jsr310` 2.23.0). The Spring Boot part uses Boot 4.2.0, which manages Jackson 3.1.8.
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./scripts/run-all.sh # plain Jackson programs T01..T11 and the two compile errors
|
||||||
|
./scripts/run-versions.sh # T11 on Jackson 3.1.8, 3.2.4 and 3.3.0
|
||||||
|
./scripts/run-boot.sh # the Spring Boot app in boot/, one start per configuration
|
||||||
|
```
|
||||||
|
|
||||||
|
Set `MVN_ARGS` to pass extra Maven options. The scripts run the JVM with `-Duser.timezone=UTC`;
|
||||||
|
`T05TimeZones` changes the zone itself.
|
||||||
|
|
||||||
|
## Files
|
||||||
|
|
||||||
|
| Program | Shows | Output |
|
||||||
|
|---|---|---|
|
||||||
|
| [T01NoModuleNeeded](src/main/java/com/ankurm/javatime/T01NoModuleNeeded.java) | Jackson 2 needs a module, Jackson 3 does not | [01](output/01-NoModuleNeeded.txt) |
|
||||||
|
| [T02OutputDiff](src/main/java/com/ankurm/javatime/T02OutputDiff.java) | Four mappers, twenty values | [02](output/02-OutputDiff.txt) |
|
||||||
|
| [T03FeatureDefaults](src/main/java/com/ankurm/javatime/T03FeatureDefaults.java) | Every `DateTimeFeature` default | [03](output/03-FeatureDefaults.txt) |
|
||||||
|
| [T04ReadingOffsets](src/main/java/com/ankurm/javatime/T04ReadingOffsets.java) | Offsets and zone ids on the way in | [04](output/04-ReadingOffsets.txt) |
|
||||||
|
| [T05TimeZones](src/main/java/com/ankurm/javatime/T05TimeZones.java) | JVM zone vs mapper zone | [05](output/05-TimeZones.txt) |
|
||||||
|
| [T06JsonFormat](src/main/java/com/ankurm/javatime/T06JsonFormat.java) | `@JsonFormat` per field | [06](output/06-JsonFormat.txt) |
|
||||||
|
| [T07FormatTraps](src/main/java/com/ankurm/javatime/T07FormatTraps.java) | Lenient dates, `YYYY`, quoted `Z` | [07](output/07-FormatTraps.txt) |
|
||||||
|
| [T08EpochNumbers](src/main/java/com/ankurm/javatime/T08EpochNumbers.java) | Seconds vs milliseconds | [08](output/08-EpochNumbers.txt) |
|
||||||
|
| [T09GlobalFormats](src/main/java/com/ankurm/javatime/T09GlobalFormats.java) | Mapper-wide formats | [09](output/09-GlobalFormats.txt) |
|
||||||
|
| [T10StoredPayloads](src/main/java/com/ankurm/javatime/T10StoredPayloads.java) | Reading across the two majors | [10](output/10-StoredPayloads.txt) |
|
||||||
|
| [T11MonthTrap](src/main/java/com/ankurm/javatime/T11MonthTrap.java) | `Month` as a number | [11](output/11-MonthTrap.txt), [12](output/12-month-across-versions.txt) |
|
||||||
|
| [boot/](boot/src/main/java/com/ankurm/javatime/boot) | `spring.jackson.*` on Boot 4.2.0 | [13](output/13-spring-boot.txt) |
|
||||||
|
| [src/broken/](src/broken) | Two Jackson 2 habits that no longer compile | [00](output/00-broken-compile-errors.txt) |
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>4.2.0</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>com.ankurm</groupId>
|
||||||
|
<artifactId>java-time-boot</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<name>java-time-boot</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>25</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Brings in spring-boot-starter-jackson; the Jackson 3 version comes from the Boot BOM. -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package com.ankurm.javatime.boot;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.SpringBootVersion;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Starts the app once per configuration on a random port and calls it over real HTTP. */
|
||||||
|
public class BootDemo {
|
||||||
|
|
||||||
|
static final HttpClient HTTP = HttpClient.newHttpClient();
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
|
System.out.println("Spring Boot " + SpringBootVersion.getVersion()
|
||||||
|
+ ", Jackson " + tools.jackson.databind.cfg.PackageVersion.VERSION);
|
||||||
|
|
||||||
|
run("no spring.jackson.* properties");
|
||||||
|
run("write-dates-as-timestamps",
|
||||||
|
"--spring.jackson.datatype.datetime.write-dates-as-timestamps=true");
|
||||||
|
run("date-format", "--spring.jackson.date-format=dd/MM/yyyy HH:mm");
|
||||||
|
run("time-zone", "--spring.jackson.time-zone=Asia/Kolkata");
|
||||||
|
run("keep the client's offset",
|
||||||
|
"--spring.jackson.datatype.datetime.adjust-dates-to-context-time-zone=false");
|
||||||
|
run("use-jackson2-defaults", "--spring.jackson.use-jackson2-defaults=true");
|
||||||
|
run("spring.mvc.format.date", "--spring.mvc.format.date=dd/MM/yyyy");
|
||||||
|
run("profile indian-dates (JsonMapperBuilderCustomizer)", "--spring.profiles.active=indian-dates");
|
||||||
|
run("profile recommended (application-recommended.yml)", "--spring.profiles.active=recommended");
|
||||||
|
run("Boot 3 property name left in place",
|
||||||
|
"--spring.jackson.serialization.write-dates-as-timestamps=false");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void run(String title, String... properties) throws Exception {
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("==== " + title);
|
||||||
|
for (String p : properties) {
|
||||||
|
System.out.println(p.substring(2));
|
||||||
|
}
|
||||||
|
List<String> args = new ArrayList<>(List.of(properties));
|
||||||
|
args.add("--server.port=0");
|
||||||
|
args.add("--spring.main.banner-mode=off");
|
||||||
|
args.add("--logging.level.root=OFF");
|
||||||
|
try (ConfigurableApplicationContext context =
|
||||||
|
SpringApplication.run(DemoApplication.class, args.toArray(String[]::new))) {
|
||||||
|
String base = "http://localhost:" + context.getEnvironment().getProperty("local.server.port");
|
||||||
|
call("GET /event", HttpRequest.newBuilder(URI.create(base + "/event")).build());
|
||||||
|
call("POST /booking", HttpRequest.newBuilder(URI.create(base + "/booking"))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString("{\"startsAt\":\"2026-12-15T10:30:00+05:30\"}"))
|
||||||
|
.build());
|
||||||
|
call("GET /search?day=2026-12-15", HttpRequest.newBuilder(URI.create(base + "/search?day=2026-12-15")).build());
|
||||||
|
call("GET /search?day=15/12/2026", HttpRequest.newBuilder(URI.create(base + "/search?day=15/12/2026")).build());
|
||||||
|
} catch (Exception e) {
|
||||||
|
Throwable root = e;
|
||||||
|
while (root.getCause() != null) {
|
||||||
|
root = root.getCause();
|
||||||
|
}
|
||||||
|
System.out.println("startup FAILED: " + e.getClass().getSimpleName());
|
||||||
|
System.out.println("root cause : " + root.getClass().getSimpleName() + ": " + root.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void call(String label, HttpRequest request) throws Exception {
|
||||||
|
HttpResponse<String> response = HTTP.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
String body = response.body();
|
||||||
|
if (response.statusCode() != 200) {
|
||||||
|
body = "(" + JsonMapper.shared().readTree(body).path("error").asString() + ")";
|
||||||
|
}
|
||||||
|
System.out.println(label + " -> " + response.statusCode() + " " + body);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.ankurm.javatime.boot;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
class DemoApplication {
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.ankurm.javatime.boot;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/** Demo endpoints: one writes date types, one reads them from a body, one from a query string. */
|
||||||
|
@RestController
|
||||||
|
class EventController {
|
||||||
|
|
||||||
|
record Event(LocalDate day, LocalDateTime wallClock, Instant createdAt,
|
||||||
|
OffsetDateTime startsAt, Duration length, Month month, Date legacy) { }
|
||||||
|
|
||||||
|
record Booking(OffsetDateTime startsAt) { }
|
||||||
|
|
||||||
|
@GetMapping("/event")
|
||||||
|
Event event() {
|
||||||
|
LocalDateTime wall = LocalDateTime.of(2026, 12, 15, 10, 30);
|
||||||
|
OffsetDateTime start = wall.atOffset(ZoneOffset.ofHoursMinutes(5, 30));
|
||||||
|
return new Event(wall.toLocalDate(), wall, start.toInstant(), start,
|
||||||
|
Duration.ofMinutes(90), Month.NOVEMBER, Date.from(start.toInstant()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Shows what the controller actually receives after Jackson has read the body. */
|
||||||
|
@PostMapping("/booking")
|
||||||
|
Map<String, String> booking(@RequestBody Booking booking) {
|
||||||
|
return Map.of("received", booking.startsAt().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A query parameter is converted by Spring MVC, not by Jackson. */
|
||||||
|
@GetMapping("/search")
|
||||||
|
Map<String, String> search(@RequestParam LocalDate day) {
|
||||||
|
return Map.of("parsed", day.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.ankurm.javatime.boot;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import tools.jackson.core.JsonGenerator;
|
||||||
|
import tools.jackson.databind.SerializationContext;
|
||||||
|
import tools.jackson.databind.ext.javatime.deser.LocalDateDeserializer;
|
||||||
|
import tools.jackson.databind.module.SimpleModule;
|
||||||
|
import tools.jackson.databind.ser.std.StdSerializer;
|
||||||
|
|
||||||
|
/** Global rules that properties cannot express. Active with the "indian-dates" profile. */
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@Profile("indian-dates")
|
||||||
|
class IndianDatesCustomizer {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
JsonMapperBuilderCustomizer indianDates() {
|
||||||
|
// Month by name, with a serializer because the Jackson that Boot 4.2.0 manages
|
||||||
|
// ignores a STRING shape override for Month.
|
||||||
|
SimpleModule months = new SimpleModule().addSerializer(Month.class, new StdSerializer<Month>(Month.class) {
|
||||||
|
@Override
|
||||||
|
public void serialize(Month value, JsonGenerator gen, SerializationContext context) {
|
||||||
|
gen.writeString(value.name());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return builder -> builder
|
||||||
|
.withConfigOverride(LocalDate.class,
|
||||||
|
o -> o.setFormat(JsonFormat.Value.forPattern("dd/MM/yyyy")))
|
||||||
|
.addModule(months);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# A starting point for a new Spring Boot 4 API. Active with the "recommended" profile.
|
||||||
|
spring:
|
||||||
|
jackson:
|
||||||
|
datatype:
|
||||||
|
datetime:
|
||||||
|
# Hand controllers the offset the client sent instead of converting it to the mapper zone.
|
||||||
|
adjust-dates-to-context-time-zone: false
|
||||||
|
mvc:
|
||||||
|
format:
|
||||||
|
# Query and path parameters are not read by Jackson; state their format separately.
|
||||||
|
date: iso
|
||||||
|
date-time: iso
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
=== OldFlagLocation.java ===
|
||||||
|
OldFlagLocation.java:7: error: cannot find symbol
|
||||||
|
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||||
|
^
|
||||||
|
1 error
|
||||||
|
|
||||||
|
=== RegisterJackson2Module.java ===
|
||||||
|
RegisterJackson2Module.java:8: error: incompatible types: JavaTimeModule cannot be converted to JacksonModule
|
||||||
|
.addModule(new JavaTimeModule())
|
||||||
|
^
|
||||||
|
1 error
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
== Jackson 2.23.0, plain ObjectMapper
|
||||||
|
InvalidDefinitionException: Java 8 date/time type `java.time.LocalDate` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (or disable `MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_TIMES`) (through reference chain: com.ankurm.javatime.Meeting["day"])
|
||||||
|
|
||||||
|
== Jackson 2 + JavaTimeModule
|
||||||
|
{"day":[2026,12,15],"wallClock":[2026,12,15,10,30],"createdAt":1797310800.000000000,"startsAt":1797310800.000000000,"reminderAt":1797310800.000000000}
|
||||||
|
|
||||||
|
== Jackson 3.3.0, JsonMapper.builder().build()
|
||||||
|
{"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","reminderAt":"2026-12-15T10:30:00+05:30"}
|
||||||
|
registered modules : 0
|
||||||
|
read back : Meeting[day=2026-12-15, wallClock=2026-12-15T10:30, createdAt=2026-12-15T05:00:00Z, startsAt=2026-12-15T05:00Z, reminderAt=2026-12-15T05:00Z]
|
||||||
|
|
||||||
|
present : com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||||
|
absent : tools.jackson.datatype.jsr310.JavaTimeModule
|
||||||
|
present : tools.jackson.databind.ext.javatime.JavaTimeInitializer
|
||||||
|
present : tools.jackson.databind.ext.javatime.ser.InstantSerializer
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
A = Jackson 2.23.0 + JavaTimeModule
|
||||||
|
B = A + disable(WRITE_DATES_AS_TIMESTAMPS)
|
||||||
|
C = Jackson 3.3.0 defaults
|
||||||
|
D = Jackson 3.3.0 builderWithJackson2Defaults()
|
||||||
|
|
||||||
|
LocalDate
|
||||||
|
A [2026,12,15]
|
||||||
|
B "2026-12-15"
|
||||||
|
C "2026-12-15"
|
||||||
|
D [2026,12,15]
|
||||||
|
|
||||||
|
LocalTime
|
||||||
|
A [10,30]
|
||||||
|
B "10:30:00"
|
||||||
|
C "10:30:00"
|
||||||
|
D [10,30]
|
||||||
|
|
||||||
|
LocalDateTime
|
||||||
|
A [2026,12,15,10,30]
|
||||||
|
B "2026-12-15T10:30:00"
|
||||||
|
C "2026-12-15T10:30:00"
|
||||||
|
D [2026,12,15,10,30]
|
||||||
|
|
||||||
|
LocalDateTime+nanos
|
||||||
|
A [2026,12,15,10,30,0,123456000]
|
||||||
|
B "2026-12-15T10:30:00.123456"
|
||||||
|
C "2026-12-15T10:30:00.123456"
|
||||||
|
D [2026,12,15,10,30,0,123456000]
|
||||||
|
|
||||||
|
Instant
|
||||||
|
A 1797310800.000000000
|
||||||
|
B "2026-12-15T05:00:00Z"
|
||||||
|
C "2026-12-15T05:00:00Z"
|
||||||
|
D 1797310800.000000000
|
||||||
|
|
||||||
|
OffsetDateTime
|
||||||
|
A 1797310800.000000000
|
||||||
|
B "2026-12-15T10:30:00+05:30"
|
||||||
|
C "2026-12-15T10:30:00+05:30"
|
||||||
|
D 1797310800.000000000
|
||||||
|
|
||||||
|
ZonedDateTime
|
||||||
|
A 1797310800.000000000
|
||||||
|
B "2026-12-15T10:30:00+05:30"
|
||||||
|
C "2026-12-15T10:30:00+05:30"
|
||||||
|
D 1797310800.000000000
|
||||||
|
|
||||||
|
Duration
|
||||||
|
A 5400.000000000
|
||||||
|
B 5400.000000000
|
||||||
|
C "PT1H30M"
|
||||||
|
D 5400.000000000
|
||||||
|
|
||||||
|
Period
|
||||||
|
A "P1Y2M3D"
|
||||||
|
B "P1Y2M3D"
|
||||||
|
C "P1Y2M3D"
|
||||||
|
D "P1Y2M3D"
|
||||||
|
|
||||||
|
Year
|
||||||
|
A 2026
|
||||||
|
B "2026"
|
||||||
|
C 2026
|
||||||
|
D 2026
|
||||||
|
|
||||||
|
YearMonth
|
||||||
|
A [2026,12]
|
||||||
|
B "2026-12"
|
||||||
|
C "2026-12"
|
||||||
|
D [2026,12]
|
||||||
|
|
||||||
|
MonthDay
|
||||||
|
A "--12-15"
|
||||||
|
B "--12-15"
|
||||||
|
C "--12-15"
|
||||||
|
D "--12-15"
|
||||||
|
|
||||||
|
Month
|
||||||
|
A "DECEMBER"
|
||||||
|
B "DECEMBER"
|
||||||
|
C 12
|
||||||
|
D 11
|
||||||
|
|
||||||
|
DayOfWeek
|
||||||
|
A "TUESDAY"
|
||||||
|
B "TUESDAY"
|
||||||
|
C "TUESDAY"
|
||||||
|
D "TUESDAY"
|
||||||
|
|
||||||
|
ZoneId
|
||||||
|
A "Asia/Kolkata"
|
||||||
|
B "Asia/Kolkata"
|
||||||
|
C "Asia/Kolkata"
|
||||||
|
D "Asia/Kolkata"
|
||||||
|
|
||||||
|
ZoneOffset
|
||||||
|
A "+05:30"
|
||||||
|
B "+05:30"
|
||||||
|
C "+05:30"
|
||||||
|
D "+05:30"
|
||||||
|
|
||||||
|
java.util.Date
|
||||||
|
A 1797310800000
|
||||||
|
B "2026-12-15T05:00:00.000+00:00"
|
||||||
|
C "2026-12-15T05:00:00.000Z"
|
||||||
|
D 1797310800000
|
||||||
|
|
||||||
|
java.sql.Date
|
||||||
|
A 1797292800000
|
||||||
|
B "2026-12-15"
|
||||||
|
C "2026-12-15T00:00:00.000Z"
|
||||||
|
D 1797292800000
|
||||||
|
|
||||||
|
Map<LocalDate,..> key
|
||||||
|
A {"2026-12-15":1}
|
||||||
|
B {"2026-12-15":1}
|
||||||
|
C {"2026-12-15":1}
|
||||||
|
D {"2026-12-15":1}
|
||||||
|
|
||||||
|
Map<Date,..> key
|
||||||
|
A {"2026-12-15T05:00:00.000+00:00":1}
|
||||||
|
B {"2026-12-15T05:00:00.000+00:00":1}
|
||||||
|
C {"2026-12-15T05:00:00.000Z":1}
|
||||||
|
D {"2026-12-15T05:00:00.000+00:00":1}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
DateTimeFeature default with Jackson 2 defaults
|
||||||
|
ADJUST_DATES_TO_CONTEXT_TIME_ZONE true true
|
||||||
|
ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS false false
|
||||||
|
ALWAYS_WRITE_SUBSECOND_DIGITS false false
|
||||||
|
NORMALIZE_DESERIALIZED_ZONE_ID true true
|
||||||
|
ONE_BASED_MONTHS true false
|
||||||
|
READ_DATE_TIMESTAMPS_AS_NANOSECONDS true true
|
||||||
|
TRUNCATE_TO_MSECS_ON_READ false false
|
||||||
|
TRUNCATE_TO_MSECS_ON_WRITE false false
|
||||||
|
USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING false false
|
||||||
|
WRITE_DATES_AS_TIMESTAMPS false true
|
||||||
|
WRITE_DATE_KEYS_AS_TIMESTAMPS false false
|
||||||
|
WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS true true
|
||||||
|
WRITE_DATES_WITH_CONTEXT_TIME_ZONE true true
|
||||||
|
WRITE_DATES_WITH_ZONE_ID false false
|
||||||
|
WRITE_DURATIONS_AS_TIMESTAMPS false true
|
||||||
|
WRITE_UTC_AS_OFFSET false true
|
||||||
|
|
||||||
|
default time zone (mapper): UTC
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
input: "2026-12-15T10:30:00+05:30"
|
||||||
|
|
||||||
|
== Jackson 2.23.0 + JavaTimeModule, defaults
|
||||||
|
OffsetDateTime : 2026-12-15T05:00Z
|
||||||
|
ZonedDateTime : 2026-12-15T05:00Z
|
||||||
|
Instant : 2026-12-15T05:00:00Z
|
||||||
|
|
||||||
|
== Jackson 3 defaults (mapper zone UTC)
|
||||||
|
OffsetDateTime : 2026-12-15T05:00Z
|
||||||
|
ZonedDateTime : 2026-12-15T05:00Z
|
||||||
|
Instant : 2026-12-15T05:00:00Z
|
||||||
|
|
||||||
|
== Jackson 3, defaultTimeZone(Asia/Kolkata)
|
||||||
|
OffsetDateTime : 2026-12-15T10:30+05:30
|
||||||
|
ZonedDateTime : 2026-12-15T10:30+05:30[Asia/Kolkata]
|
||||||
|
Instant : 2026-12-15T05:00:00Z
|
||||||
|
|
||||||
|
== Jackson 3, ADJUST_DATES_TO_CONTEXT_TIME_ZONE disabled
|
||||||
|
OffsetDateTime : 2026-12-15T10:30+05:30
|
||||||
|
ZonedDateTime : 2026-12-15T10:30+05:30
|
||||||
|
Instant : 2026-12-15T05:00:00Z
|
||||||
|
|
||||||
|
== A region id survives only if you ask for it
|
||||||
|
original : 2026-12-15T10:30+05:30[Asia/Kolkata]
|
||||||
|
written by default : "2026-12-15T10:30:00+05:30"
|
||||||
|
read back : 2026-12-15T10:30+05:30
|
||||||
|
WRITE_DATES_WITH_ZONE_ID : "2026-12-15T10:30:00+05:30[Asia/Kolkata]"
|
||||||
|
read back : 2026-12-15T10:30+05:30[Asia/Kolkata]
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
== JVM zone UTC, mapper defaults
|
||||||
|
{
|
||||||
|
"localDateTime" : "2026-12-15T10:30:00",
|
||||||
|
"instant" : "2026-12-15T05:00:00Z",
|
||||||
|
"offset" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"zoned" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"utilDate" : "2026-12-15T05:00:00.000Z",
|
||||||
|
"sqlDate" : "2026-12-15T00:00:00.000Z"
|
||||||
|
}
|
||||||
|
|
||||||
|
== JVM zone Asia/Kolkata, mapper defaults
|
||||||
|
{
|
||||||
|
"localDateTime" : "2026-12-15T10:30:00",
|
||||||
|
"instant" : "2026-12-15T05:00:00Z",
|
||||||
|
"offset" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"zoned" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"utilDate" : "2026-12-15T05:00:00.000Z",
|
||||||
|
"sqlDate" : "2026-12-14T18:30:00.000Z"
|
||||||
|
}
|
||||||
|
|
||||||
|
== JVM zone America/New_York, mapper defaults
|
||||||
|
{
|
||||||
|
"localDateTime" : "2026-12-15T10:30:00",
|
||||||
|
"instant" : "2026-12-15T05:00:00Z",
|
||||||
|
"offset" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"zoned" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"utilDate" : "2026-12-15T05:00:00.000Z",
|
||||||
|
"sqlDate" : "2026-12-15T05:00:00.000Z"
|
||||||
|
}
|
||||||
|
|
||||||
|
== JVM zone UTC, defaultTimeZone(Asia/Kolkata)
|
||||||
|
{
|
||||||
|
"localDateTime" : "2026-12-15T10:30:00",
|
||||||
|
"instant" : "2026-12-15T05:00:00Z",
|
||||||
|
"offset" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"zoned" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"utilDate" : "2026-12-15T10:30:00.000+05:30",
|
||||||
|
"sqlDate" : "2026-12-15T05:30:00.000+05:30"
|
||||||
|
}
|
||||||
|
|
||||||
|
== defaultTimeZone(Asia/Kolkata), WRITE_DATES_WITH_CONTEXT_TIME_ZONE disabled
|
||||||
|
{
|
||||||
|
"localDateTime" : "2026-12-15T10:30:00",
|
||||||
|
"instant" : "2026-12-15T05:00:00Z",
|
||||||
|
"offset" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"zoned" : "2026-12-15T10:30:00+05:30",
|
||||||
|
"utilDate" : "2026-12-15T10:30:00.000+05:30",
|
||||||
|
"sqlDate" : "2026-12-15T05:30:00.000+05:30"
|
||||||
|
}
|
||||||
|
|
||||||
|
== @JsonFormat(pattern) on offset types, values are at +05:30
|
||||||
|
mapper zone not set : {"offset":"2026-12-15 10:30 +05:30","zoned":"2026-12-15 10:30 +05:30"}
|
||||||
|
defaultTimeZone(Europe/London) : {"offset":"2026-12-15 05:00 Z","zoned":"2026-12-15 05:00 Z"}
|
||||||
|
London, CONTEXT_TIME_ZONE disabled: {"offset":"2026-12-15 10:30 +05:30","zoned":"2026-12-15 10:30 +05:30"}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"issued" : "15/12/2026",
|
||||||
|
"dueAt" : "15-12-2026 10:30",
|
||||||
|
"paidAt" : "2026-12-15 10:30:00",
|
||||||
|
"signedAt" : "2026-12-15T10:30+05:30",
|
||||||
|
"printed" : "15 décembre 2026",
|
||||||
|
"epochSeconds" : 1797310800.000000000,
|
||||||
|
"writeOnlyMillis" : 1797310800000,
|
||||||
|
"epochMillis" : 1797310800000,
|
||||||
|
"asArray" : [ 2026, 12, 15 ],
|
||||||
|
"monthDefault" : 12,
|
||||||
|
"monthName" : "DECEMBER"
|
||||||
|
}
|
||||||
|
|
||||||
|
issued : 2026-12-15
|
||||||
|
dueAt : 2026-12-15T10:30
|
||||||
|
paidAt : 2026-12-15T05:00:00Z
|
||||||
|
signedAt : 2026-12-15T10:30+05:30
|
||||||
|
printed : 2026-12-15
|
||||||
|
epochSeconds : 2026-12-15T05:00:00Z
|
||||||
|
writeOnlyMillis : +58924-07-06T08:00:00Z
|
||||||
|
epochMillis : 2026-12-15T05:00:00Z
|
||||||
|
asArray : 2026-12-15
|
||||||
|
monthDefault : DECEMBER
|
||||||
|
monthName : DECEMBER
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
== 30 February
|
||||||
|
no pattern : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2026-02-30": Failed to deserialize `java.time.LocalDate` (with format 'Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'): (java.time.format.DateTimeParseException) Text '2026-02-30' could not be parsed: Invalid date 'FEBRUARY 30'
|
||||||
|
pattern : Default[day=2026-02-28]
|
||||||
|
lenient=FALSE, yyyy : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2026-02-30": Failed to deserialize `java.time.LocalDate` (with format 'Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'): (java.time.format.DateTimeParseException) Text '2026-02-30' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=2, DayOfMonth=30, YearOfEra=2026},ISO of type java.time.format.Parsed
|
||||||
|
lenient=FALSE, yyyy ok : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2026-02-28": Failed to deserialize `java.time.LocalDate` (with format 'Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'): (java.time.format.DateTimeParseException) Text '2026-02-28' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=2, DayOfMonth=28, YearOfEra=2026},ISO of type java.time.format.Parsed
|
||||||
|
lenient=FALSE, uuuu : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2026-02-30": Failed to deserialize `java.time.LocalDate` (with format 'Value(Year,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'): (java.time.format.DateTimeParseException) Text '2026-02-30' could not be parsed: Invalid date 'FEBRUARY 30'
|
||||||
|
lenient=FALSE, uuuu ok : StrictU[day=2026-02-28]
|
||||||
|
|
||||||
|
== YYYY is the week-based year, yyyy is the calendar year
|
||||||
|
value 2026-12-29, YYYY : {"day":"2027-12-29"}
|
||||||
|
value 2026-12-29, yyyy : {"day":"2026-12-29"}
|
||||||
|
YYYY read : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2026-12-29": Failed to deserialize `java.time.LocalDate` (with format 'Localized(WeekBasedYear,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'): (java.time.format.DateTimeParseException) Text '2026-12-29' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2026, MonthOfYear=12, DayOfMonth=29},ISO of type java.time.format.Parsed
|
||||||
|
|
||||||
|
== A quoted 'Z' is just a letter
|
||||||
|
10:30 India wall clock : {"at":"2026-12-15T10:30:00Z"}
|
||||||
|
|
||||||
|
== A pattern on a type that has no calendar fields
|
||||||
|
Instant, no timezone : DatabindException: Unsupported field: YearOfEra
|
||||||
|
ZonedDateTime : {"at":"2026-12-15 10:30"}
|
||||||
|
Instant, no timezone : InvalidFormatException: Cannot deserialize value of type `java.time.Instant` from String "2026-12-15 05:00": Failed to deserialize `java.time.Instant` (with format 'Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)' 'Value(HourOfDay,2)':'Value(MinuteOfHour,2)'): (java.time.DateTimeException) Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2026-12-15T05:00 of type java.time.format.Parsed
|
||||||
|
ZonedDateTime : InvalidFormatException: Cannot deserialize value of type `java.time.ZonedDateTime` from String "2026-12-15 10:30": Failed to deserialize `java.time.ZonedDateTime` (with format 'Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)' 'Value(HourOfDay,2)':'Value(MinuteOfHour,2)'): (java.time.DateTimeException) Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2026-12-15T10:30 of type java.time.format.Parsed
|
||||||
|
|
||||||
|
== Strings that carry more, or less, than the target type
|
||||||
|
LocalDateTime <- ...Z : 2026-12-15T05:00
|
||||||
|
LocalDateTime <- +05:30 : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2026-12-15T10:30:00+05:30": Failed to deserialize `java.time.LocalDateTime` (with format 'ParseCaseSensitive(false)(Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2))'T'(Value(HourOfDay,2)':'Value(MinuteOfHour,2)[':'Value(SecondOfMinute,2)[Fraction(NanoOfSecond,0,9,DecimalPoint)]])'): (java.time.format.DateTimeParseException) Text '2026-12-15T10:30:00+05:30' could not be parsed, unparsed text found at index 19
|
||||||
|
LocalDate <- date-time : 2026-12-15
|
||||||
|
LocalDate <- ...Z : 2026-12-15
|
||||||
|
Instant <- no offset : InvalidFormatException: Cannot deserialize value of type `java.time.Instant` from String "2026-12-15T10:30:00": Failed to deserialize `java.time.Instant` (with format 'ParseCaseSensitive(false)Instant()'): (java.time.format.DateTimeParseException) Text '2026-12-15T10:30:00' could not be parsed at index 19
|
||||||
|
LocalDateTime <- space : InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2026-12-15 10:30:00": Failed to deserialize `java.time.LocalDateTime` (with format 'ParseCaseSensitive(false)(Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2))'T'(Value(HourOfDay,2)':'Value(MinuteOfHour,2)[':'Value(SecondOfMinute,2)[Fraction(NanoOfSecond,0,9,DecimalPoint)]])'): (java.time.format.DateTimeParseException) Text '2026-12-15 10:30:00' could not be parsed at index 10
|
||||||
|
LocalDate <- "" : null
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
expected instant : 2026-12-15T05:00:00Z
|
||||||
|
|
||||||
|
defaults, Instant <- seconds : 2026-12-15T05:00:00Z
|
||||||
|
defaults, Instant <- milliseconds : +58924-07-06T08:00:00Z
|
||||||
|
defaults, Instant <- 1797310800.5 : 2026-12-15T05:00:00.500Z
|
||||||
|
NANOSECONDS off, Instant <- milliseconds: 2026-12-15T05:00:00Z
|
||||||
|
NANOSECONDS off, Instant <- seconds : 1970-01-21T19:15:10.800Z
|
||||||
|
defaults, java.util.Date <- milliseconds: 2026-12-15T05:00:00Z
|
||||||
|
defaults, java.util.Date <- seconds : 1970-01-21T19:15:10.800Z
|
||||||
|
defaults, Instant <- "1797310800" : 2026-12-15T05:00:00Z
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
== defaultDateFormat(SimpleDateFormat)
|
||||||
|
{"day":"2026-12-15","at":"2026-12-15T10:30:00","legacy":"15/12/2026 05:00"}
|
||||||
|
read back: 2026-12-15T10:30
|
||||||
|
|
||||||
|
== withConfigOverride per type
|
||||||
|
{"day":"15/12/2026","at":"15/12/2026 10:30","legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
read back: 2026-12-15T10:30
|
||||||
|
|
||||||
|
== SimpleModule with a DateTimeFormatter
|
||||||
|
{"day":"2026-12-15","at":"15.12.2026 10.30","legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
read back: 2026-12-15T10:30
|
||||||
|
|
||||||
|
== an annotation beats the global override
|
||||||
|
{"day":"20261215","at":"15/12/2026 10:30","legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
== old Jackson 2 payload, read by Jackson 3 defaults
|
||||||
|
{"meeting":{"day":[2026,12,15],"wallClock":[2026,12,15,10,30],"createdAt":1797310800.000000000,"startsAt":1797310800.000000000,"reminderAt":1797310800.000000000},"length":5400.000000000,"month":"DECEMBER"}
|
||||||
|
OK Job[meeting=Meeting[day=2026-12-15, wallClock=2026-12-15T10:30, createdAt=2026-12-15T05:00:00Z, startsAt=2026-12-15T05:00Z, reminderAt=2026-12-15T05:00Z], length=PT1H30M, month=DECEMBER]
|
||||||
|
|
||||||
|
== new Jackson 3 payload, read by a Jackson 2 consumer (defaults + module)
|
||||||
|
{"meeting":{"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","reminderAt":"2026-12-15T10:30:00+05:30"},"length":"PT1H30M","month":12}
|
||||||
|
FAIL InvalidFormatException: Cannot deserialize value of type `java.time.Month` from number 12: index value outside legal index range [0..11]
|
||||||
|
|
||||||
|
== Jackson 3 told to keep writing the Jackson 2 shapes
|
||||||
|
{"meeting":{"day":[2026,12,15],"wallClock":[2026,12,15,10,30],"createdAt":1797310800.000000000,"startsAt":1797310800.000000000,"reminderAt":1797310800.000000000},"length":5400.000000000,"month":12}
|
||||||
|
identical to the Jackson 2 payload : false
|
||||||
|
FAIL InvalidFormatException: Cannot deserialize value of type `java.time.Month` from number 12: index value outside legal index range [0..11]
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
value sent : NOVEMBER
|
||||||
|
Jackson 2 writes : {"month":"NOVEMBER"}
|
||||||
|
Jackson 3 defaults write : {"month":11}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : DECEMBER
|
||||||
|
Jackson 2 + ONE_BASED_MONTHS reads : NOVEMBER
|
||||||
|
Jackson 3, ONE_BASED_MONTHS off : {"month":10}
|
||||||
|
Jackson 3 defaults read it : OCTOBER
|
||||||
|
Jackson 2 reads it : NOVEMBER
|
||||||
|
Jackson 3, Month override STRING : {"month":"NOVEMBER"}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : NOVEMBER
|
||||||
|
Jackson 3, MonthNameSerializer : {"month":"NOVEMBER"}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
######## Jackson 3.1.8
|
||||||
|
value sent : NOVEMBER
|
||||||
|
Jackson 2 writes : {"month":"NOVEMBER"}
|
||||||
|
Jackson 3 defaults write : {"month":11}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : DECEMBER
|
||||||
|
Jackson 2 + ONE_BASED_MONTHS reads : NOVEMBER
|
||||||
|
Jackson 3, ONE_BASED_MONTHS off : {"month":10}
|
||||||
|
Jackson 3 defaults read it : OCTOBER
|
||||||
|
Jackson 2 reads it : NOVEMBER
|
||||||
|
Jackson 3, Month override STRING : {"month":11}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : DECEMBER
|
||||||
|
Jackson 3, MonthNameSerializer : {"month":"NOVEMBER"}
|
||||||
|
|
||||||
|
######## Jackson 3.2.4
|
||||||
|
value sent : NOVEMBER
|
||||||
|
Jackson 2 writes : {"month":"NOVEMBER"}
|
||||||
|
Jackson 3 defaults write : {"month":11}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : DECEMBER
|
||||||
|
Jackson 2 + ONE_BASED_MONTHS reads : NOVEMBER
|
||||||
|
Jackson 3, ONE_BASED_MONTHS off : {"month":10}
|
||||||
|
Jackson 3 defaults read it : OCTOBER
|
||||||
|
Jackson 2 reads it : NOVEMBER
|
||||||
|
Jackson 3, Month override STRING : {"month":11}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : DECEMBER
|
||||||
|
Jackson 3, MonthNameSerializer : {"month":"NOVEMBER"}
|
||||||
|
|
||||||
|
######## Jackson 3.3.0
|
||||||
|
value sent : NOVEMBER
|
||||||
|
Jackson 2 writes : {"month":"NOVEMBER"}
|
||||||
|
Jackson 3 defaults write : {"month":11}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : DECEMBER
|
||||||
|
Jackson 2 + ONE_BASED_MONTHS reads : NOVEMBER
|
||||||
|
Jackson 3, ONE_BASED_MONTHS off : {"month":10}
|
||||||
|
Jackson 3 defaults read it : OCTOBER
|
||||||
|
Jackson 2 reads it : NOVEMBER
|
||||||
|
Jackson 3, Month override STRING : {"month":"NOVEMBER"}
|
||||||
|
Jackson 3 reads it : NOVEMBER
|
||||||
|
Jackson 2 reads it : NOVEMBER
|
||||||
|
Jackson 3, MonthNameSerializer : {"month":"NOVEMBER"}
|
||||||
|
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
Spring Boot 4.2.0, Jackson 3.1.8
|
||||||
|
|
||||||
|
==== no spring.jackson.* properties
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":11,"legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T05:00Z"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== write-dates-as-timestamps
|
||||||
|
spring.jackson.datatype.datetime.write-dates-as-timestamps=true
|
||||||
|
GET /event -> 200 {"day":[2026,12,15],"wallClock":[2026,12,15,10,30],"createdAt":1797310800.000000000,"startsAt":1797310800.000000000,"length":"PT1H30M","month":11,"legacy":1797310800000}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T05:00Z"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== date-format
|
||||||
|
spring.jackson.date-format=dd/MM/yyyy HH:mm
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":11,"legacy":"15/12/2026 05:00"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T05:00Z"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== time-zone
|
||||||
|
spring.jackson.time-zone=Asia/Kolkata
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":11,"legacy":"2026-12-15T10:30:00.000+05:30"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T10:30+05:30"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== keep the client's offset
|
||||||
|
spring.jackson.datatype.datetime.adjust-dates-to-context-time-zone=false
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":11,"legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T10:30+05:30"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== use-jackson2-defaults
|
||||||
|
spring.jackson.use-jackson2-defaults=true
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":10,"legacy":"2026-12-15T05:00:00.000+00:00"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T05:00Z"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== spring.mvc.format.date
|
||||||
|
spring.mvc.format.date=dd/MM/yyyy
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":11,"legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T05:00Z"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
|
||||||
|
==== profile indian-dates (JsonMapperBuilderCustomizer)
|
||||||
|
spring.profiles.active=indian-dates
|
||||||
|
GET /event -> 200 {"day":"15/12/2026","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":"NOVEMBER","legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T05:00Z"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== profile recommended (application-recommended.yml)
|
||||||
|
spring.profiles.active=recommended
|
||||||
|
GET /event -> 200 {"day":"2026-12-15","wallClock":"2026-12-15T10:30:00","createdAt":"2026-12-15T05:00:00Z","startsAt":"2026-12-15T10:30:00+05:30","length":"PT1H30M","month":11,"legacy":"2026-12-15T05:00:00.000Z"}
|
||||||
|
POST /booking -> 200 {"received":"2026-12-15T10:30+05:30"}
|
||||||
|
GET /search?day=2026-12-15 -> 200 {"parsed":"2026-12-15"}
|
||||||
|
GET /search?day=15/12/2026 -> 400 (Bad Request)
|
||||||
|
|
||||||
|
==== Boot 3 property name left in place
|
||||||
|
spring.jackson.serialization.write-dates-as-timestamps=false
|
||||||
|
startup FAILED: UnsatisfiedDependencyException
|
||||||
|
root cause : IllegalArgumentException: No enum constant tools.jackson.databind.SerializationFeature.write-dates-as-timestamps
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<!-- Standalone module: build it from inside this directory. Jackson 2 and Jackson 3 sit side by side
|
||||||
|
on one classpath (com.fasterxml.jackson vs tools.jackson) so their output can be compared line by line. -->
|
||||||
|
<groupId>com.ankurm</groupId>
|
||||||
|
<artifactId>java-time</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>java-time</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>25</maven.compiler.release>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<jackson2.version>2.23.0</jackson2.version>
|
||||||
|
<jackson3.version>3.3.0</jackson3.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson</groupId>
|
||||||
|
<artifactId>jackson-bom</artifactId>
|
||||||
|
<version>${jackson2.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>tools.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
|
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
Executable
+17
@@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Compiles each file in src/broken/ against Jackson 3 and records the compiler's own message.
|
||||||
|
# Output keeps each "error:" line with its source line and caret, and the error count (javac lists every candidate overload).
|
||||||
|
# These files are deliberately NOT part of the Maven build (they live outside src/main).
|
||||||
|
set -uo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
CP="target/classes:$(cat target/cp.txt)"
|
||||||
|
OUT=output/00-broken-compile-errors.txt
|
||||||
|
: > "$OUT"
|
||||||
|
for f in src/broken/*.java; do
|
||||||
|
echo "=== $(basename "$f") ===" >> "$OUT"
|
||||||
|
tmp=$(mktemp -d)
|
||||||
|
javac -Xlint:none -d "$tmp" -cp "$CP" "$f" 2>&1 | grep -vE "Picked up|^Note:" | sed "s#^src/broken/##" | awk '/error:/{c=3} c>0{print; c--} /^[0-9]+ errors?$/{print}' >> "$OUT"
|
||||||
|
rm -rf "$tmp"
|
||||||
|
echo >> "$OUT"
|
||||||
|
done
|
||||||
|
echo "wrote $OUT ($(wc -l < "$OUT") lines)"
|
||||||
Executable
+18
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Regenerates every file in output/. Run from the module directory: ./scripts/run-all.sh
|
||||||
|
# Needs JDK 25 and Maven. The Spring Boot examples are in boot/ and run from ./scripts/run-boot.sh
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
MVN="mvn -B ${MVN_ARGS:-}"
|
||||||
|
$MVN -q compile 2>&1 | grep -vE "Picked up|WARNING" || true
|
||||||
|
$MVN -q dependency:build-classpath -Dmdep.outputFile=target/cp.txt >/dev/null 2>&1
|
||||||
|
CP="target/classes:$(cat target/cp.txt)"
|
||||||
|
mkdir -p output
|
||||||
|
n=1
|
||||||
|
for src in src/main/java/com/ankurm/javatime/T[0-9][0-9]*.java; do
|
||||||
|
c=$(basename "$src" .java)
|
||||||
|
out="output/${c:1:2}-${c:3}.txt"
|
||||||
|
java -Duser.timezone=UTC -Duser.language=en -Duser.country=US -Dstdout.encoding=UTF-8 -cp "$CP" "com.ankurm.javatime.$c" 2>&1 | grep -vE "Picked up|^WARNING" > "$out"
|
||||||
|
echo "ran $c ($(wc -l < "$out") lines)"
|
||||||
|
done
|
||||||
|
./scripts/broken.sh
|
||||||
Executable
+10
@@ -0,0 +1,10 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Regenerates output/13-spring-boot.txt (in the module's output folder). Needs JDK 25 and Maven.
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/../boot"
|
||||||
|
MVN="mvn -B ${MVN_ARGS:-}"
|
||||||
|
$MVN -q clean compile 2>&1 | grep -vE "Picked up|WARNING" || true
|
||||||
|
$MVN -q dependency:build-classpath -Dmdep.outputFile=target/cp.txt >/dev/null 2>&1
|
||||||
|
java -Duser.timezone=UTC -cp "target/classes:$(cat target/cp.txt)" com.ankurm.javatime.boot.BootDemo 2>&1 \
|
||||||
|
| grep -vE "Picked up|^WARNING|^SLF4J" > ../output/13-spring-boot.txt
|
||||||
|
echo "wrote ../output/13-spring-boot.txt"
|
||||||
Executable
+16
@@ -0,0 +1,16 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Runs the Month program against several Jackson 3 releases (3.1.8 is what Spring Boot 4.2.0 manages).
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
MVN="mvn -B ${MVN_ARGS:-}"
|
||||||
|
out=output/12-month-across-versions.txt
|
||||||
|
: > "$out"
|
||||||
|
for v in 3.1.8 3.2.4 3.3.0; do
|
||||||
|
$MVN -q -Djackson3.version=$v clean compile 2>&1 | grep -vE "Picked up|WARNING" || true
|
||||||
|
$MVN -q -Djackson3.version=$v dependency:build-classpath -Dmdep.outputFile=target/cp.txt >/dev/null 2>&1
|
||||||
|
echo "######## Jackson $v" >> "$out"
|
||||||
|
java -Duser.timezone=UTC -cp "target/classes:$(cat target/cp.txt)" com.ankurm.javatime.T11MonthTrap 2>&1 \
|
||||||
|
| grep -vE "Picked up|^WARNING" >> "$out"
|
||||||
|
echo >> "$out"
|
||||||
|
done
|
||||||
|
echo "wrote $out"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import tools.jackson.databind.SerializationFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
public class OldFlagLocation {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JsonMapper.builder()
|
||||||
|
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
public class RegisterJackson2Module {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JsonMapper.builder()
|
||||||
|
.addModule(new JavaTimeModule())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
|
||||||
|
/** One value of each common java.time "moment" type, with no Jackson annotations. */
|
||||||
|
public record Meeting(LocalDate day,
|
||||||
|
LocalDateTime wallClock,
|
||||||
|
Instant createdAt,
|
||||||
|
OffsetDateTime startsAt,
|
||||||
|
ZonedDateTime reminderAt) { }
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
|
||||||
|
/** The same sample moment used by every program: 15 Dec 2026, 10:30 in India. */
|
||||||
|
public final class Samples {
|
||||||
|
private Samples() { }
|
||||||
|
|
||||||
|
public static final LocalDate DAY = LocalDate.of(2026, 12, 15);
|
||||||
|
public static final LocalDateTime WALL = LocalDateTime.of(2026, 12, 15, 10, 30);
|
||||||
|
public static final OffsetDateTime OFFSET = OffsetDateTime.of(WALL, ZoneOffset.ofHoursMinutes(5, 30));
|
||||||
|
public static final ZonedDateTime ZONED = ZonedDateTime.of(WALL, ZoneId.of("Asia/Kolkata"));
|
||||||
|
public static final Instant INSTANT = OFFSET.toInstant();
|
||||||
|
|
||||||
|
public static Meeting meeting() {
|
||||||
|
return new Meeting(DAY, WALL, INSTANT, OFFSET, ZONED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Jackson wraps the useful text in the first line of a long message. */
|
||||||
|
public static String firstLine(Throwable t) {
|
||||||
|
return t.getClass().getSimpleName() + ": " + t.getMessage().lines().findFirst().orElse("");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Jackson 2 needs a module (and a flag) for java.time. Jackson 3 needs neither. */
|
||||||
|
public class T01NoModuleNeeded {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Meeting meeting = Samples.meeting();
|
||||||
|
|
||||||
|
ObjectMapper plain2 = new ObjectMapper();
|
||||||
|
System.out.println("== Jackson " + plain2.version() + ", plain ObjectMapper");
|
||||||
|
try {
|
||||||
|
plain2.writeValueAsString(meeting);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(Samples.firstLine(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjectMapper module2 = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== Jackson 2 + JavaTimeModule");
|
||||||
|
System.out.println(module2.writeValueAsString(meeting));
|
||||||
|
|
||||||
|
JsonMapper m3 = JsonMapper.builder().build();
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== Jackson " + m3.version() + ", JsonMapper.builder().build()");
|
||||||
|
String json = m3.writeValueAsString(meeting);
|
||||||
|
System.out.println(json);
|
||||||
|
System.out.println("registered modules : " + m3.registeredModules().size());
|
||||||
|
System.out.println("read back : " + m3.readValue(json, Meeting.class));
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
for (String name : new String[] {
|
||||||
|
"com.fasterxml.jackson.datatype.jsr310.JavaTimeModule",
|
||||||
|
"tools.jackson.datatype.jsr310.JavaTimeModule",
|
||||||
|
"tools.jackson.databind.ext.javatime.JavaTimeInitializer",
|
||||||
|
"tools.jackson.databind.ext.javatime.ser.InstantSerializer"}) {
|
||||||
|
boolean found;
|
||||||
|
try {
|
||||||
|
Class.forName(name);
|
||||||
|
found = true;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
found = false;
|
||||||
|
}
|
||||||
|
System.out.println((found ? "present : " : "absent : ") + name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.DayOfWeek;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.time.MonthDay;
|
||||||
|
import java.time.Period;
|
||||||
|
import java.time.Year;
|
||||||
|
import java.time.YearMonth;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** The same values through four mappers, one block per type. */
|
||||||
|
public class T02OutputDiff {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ObjectMapper j2 = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||||
|
ObjectMapper j2iso = new ObjectMapper().registerModule(new JavaTimeModule())
|
||||||
|
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||||
|
JsonMapper j3 = JsonMapper.builder().build();
|
||||||
|
JsonMapper j3compat = JsonMapper.builderWithJackson2Defaults().build();
|
||||||
|
|
||||||
|
Map<String, Object> values = new LinkedHashMap<>();
|
||||||
|
values.put("LocalDate", Samples.DAY);
|
||||||
|
values.put("LocalTime", LocalTime.of(10, 30));
|
||||||
|
values.put("LocalDateTime", Samples.WALL);
|
||||||
|
values.put("LocalDateTime+nanos", Samples.WALL.withNano(123_456_000));
|
||||||
|
values.put("Instant", Samples.INSTANT);
|
||||||
|
values.put("OffsetDateTime", Samples.OFFSET);
|
||||||
|
values.put("ZonedDateTime", Samples.ZONED);
|
||||||
|
values.put("Duration", Duration.ofMinutes(90));
|
||||||
|
values.put("Period", Period.of(1, 2, 3));
|
||||||
|
values.put("Year", Year.of(2026));
|
||||||
|
values.put("YearMonth", YearMonth.of(2026, 12));
|
||||||
|
values.put("MonthDay", MonthDay.of(12, 15));
|
||||||
|
values.put("Month", Month.DECEMBER);
|
||||||
|
values.put("DayOfWeek", DayOfWeek.TUESDAY);
|
||||||
|
values.put("ZoneId", ZoneId.of("Asia/Kolkata"));
|
||||||
|
values.put("ZoneOffset", ZoneOffset.ofHoursMinutes(5, 30));
|
||||||
|
values.put("java.util.Date", Date.from(Samples.INSTANT));
|
||||||
|
values.put("java.sql.Date", java.sql.Date.valueOf(Samples.DAY));
|
||||||
|
values.put("Map<LocalDate,..> key", Map.of(Samples.DAY, 1));
|
||||||
|
values.put("Map<Date,..> key", Map.of(Date.from(Samples.INSTANT), 1));
|
||||||
|
|
||||||
|
System.out.println("A = Jackson 2.23.0 + JavaTimeModule");
|
||||||
|
System.out.println("B = A + disable(WRITE_DATES_AS_TIMESTAMPS)");
|
||||||
|
System.out.println("C = Jackson 3.3.0 defaults");
|
||||||
|
System.out.println("D = Jackson 3.3.0 builderWithJackson2Defaults()");
|
||||||
|
for (var entry : values.entrySet()) {
|
||||||
|
Object v = entry.getValue();
|
||||||
|
System.out.println();
|
||||||
|
System.out.println(entry.getKey());
|
||||||
|
System.out.println(" A " + write(() -> j2.writeValueAsString(v)));
|
||||||
|
System.out.println(" B " + write(() -> j2iso.writeValueAsString(v)));
|
||||||
|
System.out.println(" C " + write(() -> j3.writeValueAsString(v)));
|
||||||
|
System.out.println(" D " + write(() -> j3compat.writeValueAsString(v)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Call { String run() throws Exception; }
|
||||||
|
|
||||||
|
static String write(Call call) {
|
||||||
|
try {
|
||||||
|
return call.run();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return Samples.firstLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import tools.jackson.databind.cfg.DateTimeFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Prints every DateTimeFeature with its default, and what Jackson 2 compatibility mode flips. */
|
||||||
|
public class T03FeatureDefaults {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JsonMapper j3 = JsonMapper.builder().build();
|
||||||
|
JsonMapper compat = JsonMapper.builderWithJackson2Defaults().build();
|
||||||
|
System.out.printf("%-42s %-8s %s%n", "DateTimeFeature", "default", "with Jackson 2 defaults");
|
||||||
|
for (DateTimeFeature f : DateTimeFeature.values()) {
|
||||||
|
System.out.printf("%-42s %-8s %s%n", f.name(),
|
||||||
|
j3.serializationConfig().isEnabled(f),
|
||||||
|
compat.serializationConfig().isEnabled(f));
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("default time zone (mapper): " + j3.serializationConfig().getTimeZone().getID());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import tools.jackson.databind.cfg.DateTimeFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** What happens to "+05:30" when the text is read back into Java. */
|
||||||
|
public class T04ReadingOffsets {
|
||||||
|
static final String TEXT = "\"2026-12-15T10:30:00+05:30\"";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ObjectMapper j2 = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||||
|
System.out.println("input: " + TEXT);
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== Jackson 2.23.0 + JavaTimeModule, defaults");
|
||||||
|
System.out.println("OffsetDateTime : " + j2.readValue(TEXT, OffsetDateTime.class));
|
||||||
|
System.out.println("ZonedDateTime : " + j2.readValue(TEXT, ZonedDateTime.class));
|
||||||
|
System.out.println("Instant : " + j2.readValue(TEXT, Instant.class));
|
||||||
|
|
||||||
|
show("Jackson 3 defaults (mapper zone UTC)", JsonMapper.builder().build());
|
||||||
|
show("Jackson 3, defaultTimeZone(Asia/Kolkata)", JsonMapper.builder()
|
||||||
|
.defaultTimeZone(TimeZone.getTimeZone("Asia/Kolkata")).build());
|
||||||
|
JsonMapper keep = JsonMapper.builder()
|
||||||
|
.disable(DateTimeFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).build();
|
||||||
|
show("Jackson 3, ADJUST_DATES_TO_CONTEXT_TIME_ZONE disabled", keep);
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== A region id survives only if you ask for it");
|
||||||
|
JsonMapper withId = JsonMapper.builder()
|
||||||
|
.enable(DateTimeFeature.WRITE_DATES_WITH_ZONE_ID)
|
||||||
|
.disable(DateTimeFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
|
||||||
|
.build();
|
||||||
|
String plain = keep.writeValueAsString(Samples.ZONED);
|
||||||
|
String tagged = withId.writeValueAsString(Samples.ZONED);
|
||||||
|
System.out.println("original : " + Samples.ZONED);
|
||||||
|
System.out.println("written by default : " + plain);
|
||||||
|
System.out.println(" read back : " + keep.readValue(plain, ZonedDateTime.class));
|
||||||
|
System.out.println("WRITE_DATES_WITH_ZONE_ID : " + tagged);
|
||||||
|
System.out.println(" read back : " + withId.readValue(tagged, ZonedDateTime.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void show(String title, JsonMapper mapper) {
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== " + title);
|
||||||
|
System.out.println("OffsetDateTime : " + mapper.readValue(TEXT, OffsetDateTime.class));
|
||||||
|
System.out.println("ZonedDateTime : " + mapper.readValue(TEXT, ZonedDateTime.class));
|
||||||
|
System.out.println("Instant : " + mapper.readValue(TEXT, Instant.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
import tools.jackson.databind.cfg.DateTimeFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Which settings move the output: the JVM zone, the mapper zone, or neither. */
|
||||||
|
public class T05TimeZones {
|
||||||
|
record Values(Object localDateTime, Object instant, Object offset, Object zoned,
|
||||||
|
Date utilDate, java.sql.Date sqlDate) { }
|
||||||
|
|
||||||
|
record Patterned(
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm XXX") OffsetDateTime offset,
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm XXX") ZonedDateTime zoned) { }
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
for (String jvmZone : new String[] {"UTC", "Asia/Kolkata", "America/New_York"}) {
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone(jvmZone));
|
||||||
|
JsonMapper mapper = JsonMapper.builder().build();
|
||||||
|
print("JVM zone " + jvmZone + ", mapper defaults", mapper);
|
||||||
|
}
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
|
|
||||||
|
TimeZone india = TimeZone.getTimeZone("Asia/Kolkata");
|
||||||
|
print("JVM zone UTC, defaultTimeZone(Asia/Kolkata)",
|
||||||
|
JsonMapper.builder().defaultTimeZone(india).build());
|
||||||
|
print("defaultTimeZone(Asia/Kolkata), WRITE_DATES_WITH_CONTEXT_TIME_ZONE disabled",
|
||||||
|
JsonMapper.builder().defaultTimeZone(india)
|
||||||
|
.disable(DateTimeFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE).build());
|
||||||
|
|
||||||
|
TimeZone london = TimeZone.getTimeZone("Europe/London");
|
||||||
|
Patterned p = new Patterned(Samples.OFFSET, Samples.ZONED);
|
||||||
|
System.out.println("== @JsonFormat(pattern) on offset types, values are at +05:30");
|
||||||
|
System.out.println("mapper zone not set : "
|
||||||
|
+ JsonMapper.builder().build().writeValueAsString(p));
|
||||||
|
System.out.println("defaultTimeZone(Europe/London) : "
|
||||||
|
+ JsonMapper.builder().defaultTimeZone(london).build().writeValueAsString(p));
|
||||||
|
System.out.println("London, CONTEXT_TIME_ZONE disabled: "
|
||||||
|
+ JsonMapper.builder().defaultTimeZone(london)
|
||||||
|
.disable(DateTimeFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE).build()
|
||||||
|
.writeValueAsString(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print(String title, JsonMapper mapper) throws Exception {
|
||||||
|
// sql.Date is rebuilt each time: valueOf() reads the JVM zone at construction.
|
||||||
|
Values v = new Values(Samples.WALL, Samples.INSTANT, Samples.OFFSET, Samples.ZONED,
|
||||||
|
Date.from(Samples.INSTANT), java.sql.Date.valueOf(Samples.DAY));
|
||||||
|
System.out.println("== " + title);
|
||||||
|
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(v));
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Per-field formats with the (unchanged) com.fasterxml.jackson.annotation.JsonFormat. */
|
||||||
|
public class T06JsonFormat {
|
||||||
|
public record Invoice(
|
||||||
|
@JsonFormat(pattern = "dd/MM/yyyy") LocalDate issued,
|
||||||
|
@JsonFormat(pattern = "dd-MM-yyyy HH:mm") LocalDateTime dueAt,
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Kolkata") Instant paidAt,
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mmXXX",
|
||||||
|
without = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) OffsetDateTime signedAt,
|
||||||
|
@JsonFormat(pattern = "d MMMM yyyy", locale = "fr_FR") LocalDate printed,
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.NUMBER) Instant epochSeconds,
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.NUMBER,
|
||||||
|
without = JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) Instant writeOnlyMillis,
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.NUMBER, without = {
|
||||||
|
JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,
|
||||||
|
JsonFormat.Feature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS}) Instant epochMillis,
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.ARRAY) LocalDate asArray,
|
||||||
|
Month monthDefault,
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING) Month monthName) { }
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
JsonMapper mapper = JsonMapper.builder().build();
|
||||||
|
Invoice invoice = new Invoice(Samples.DAY, Samples.WALL, Samples.INSTANT, Samples.OFFSET,
|
||||||
|
Samples.DAY, Samples.INSTANT, Samples.INSTANT, Samples.INSTANT, Samples.DAY, Month.DECEMBER, Month.DECEMBER);
|
||||||
|
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(invoice);
|
||||||
|
System.out.println(json);
|
||||||
|
System.out.println();
|
||||||
|
Invoice back = mapper.readValue(json, Invoice.class);
|
||||||
|
System.out.println("issued : " + back.issued());
|
||||||
|
System.out.println("dueAt : " + back.dueAt());
|
||||||
|
System.out.println("paidAt : " + back.paidAt());
|
||||||
|
System.out.println("signedAt : " + back.signedAt());
|
||||||
|
System.out.println("printed : " + back.printed());
|
||||||
|
System.out.println("epochSeconds : " + back.epochSeconds());
|
||||||
|
System.out.println("writeOnlyMillis : " + back.writeOnlyMillis());
|
||||||
|
System.out.println("epochMillis : " + back.epochMillis());
|
||||||
|
System.out.println("asArray : " + back.asArray());
|
||||||
|
System.out.println("monthDefault : " + back.monthDefault());
|
||||||
|
System.out.println("monthName : " + back.monthName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.annotation.OptBoolean;
|
||||||
|
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Patterns that compile, run, and give the wrong answer or a confusing error. */
|
||||||
|
public class T07FormatTraps {
|
||||||
|
record Default(@JsonFormat(pattern = "yyyy-MM-dd") LocalDate day) { }
|
||||||
|
record StrictY(@JsonFormat(pattern = "yyyy-MM-dd", lenient = OptBoolean.FALSE) LocalDate day) { }
|
||||||
|
record StrictU(@JsonFormat(pattern = "uuuu-MM-dd", lenient = OptBoolean.FALSE) LocalDate day) { }
|
||||||
|
record WeekYear(@JsonFormat(pattern = "YYYY-MM-dd") LocalDate day) { }
|
||||||
|
record CalendarYear(@JsonFormat(pattern = "yyyy-MM-dd") LocalDate day) { }
|
||||||
|
record FakeZ(@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'") LocalDateTime at) { }
|
||||||
|
record NoZone(@JsonFormat(pattern = "yyyy-MM-dd HH:mm") Instant at) { }
|
||||||
|
record ZonedNoZone(@JsonFormat(pattern = "yyyy-MM-dd HH:mm") ZonedDateTime at) { }
|
||||||
|
|
||||||
|
static final JsonMapper MAPPER = JsonMapper.builder().build();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("== 30 February");
|
||||||
|
read("no pattern ", "\"2026-02-30\"", LocalDate.class);
|
||||||
|
read("pattern ", "{\"day\":\"2026-02-30\"}", Default.class);
|
||||||
|
read("lenient=FALSE, yyyy ", "{\"day\":\"2026-02-30\"}", StrictY.class);
|
||||||
|
read("lenient=FALSE, yyyy ok", "{\"day\":\"2026-02-28\"}", StrictY.class);
|
||||||
|
read("lenient=FALSE, uuuu ", "{\"day\":\"2026-02-30\"}", StrictU.class);
|
||||||
|
read("lenient=FALSE, uuuu ok", "{\"day\":\"2026-02-28\"}", StrictU.class);
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== YYYY is the week-based year, yyyy is the calendar year");
|
||||||
|
LocalDate d = LocalDate.of(2026, 12, 29);
|
||||||
|
System.out.println("value 2026-12-29, YYYY : " + MAPPER.writeValueAsString(new WeekYear(d)));
|
||||||
|
System.out.println("value 2026-12-29, yyyy : " + MAPPER.writeValueAsString(new CalendarYear(d)));
|
||||||
|
read("YYYY read ", "{\"day\":\"2026-12-29\"}", WeekYear.class);
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== A quoted 'Z' is just a letter");
|
||||||
|
System.out.println("10:30 India wall clock : " + MAPPER.writeValueAsString(new FakeZ(Samples.WALL)));
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== A pattern on a type that has no calendar fields");
|
||||||
|
write("Instant, no timezone ", new NoZone(Samples.INSTANT));
|
||||||
|
write("ZonedDateTime ", new ZonedNoZone(Samples.ZONED));
|
||||||
|
read("Instant, no timezone ", "{\"at\":\"2026-12-15 05:00\"}", NoZone.class);
|
||||||
|
read("ZonedDateTime ", "{\"at\":\"2026-12-15 10:30\"}", ZonedNoZone.class);
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== Strings that carry more, or less, than the target type");
|
||||||
|
read("LocalDateTime <- ...Z ", "\"2026-12-15T05:00:00Z\"", LocalDateTime.class);
|
||||||
|
read("LocalDateTime <- +05:30", "\"2026-12-15T10:30:00+05:30\"", LocalDateTime.class);
|
||||||
|
read("LocalDate <- date-time", "\"2026-12-15T10:30:00\"", LocalDate.class);
|
||||||
|
read("LocalDate <- ...Z ", "\"2026-12-15T22:30:00Z\"", LocalDate.class);
|
||||||
|
read("Instant <- no offset ", "\"2026-12-15T10:30:00\"", Instant.class);
|
||||||
|
read("LocalDateTime <- space", "\"2026-12-15 10:30:00\"", LocalDateTime.class);
|
||||||
|
read("LocalDate <- \"\" ", "\"\"", LocalDate.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read(String label, String json, Class<?> type) {
|
||||||
|
try {
|
||||||
|
System.out.println(label + " : " + MAPPER.readValue(json, type));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(label + " : " + Samples.firstLine(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write(String label, Object value) {
|
||||||
|
try {
|
||||||
|
System.out.println(label + " : " + MAPPER.writeValueAsString(value));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(label + " : " + Samples.firstLine(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import tools.jackson.databind.cfg.DateTimeFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** A bare number: seconds or milliseconds? It depends on the target type and one feature. */
|
||||||
|
public class T08EpochNumbers {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JsonMapper defaults = JsonMapper.builder().build();
|
||||||
|
JsonMapper millis = JsonMapper.builder()
|
||||||
|
.disable(DateTimeFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS).build();
|
||||||
|
String seconds = "1797310800";
|
||||||
|
String ms = "1797310800000";
|
||||||
|
|
||||||
|
System.out.println("expected instant : " + Samples.INSTANT);
|
||||||
|
System.out.println();
|
||||||
|
read("defaults, Instant <- seconds ", defaults, seconds, Instant.class);
|
||||||
|
read("defaults, Instant <- milliseconds ", defaults, ms, Instant.class);
|
||||||
|
read("defaults, Instant <- 1797310800.5 ", defaults, "1797310800.5", Instant.class);
|
||||||
|
read("NANOSECONDS off, Instant <- milliseconds", millis, ms, Instant.class);
|
||||||
|
read("NANOSECONDS off, Instant <- seconds ", millis, seconds, Instant.class);
|
||||||
|
read("defaults, java.util.Date <- milliseconds", defaults, ms, Date.class);
|
||||||
|
read("defaults, java.util.Date <- seconds ", defaults, seconds, Date.class);
|
||||||
|
read("defaults, Instant <- \"1797310800\" ", defaults, "\"1797310800\"", Instant.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read(String label, JsonMapper mapper, String json, Class<?> type) {
|
||||||
|
try {
|
||||||
|
Object value = mapper.readValue(json, type);
|
||||||
|
if (value instanceof Date date) {
|
||||||
|
value = date.toInstant();
|
||||||
|
}
|
||||||
|
System.out.println(label + ": " + value);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(label + ": " + Samples.firstLine(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
import tools.jackson.databind.ext.javatime.deser.LocalDateTimeDeserializer;
|
||||||
|
import tools.jackson.databind.ext.javatime.ser.LocalDateTimeSerializer;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
import tools.jackson.databind.module.SimpleModule;
|
||||||
|
|
||||||
|
/** Three ways to set a format once for the whole mapper, and what each one reaches. */
|
||||||
|
public class T09GlobalFormats {
|
||||||
|
record Row(LocalDate day, LocalDateTime at, Date legacy) { }
|
||||||
|
record Overridden(@JsonFormat(pattern = "yyyyMMdd") LocalDate day, LocalDateTime at, Date legacy) { }
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Row row = new Row(Samples.DAY, Samples.WALL, Date.from(Samples.INSTANT));
|
||||||
|
|
||||||
|
JsonMapper dateFormat = JsonMapper.builder()
|
||||||
|
.defaultDateFormat(new SimpleDateFormat("dd/MM/yyyy HH:mm"))
|
||||||
|
.build();
|
||||||
|
show("defaultDateFormat(SimpleDateFormat)", dateFormat, row);
|
||||||
|
|
||||||
|
JsonMapper perType = JsonMapper.builder()
|
||||||
|
.withConfigOverride(LocalDate.class,
|
||||||
|
o -> o.setFormat(JsonFormat.Value.forPattern("dd/MM/yyyy")))
|
||||||
|
.withConfigOverride(LocalDateTime.class,
|
||||||
|
o -> o.setFormat(JsonFormat.Value.forPattern("dd/MM/yyyy HH:mm")))
|
||||||
|
.build();
|
||||||
|
show("withConfigOverride per type", perType, row);
|
||||||
|
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH.mm");
|
||||||
|
SimpleModule module = new SimpleModule()
|
||||||
|
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter))
|
||||||
|
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
|
||||||
|
show("SimpleModule with a DateTimeFormatter",
|
||||||
|
JsonMapper.builder().addModule(module).build(), row);
|
||||||
|
|
||||||
|
System.out.println("== an annotation beats the global override");
|
||||||
|
System.out.println(perType.writeValueAsString(
|
||||||
|
new Overridden(Samples.DAY, Samples.WALL, Date.from(Samples.INSTANT))));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void show(String title, JsonMapper mapper, Row row) throws Exception {
|
||||||
|
System.out.println("== " + title);
|
||||||
|
String json = mapper.writeValueAsString(row);
|
||||||
|
System.out.println(json);
|
||||||
|
System.out.println("read back: " + mapper.readValue(json, Row.class).at());
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Month;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import tools.jackson.databind.cfg.DateTimeFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
/** Migration both ways: can each side read what the other wrote? */
|
||||||
|
public class T10StoredPayloads {
|
||||||
|
record Job(Meeting meeting, Duration length, Month month) { }
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ObjectMapper j2 = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||||
|
JsonMapper j3 = JsonMapper.builder().build();
|
||||||
|
Job job = new Job(Samples.meeting(), Duration.ofMinutes(90), Month.DECEMBER);
|
||||||
|
|
||||||
|
String old = j2.writeValueAsString(job);
|
||||||
|
String fresh = j3.writeValueAsString(job);
|
||||||
|
|
||||||
|
System.out.println("== old Jackson 2 payload, read by Jackson 3 defaults");
|
||||||
|
System.out.println(old);
|
||||||
|
attempt(() -> j3.readValue(old, Job.class).toString());
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== new Jackson 3 payload, read by a Jackson 2 consumer (defaults + module)");
|
||||||
|
System.out.println(fresh);
|
||||||
|
attempt(() -> j2.readValue(fresh, Job.class).toString());
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("== Jackson 3 told to keep writing the Jackson 2 shapes");
|
||||||
|
JsonMapper keepOld = JsonMapper.builder()
|
||||||
|
.enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS,
|
||||||
|
DateTimeFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
|
||||||
|
.build();
|
||||||
|
String kept = keepOld.writeValueAsString(job);
|
||||||
|
System.out.println(kept);
|
||||||
|
System.out.println("identical to the Jackson 2 payload : " + kept.equals(old));
|
||||||
|
attempt(() -> j2.readValue(kept, Job.class).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Call { String run() throws Exception; }
|
||||||
|
|
||||||
|
static void attempt(Call call) {
|
||||||
|
try {
|
||||||
|
System.out.println("OK " + call.run());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("FAIL " + Samples.firstLine(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.ankurm.javatime;
|
||||||
|
|
||||||
|
import java.time.Month;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import tools.jackson.core.JsonGenerator;
|
||||||
|
import tools.jackson.databind.SerializationContext;
|
||||||
|
import tools.jackson.databind.cfg.DateTimeFeature;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
import tools.jackson.databind.module.SimpleModule;
|
||||||
|
import tools.jackson.databind.ser.std.StdSerializer;
|
||||||
|
|
||||||
|
/** java.time.Month changed from a name to a number, and the number can be off by one. */
|
||||||
|
public class T11MonthTrap {
|
||||||
|
record Report(Month month) { }
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ObjectMapper j2 = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||||
|
JsonMapper j3 = JsonMapper.builder().build();
|
||||||
|
JsonMapper zeroBased = JsonMapper.builder().disable(DateTimeFeature.ONE_BASED_MONTHS).build();
|
||||||
|
JsonMapper names = JsonMapper.builder()
|
||||||
|
.withConfigOverride(Month.class,
|
||||||
|
o -> o.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
|
||||||
|
.build();
|
||||||
|
Report november = new Report(Month.NOVEMBER);
|
||||||
|
|
||||||
|
System.out.println("value sent : " + november.month());
|
||||||
|
System.out.println("Jackson 2 writes : " + j2.writeValueAsString(november));
|
||||||
|
String fromJ3 = j3.writeValueAsString(november);
|
||||||
|
System.out.println("Jackson 3 defaults write : " + fromJ3);
|
||||||
|
System.out.println(" Jackson 3 reads it : " + j3.readValue(fromJ3, Report.class).month());
|
||||||
|
System.out.println(" Jackson 2 reads it : " + j2.readValue(fromJ3, Report.class).month());
|
||||||
|
String compat = zeroBased.writeValueAsString(november);
|
||||||
|
ObjectMapper j2oneBased = com.fasterxml.jackson.databind.json.JsonMapper.builder()
|
||||||
|
.addModule(new JavaTimeModule()
|
||||||
|
.enable(com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature.ONE_BASED_MONTHS))
|
||||||
|
.build();
|
||||||
|
System.out.println(" Jackson 2 + ONE_BASED_MONTHS reads : " + j2oneBased.readValue(fromJ3, Report.class).month());
|
||||||
|
System.out.println("Jackson 3, ONE_BASED_MONTHS off : " + compat);
|
||||||
|
System.out.println(" Jackson 3 defaults read it : " + j3.readValue(compat, Report.class).month());
|
||||||
|
System.out.println(" Jackson 2 reads it : " + j2.readValue(compat, Report.class).month());
|
||||||
|
String named = names.writeValueAsString(november);
|
||||||
|
System.out.println("Jackson 3, Month override STRING : " + named);
|
||||||
|
System.out.println(" Jackson 3 reads it : " + names.readValue(named, Report.class).month());
|
||||||
|
System.out.println(" Jackson 2 reads it : " + j2.readValue(named, Report.class).month());
|
||||||
|
|
||||||
|
SimpleModule module = new SimpleModule().addSerializer(Month.class, new MonthNameSerializer());
|
||||||
|
System.out.println("Jackson 3, MonthNameSerializer : "
|
||||||
|
+ JsonMapper.builder().addModule(module).build().writeValueAsString(november));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Works on every 3.x line tried here, including the one Spring Boot 4.2.0 manages. */
|
||||||
|
static final class MonthNameSerializer extends StdSerializer<Month> {
|
||||||
|
MonthNameSerializer() {
|
||||||
|
super(Month.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(Month value, JsonGenerator gen, SerializationContext context) {
|
||||||
|
gen.writeString(value.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user