FasterXML / FasterXML/jackson-modules-java8
OffsetTimeSerializer not writing seconds for time object like 15:30:00Z (where seconds is 00)
- 主要語言
- Java
- 星號
- 425
- 分支
- 124
- PR 合併指標
- 30 天內沒有已合併 PR
描述
When having a `OffsetTime` field with e.g. `15:00:00Z` and serializing this with Jackson it outputs:
`15:00Z` and thus the seconds are lost.
Note I have the time module registered via `JavaTimeModule()`.
Although one could argue that the seconds are not needed this gives some problems when validating outputted data against JSON schema.
when using the JSON schema format `time` then the seconds are expected, see https://json-schema.org/understanding-json-schema/reference/string#dates-and-times
without the seconds the JSON schema validator fails with: The value must be a valid time.
(note I'm using [Justify](https://github.com/leadpony/justify) for schema validation)
Looking at the source code, I see this is done on purpose
https://github.com/FasterXML/jackson-modules-java8/blob/master/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java#L92-L111
```
private final void _serializeAsArrayContents(OffsetTime value, JsonGenerator g,
SerializerProvider provider)
throws JacksonException
{
g.writeNumber(value.getHour());
g.writeNumber(value.getMinute());
final int secs = value.getSecond();
final int nanos = value.getNano();
if ((secs > 0) || (nanos > 0)) {
g.writeNumber(secs);
if (nanos > 0) {
if(useNanoseconds(provider)) {
g.writeNumber(nanos);
} else {
g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND));
}
}
}
g.writeString(value.getOffset().toString());
}
```
Is it possible to override this behaviour?
Currently I think my only option is create a custom Serializer?
If not possible to override this behaviour would it be at least possible to change this `_serializeAsArrayContents` method from `private` to `protected` so one could simply extend this class and override this method?
貢獻指南
這個儲存庫沒有索引到貢獻指南
研究方向
從 datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java 開始,特別查看 _serializeAsArrayContents,並使用 JavaTimeModule 重現 15:00:00Z 的序列化。確認預期的 JSON 時間表示形式,並判斷應採用所要求的行為,還是可覆寫的 serializer hook;當該行為由回歸測試涵蓋時,即視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- java
- 領域
- api, backend
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100