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

json.Marshaler implementors not treated unsafe? #9

Closed
scop opened this issue Feb 8, 2022 · 1 comment · Fixed by #11
Closed

json.Marshaler implementors not treated unsafe? #9

scop opened this issue Feb 8, 2022 · 1 comment · Fixed by #11

Comments

@scop
Copy link
Contributor

scop commented Feb 8, 2022

From README.md, things treated as unsafe:

If a type implements the json.Marshaler or encoding.TextMarshaler interface (e.g. json.Number).

For encoding.TextMarshaler it seems to hold:

package main

import (
	"encoding/json"
	"fmt"
)

type Foo int

func (s Foo) MarshalText() (text []byte, err error) {
	return []byte("\"foo\""), nil
}

type Bar struct {
	X Foo
}

func main() {
	x := Bar{X: Foo(0)}
	out, _ := json.Marshal(x)
	fmt.Println(string(out))
}

...gives

foo.go:20:12: Error return value of `encoding/json.Marshal` is not checked: unsafe type `main.Foo` found

However, for json.Marshaler it does not seem to hold; if MarshalText is changed to MarshalJSON in the example, thus making it implement json.Marshaler, no error is provoked. This is with 0.2.2.

@breml
Copy link
Owner

breml commented Feb 8, 2022

@scop Thanks for your bug reports.

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

Successfully merging a pull request may close this issue.

2 participants