config icon indicating copy to clipboard operation
config copied to clipboard

double value (e.g., 1.0) is translated to ConfigInt(1) instead of ConfigDouble(1.0)

Open kyunam opened this issue 6 years ago • 4 comments
trafficstars

val config = ConfigFactory.parseString(
      """
        |decoders = [
        | { a : 1.0 },
        | { b : 2.2 },
        | { c : 3.3 }
        |]
      """.stripMargin)

    val decoders = config.getObjectList("decoders")
    decoders.foreach(co => println(co.toConfig.entrySet()))

prints

[a=ConfigInt(1)] [b=ConfigDouble(2.2)] [c=ConfigDouble(3.3)]`

I'm expecting [a=ConfigDouble(1.0)].

Thanks, Kyunam

kyunam avatar Jan 18 '19 05:01 kyunam

In ConfigNumber.newNumber it chooses an integer representation if it would be lossless.

In general if you depend on the parsed type (vs the gettable type with getDoubleList) some uses of the library will break, such as the ability to override values with env vars and properties. So if possible avoid relying on this.

I’m not sure of the implications of changing how it works now, it’s possible we could.

havocp avatar Jan 18 '19 13:01 havocp

Thank you Havoc. It would be awesome if it could be changed, then I’d feel very com.typesafed :)

Thanks again.

kyunam avatar Jan 18 '19 17:01 kyunam

I also met this problem and it made me confused a lot. The situation is when to parse configurations dynamically from a file, I know neither the key names nor the value types ,both can change dynamically, so I can't use the the methods like getInt or getDouble and during runtime, this problem caused 'java int cannot be converted to double' error. I don't know whether other literals will be parsed like that, which makes me quite unconfident with the com.typesafe.config.

yinshurman avatar Oct 21 '19 08:10 yinshurman

Same issue for dynamically loaded config. #581 (originalText) could help if it's quicker

ckorakidis avatar Sep 04 '20 00:09 ckorakidis