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

encoding/json: wrong encoding for string field with integer option (struct tag) #41134

Closed
ihsanturk opened this issue Aug 30, 2020 · 3 comments
Closed

Comments

@ihsanturk
Copy link

@ihsanturk ihsanturk commented Aug 30, 2020

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

$ go version
go version go1.14.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ihsan/Library/Caches/go-build"
GOENV="/Users/ihsan/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ihsan/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/j2/b4qmbgs56hsc2flg0gpkl66c0000gn/T/go-build330661112=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import ( "fmt"; "encoding/json"; )

type Tweet struct {
	Id string `json:"_id,integer"`
};

func main() {
	json, _ := json.Marshal(Tweet{ Id: "42" })
	fmt.Println(string(json))
}

What did you expect to see?

{"_id":42}

What did you see instead?

{"_id":"42"}

@mvdan
Copy link
Member

@mvdan mvdan commented Aug 30, 2020

Where did you find that ,integer option? It's not documented in https://golang.org/pkg/encoding/json/, and as far as I can tell it does not exist.

@ihsanturk
Copy link
Author

@ihsanturk ihsanturk commented Aug 30, 2020

The "string" option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types.

I thought that this means it supports the ,integer option, so I am wrong. Better I study English instead.

My actual problem is, I need to use my _id field as string in my go code and as integer when I am inserting to MongoDB (It uses struct tag I guess): coll.InsertOne(ctx, myTweetStructInstance)

In that case, should I create a clone of that struct with the wrong field type fixed and use the cloned struct just for inserting to MongoDB?

@mvdan
Copy link
Member

@mvdan mvdan commented Aug 30, 2020

Yes, you could use a different type. For questions, see https://golang.org/wiki/Questions.

@mvdan mvdan closed this Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.