circe-yaml
circe-yaml copied to clipboard
Use integer number format when using Int tag
This PR fixes an issue where numbers which have integral values are output in fractional format, but with the !!int
tag (originaly reported in #68)
Code to reproduce the issue:
import io.circe._
import io.circe.syntax._
import io.circe.generic.auto._
object Main extends App {
case class Data(x: Double, y: Double)
val foo = Seq(
Data(0.0000001, 100000000),
Data(0.1234567891234569, 1000)
)
val json = foo.asJson
val printer = yaml.Printer.spaces2
val yamlString = printer.pretty(json)
println(yamlString)
}
The output before the fix is:
- x: 1.0E-7
y: !!int '1.0E8'
- x: 0.1234567891234569
y: !!int '1.0'
After the fix the output is much more natural:
- x: 1.0E-7
y: 100000000
- x: 0.1234567891234569
y: 1
The test SnakeYamlSymmetricSerializationTests
is now failing on handling various forms of zero value. I have seen failures like:
-
input:
-0e1387
-
output:
0
-
input:
-0
-
output:
0
I have to admit I am not sure how to proceed with this. Should the behavior be fixed, or is the test too strict and it is the test which should be fixed?
@OndrejSpanel I don't have a strong opinion here, and I'm not sure whether @jeffmay is still actively maintaining this project, so I'd say make a proposal and we can discuss it.
@OndrejSpanel I'm not sure whether we should care about use cases that can't be represented by the equivalent base types on the JVM. That being said, I'm ok with either making this change or just stripping the !!int
from the string instead of converting it to a Long
first.
I am afraid I do not understand the "can't be represented by the equivalent base types on the JVM" part - can you elaborate? (In my code - what can't be represented by what type?)
Actually, the failing test example input of -0
can be represented by a float or double. I was thinking that we couldn't represent -0e1387
on the JVM (since we can't store it to a Long
), but we can store it as a float or double. It may simplify to -0.0
but that should be fine.
Maybe there is a way to detect that the value is -0
equivalent before attempting to treat it as an integer?
Closing because of build conflicts and age. Feel free to re-open or open a new PR without conflicts if you are still interested.