jakartaee / jakartaee/jsonb-api

Feature Request: Introduce global fractional seconds precision setting in `JsonbConfig`

Open
#398 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
95
Forks
41
Avg merge
1d 6h
Merged PRs (30d)
35

Description

#### Description
In multi-tier systems using **Jakarta Persistence (JPA)** and **Jakarta JSON Binding (JSON-B)**, it is common to sync database timestamp truncation rules with API payloads. For example, database fields restricted to millisecond precision via `@Column(secondPrecision = 3)` should be serialized to JSON with matching precision.

Currently, JSON-B permits formatting via `withDateFormat()`. However, configuring date formats natively using ISO-8601 strings across mixed temporal fields (like `Instant`, `LocalDateTime`, and `ZonedDateTime`) often forces tight format strings that break on specific sub-types (e.g., Issue #87).

There is no native config parameter to cleanly enforce a global **fractional second precision ceiling** (e.g., rounding or truncating down to milliseconds instead of defaulting to nanoseconds) across all serialized JSON date numbers or timestamp strings.

#### Proposed Solution
Extend `JsonbConfig` to provide a dedicated property to configure fractional seconds precision globally, acting similarly to JPA's `secondPrecision` element.

```java
// Suggested Configuration Methods in JsonbConfig
public JsonbConfig withSecondPrecision(int precision);

// Usage Example
JsonbConfig config = new JsonbConfig()
.withSecondPrecision(3); // Limits all serialized timestamps (Instants, DateTimes) to Millisecond precision (.SSS)

Jsonb jsonb = JsonbBuilder.create(config);
```

#### Expected Serialization Behavior
When `withSecondPrecision(3)` is applied globally:
* A `java.time.Instant` representing `2026-07-13T19:05:00.123456789Z` should serialize directly as `"2026-07-13T19:05:00.123Z"`.
* String formatting strategies should naturally fall back to the maximum declared precision digit count without throwing runtime parse exceptions.

#### Additional Context
This feature brings configuration parity to enterprise stacks where serialization mechanisms in the persistence layer (JPA) and representation layer (JSON-B) must strictly match down to the exact sub-second precision level.

Contributor guide

Open the contributing guide

Research direction

Start by reading JsonbConfig and the existing withDateFormat configuration path, then trace serialization for Instant, LocalDateTime, and ZonedDateTime. Confirm the intended behavior for both timestamp strings and date numbers, including truncation or rounding and the fallback for mixed temporal types. Done means the global precision contract is defined and consistently applied without format parse failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.