Skip to content

Problem in serializing negative Duration values #165

Closed
@jpsyri

Description

@jpsyri

When SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS is true, Durations with negative value get serialized wrong. For example Duration.ofMillis(-1) gets serialized as "-1.999000000"

Problem seems to be in DurationSerializer where following code is used for serializing

                generator.writeNumber(DecimalUtils.toBigDecimal(
                        duration.getSeconds(), duration.getNano()
                ));

However Duration internally stores duration in format where seconds part basically contains floor(duration) and nanoseconds part contains offset from that value to the actual duration. So n case of duration of -1 millisecond, the seconds part value will be -1 and nanoseconds part will be 999000000. getSeconds() and getNano() return values according to that internal representation, which leads to the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions