Skip to content

encoding/json: fails to ignore fields with more than one ignore tag #26062

Closed
@siennathesane

Description

@siennathesane

What version of Go are you using (go version)?

$ go version
go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

Yes. I can replicate this with go1.10.3 linux/amd64 and go1.10.2 windows/amd64.

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/mnt/p/go//bin"
GOCACHE="/home/mike/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/mnt/p/go/"
GORACE=""
GOROOT="/home/mike/.linuxbrew/Cellar/go/1.10.3/libexec"
GOTMPDIR=""
GOTOOLDIR="/home/mike/.linuxbrew/Cellar/go/1.10.3/libexec/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc-5"
CXX="g++-5"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build284313999=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"github.com/globalsign/mgo/bson"
	"fmt"
	"encoding/json"
)

type T1 struct {
	Foo chan string `json:"-",bson:"-"`
}

type T2 struct {
	Bar chan string `bson:"-",json:"-"`
}

type T3 struct {
	Foo chan string `bson:"-"`
}

type T4 struct {
	Bar chan string `json:"-"`
}

func main() {
	_, err := bson.Marshal(&T1{Foo: make(chan string, 0)})
	if err != nil {
		fmt.Printf("json and bson fails due to multiple ignored tags: %s\n", err)
	}
	_, err = json.Marshal(&T2{Bar: make(chan string, 0)})
	if err != nil {
		fmt.Printf("bson and json fails due to multiple ignored tags: %s\n", err)
	}
	_, err = bson.Marshal(&T3{Foo: make(chan string, 0)})
	if err == nil {
		fmt.Println("bson passes due to one ignore tag.")
	}
	_, err = json.Marshal(&T4{Bar: make(chan string, 0)})
	if err == nil {
		fmt.Println("json passes due to one ignore tag.")
	}
}

What did you expect to see?

All four use cases should work. Struct field tags denoted by "-" should be ignored by all marshallers.

What did you see instead?

json and bson fails due to multiple ignored tags: Can't marshal chan string in a BSON document
bson and json fails due to multiple ignored tags: json: unsupported type: chan string
bson passes due to one ignore tag.
json passes due to one ignore tag.

My use case is having types pull double-duty as both MongoDB objects as well as JSON objects, but I want to be able to ignore unsupported types, such as channels, with struct tags and have both struct tags be ignored properly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions