pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.67k stars 208 forks source link

jsontoml integer handling #897

Closed schwichtgit closed 5 months ago

schwichtgit commented 10 months ago

*The Issue It seems that jsontoml converts integer to float values.

How To Reproduce

cat << EOF > example.toml
title = 'ImpalaPay Co.'

[clients]
data = [['gamma', 'delta'], [1, 2]]
hosts = ['alpha', 'omega']

[database]
connection_max = 5000
enabled = true
ports = [8000, 8001, 8002]
server = '192.168.1.1'

[owner]
establishment = ''
name = 'Impala Co.'

[servers]
[servers.alpha]
dc = 'eqdc10'
ip = '10.0.0.1'

[servers.beta]
dc = 'eqdc10'
ip = '10.0.0.2'
EOF

$ tomljson example.toml | jsontoml > example2.toml

$ diff -Naur example.toml example2.toml

--- example.toml    2023-09-05 17:08:50.818475984 -0400
+++ example2.toml   2023-09-05 17:10:02.074124881 -0400
@@ -1,13 +1,13 @@
 title = 'ImpalaPay Co.'

 [clients]
-data = [['gamma', 'delta'], [1, 2]]
+data = [['gamma', 'delta'], [1.0, 2.0]]
 hosts = ['alpha', 'omega']

 [database]
-connection_max = 5000
+connection_max = 5000.0
 enabled = true
-ports = [8000, 8001, 8002]
+ports = [8000.0, 8001.0, 8002.0]
 server = '192.168.1.1'

 [owner]

Expected behavior example.toml example2.toml should be identical, the diff should be empty.

Versions

pelletier commented 10 months ago

Thanks for the report! I am going to mark this as a bug. The issue is that Go's encoding/json unmarshals JSON numbers into floats by default. We can do better by asking it to UseNumber instead.

Just want to note: it's not a design goal to guarantee no byte change when getting a document through tomljson | jsontoml.

dangra commented 5 months ago

Found myself in need of this and implemented support for json.Number in https://github.com/pelletier/go-toml/pull/923