pelletier/go-toml

jsontoml integer handling

schwichtgit opened this issue · 2 comments

*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

  • go-toml: latest
  • go: 1.21.0
  • operating system: MacOS (Ventura 13.5.1 (22G90)) on aarch64/arm64/M1

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

Found myself in need of this and implemented support for json.Number in #923