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

Custom marshalling does incorrect escaping of some control characters #475

Closed
jandubois opened this issue Sep 28, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@jandubois
Copy link

jandubois commented Sep 28, 2024

Here is a sample program that uses a custom marshaller for string that just call into the regular yaml.Marshall function, so I would expect identical results as if not using a custom marshaller at all:

package main

import (
	"fmt"
	"github.com/goccy/go-yaml"
)

type Sample struct {
	Field string `yaml:"field"`
}

func marshalString(s string) ([]byte, error) {
	return yaml.Marshal(s)
}

func main() {
	sample := Sample{Field: "#foo\a\b\f\v\n\tbar"}
	b, _ := yaml.Marshal(sample)
	fmt.Print(string(b))

	options := []yaml.EncodeOption{yaml.CustomMarshaler[string](marshalString)}
	b, _ = yaml.MarshalWithOptions(sample, options...)
	fmt.Print(string(b))
}

The results are:

field: "#foo\a\b\f\v\n\tbar"
field: "#foo\\a\b\f\v\n\\tbar"

So when the custom marshaller is used, some control character (\a and \t) are getting a double-escape.

Version Variables

Go version: 1.23.1
go-yaml's Version: 1.12.0

@jandubois jandubois changed the title Custom marshalling does some incorrect escaping of some control characters Custom marshalling does incorrect escaping of some control characters Sep 28, 2024
@goccy goccy added the bug Something isn't working label Oct 31, 2024
@jandubois
Copy link
Author

This issue has been fixed by #538 and #562. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants