FasterXML / FasterXML/jackson-modules-java8

Allow Instant to be serialized as epochSecond without the fraction part

未關閉
#116 20 則留言 4 個 reaction 已指派 0 人 在 GitHub 檢視
date-time-config new-feature
主要語言
Java
星號
425
分支
124
PR 合併指標
30 天內沒有已合併 PR

描述

I have an `Instant` field that represents an epoch timestamp in seconds.

```
@JsonProperty("registered_at")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant registeredAt;
```

the mapper is configured this way

```
.featuresToDisable(ADJUST_DATES_TO_CONTEXT_TIME_ZONE,
FAIL_ON_UNKNOWN_PROPERTIES,
WRITE_DATES_AS_TIMESTAMPS,
SORT_PROPERTIES_ALPHABETICALLY)
```

However when this gets serialized as

```
"registered_at" : 1420324047.000000000
```

By looking at the code there's no way to serialize this value as epoch seconds using the standard mechanism.

`InstantSerializerBase.serialize(T value, JsonGenerator generator, SerializerProvider provider)`

```
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
generator.writeNumber(DecimalUtils.toBigDecimal(
getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value)
));
return;
}
generator.writeNumber(getEpochMillis.applyAsLong(value));
return;
}
```

The only option is to use :

```
@JsonGetter("registered_at")
public long getRegisteredAtEpoch() {
return registeredAt.getEpochSecond();
}
```

It could be nice if there could be way to tell jackson to avoid serializing the fraction part.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。