Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use integer number format when using Int tag #69

Closed
wants to merge 1 commit into from

Conversation

OndrejSpanel
Copy link

@OndrejSpanel OndrejSpanel commented May 30, 2019

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

@OndrejSpanel
Copy link
Author

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?

@travisbrown
Copy link
Member

@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.

@jeffmay
Copy link
Contributor

jeffmay commented Jun 27, 2019

@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.

@OndrejSpanel
Copy link
Author

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?)

@jeffmay
Copy link
Contributor

jeffmay commented Jul 1, 2019

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?

@zarthross
Copy link
Member

Closing because of build conflicts and age. Feel free to re-open or open a new PR without conflicts if you are still interested.

@zarthross zarthross closed this Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants