Skip to content

Commit

Permalink
Porting of JDK 9 fix of java.time.Duration serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk committed Feb 6, 2018
1 parent 9f0a2d3 commit 99ec9a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -789,27 +789,27 @@ final class JsonWriter private[jsoniter_scala](
if (x.isZero) writeBytes('"', 'P', 'T', '0', 'S', '"')
else {
writeBytes('"', 'P', 'T')
val totalSeconds = x.getSeconds
val hours = totalSeconds / 3600
val totalSecs = x.getSeconds
var effectiveTotalSecs = totalSecs
var nanos = x.getNano
if (effectiveTotalSecs < 0 && nanos > 0) effectiveTotalSecs += 1
val hours = effectiveTotalSecs / 3600
if (hours != 0) {
writeLong(hours)
writeBytes('H')
}
val secsOfHour = (totalSeconds - hours * 3600).toInt
val secsOfHour = (effectiveTotalSecs - hours * 3600).toInt
val minutes = secsOfHour / 60
if (minutes != 0) {
writeInt(minutes)
writeBytes('M')
}
val seconds = secsOfHour - minutes * 60
var nanos = x.getNano
if (seconds != 0 || nanos != 0) {
if (seconds < 0 && nanos > 0) {
nanos = 1000000000 - nanos
if (seconds == -1) writeBytes('-', '0')
else writeInt(seconds + 1)
} else writeInt(seconds)
if (totalSecs < 0 && seconds == 0 && nanos > 0) writeBytes('-', '0')
else writeInt(seconds)
if (nanos > 0) {
if (totalSecs < 0) nanos = 1000000000 - nanos
writeBytes('.')
val posLim = ensureBufferCapacity(9)
var pos = posLim + 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.scalacheck.Gen
import scala.collection.JavaConverters._

object GenUtils {
val isJDK8: Boolean = System.getProperty("java.version").startsWith("1.8")
val genHighSurrogateChar: Gen[Char] = Gen.choose('\ud800', '\udbff')
val genLowSurrogateChar: Gen[Char] = Gen.choose('\udc00', '\udfff')
val genSurrogateChar: Gen[Char] = Gen.oneOf(genHighSurrogateChar, genLowSurrogateChar)
Expand All @@ -19,9 +20,11 @@ object GenUtils {
Gen.choose(Long.MinValue / 86400, Long.MaxValue / 86400).map(Duration.ofDays),
Gen.choose(Long.MinValue / 3600, Long.MaxValue / 3600).map(Duration.ofHours),
Gen.choose(Long.MinValue / 60, Long.MaxValue / 60).map(Duration.ofMinutes),
// FIXME JDK 8/9 have bug in parsing of Duration with zero seconds and negative nanos
Gen.choose(Long.MinValue, Long.MaxValue).map(Duration.ofSeconds),
Gen.choose(Long.MinValue, Int.MaxValue.toLong).map(Duration.ofMillis),
Gen.choose(Long.MinValue, Int.MaxValue.toLong).map(Duration.ofNanos))
// FIXME JDK 8 has bug in serialization of Duration with negative nanos
Gen.choose(if (isJDK8) 0L else Int.MinValue, Int.MaxValue.toLong).map(Duration.ofMillis),
Gen.choose(if (isJDK8) 0L else Int.MinValue, Int.MaxValue.toLong).map(Duration.ofNanos))
val genInstant: Gen[Instant] =
for {
year <- Gen.choose(-1000000000, 1000000000)
Expand Down

0 comments on commit 99ec9a5

Please sign in to comment.