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

jsontoml integer handling #897

Closed
schwichtgit opened this issue Sep 5, 2023 · 2 comments · Fixed by #923
Closed

jsontoml integer handling #897

schwichtgit opened this issue Sep 5, 2023 · 2 comments · Fixed by #923
Labels
bug Issues describing a bug in go-toml.

Comments

@schwichtgit
Copy link

schwichtgit commented Sep 5, 2023

*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
@pelletier pelletier added the bug Issues describing a bug in go-toml. label Sep 8, 2023
@pelletier
Copy link
Owner

pelletier commented Sep 8, 2023

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
Copy link
Contributor

dangra commented Jan 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues describing a bug in go-toml.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants