We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
From the Gitter conversation.
The Decoders for Int, Long and other integral types try to convert a string into their number type.
Int
Long
import io.circe._, parse._, cats.data.Xor scala> val Xor.Right(i) = decode[Int]("\"42\"") i: Int = 42
The decoder for Double and Float don't try this conversion because of this:
Double
Float
scala> "Infinity".toDouble res1: Double = Infinity
However, we could use JsonNumber.fromString and _.toDouble to try to safely convert a String into a Double, for example:
JsonNumber.fromString
_.toDouble
scala> val doubleWithFromString = Decoder.instance(c => c.as[String].flatMap(s => Xor.fromOption(JsonNumber.fromString(s).map(_.toDouble), DecodingFailure("Double", c.history)))) doubleWithFromString: io.circe.Decoder[Double] = io.circe.Decoder$$anon$8@47016c8b scala> val Xor.Right(d) = decode("\"13.37\"")(doubleWithFromString) d: Double = 13.37 scala> val Xor.Left(e) = decode("\"Infinity\"")(doubleWithFromString) e: io.circe.Error = io.circe.DecodingFailure: Double scala> val Xor.Left(e) = decode("\"NaN\"")(doubleWithFromString) e: io.circe.Error = io.circe.DecodingFailure: Double
If "Infinty" and "NaN" are valid to be parsed as Double, one can fallback to using a Decoder[String].map(_.toDouble) approach.
"Infinty"
"NaN"
Decoder[String].map(_.toDouble)
The text was updated successfully, but these errors were encountered:
I think this is a good idea!
Sorry, something went wrong.
Fixed in #189.
Merge pull request circe#173 from scala-steward/update/play-json-2.7.4
e82f617
Update play-json to 2.7.4
No branches or pull requests
From the Gitter conversation.
The Decoders for
Int
,Long
and other integral types try to convert a string into their number type.The decoder for
Double
andFloat
don't try this conversion because of this:However, we could use
JsonNumber.fromString
and_.toDouble
to try to safely convert a String into a Double, for example:If
"Infinty"
and"NaN"
are valid to be parsed as Double, one can fallback to using aDecoder[String].map(_.toDouble)
approach.The text was updated successfully, but these errors were encountered: