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

Marshal/Encode result of a float value without decimal part is not same with go-yaml/yaml #166

Closed
apstndb opened this issue Oct 23, 2020 · 1 comment · Fixed by #204
Closed
Labels
enhancement New feature or request

Comments

@apstndb
Copy link

apstndb commented Oct 23, 2020

Marshal result of a float value without decimal part is not same with go-yaml/yaml.

package main

import (
	goccyyaml "github.com/goccy/go-yaml"
	goyaml "gopkg.in/yaml.v3"
	"os"
)

func main() {
	goyaml.NewEncoder(os.Stdout).Encode(1.0)
	goccyyaml.NewEncoder(os.Stdout).Encode(1.0)
}
1
1.0

This behavior is not also same with JSON marshalers and not comfortable when roundtrip.

@goccy goccy added the enhancement New feature or request label Oct 30, 2020
@philomory
Copy link

philomory commented Nov 21, 2020

I'd say that both go-yaml/yaml and goccy/yaml are incorrect.

Outputing a 1 for a float value of 1.0 is incorrect, by my reading of the YAML spec; it means that float values don't round-trip correctly, because when you read it back in, it will be an int instead. Note that this isn't aesthetic; YAML is a typed file format, and an untagged 1 is an int, not a float.

goccy/yaml correctly outputs 1.0 for a float value of 1.0, but, unfortunately, it weirdly fails to support float values with more than seven digits after the decimal point: https://play.golang.org/p/n9z9jZ2dPBI, a case that go-yaml actually handles correctly.

I'd say that ideally the correct method would be to use strconv.FormatFloat the way go-yaml does, but then check if the resulting string contains a . (or scientific notation) and, if not, append .0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants