Skip to content

json.Marshaler implementors not treated unsafe? #9

@scop

Description

@scop

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions