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

database/sql: customize JSON marshalling for nullable fields #36496

Closed
toqueteos opened this issue Jan 10, 2020 · 7 comments
Closed

database/sql: customize JSON marshalling for nullable fields #36496

toqueteos opened this issue Jan 10, 2020 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@toqueteos
Copy link

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

$ go version
go version go1.13.6 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Carlos\AppData\Local\go-build
set GOENV=C:\Users\Carlos\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\code\go
set GOPRIVATE=
set GOPROXY=direct
set GOROOT=c:\go
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\code\sample\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Carlos\AppData\Local\Temp\go-build116244046=/tmp/go-build -gno-record-gcc-switches

What did you do?

Serialize any of the database/sql null fields as JSON using encoding/json.

https://play.golang.org/p/_sFxOknJhkH

What did you expect to see?

I expect to see all the database/sql null fields serialized as nulls when their Valid == false.

{
	"bool": null,
	"float64": null,
	"int32": null,
	"int64": null,
	"string": null,
	"time": null
}

What did you see instead?

The zero value.

{
	"bool": {
		"Bool": false,
		"Valid": false
	},
	"float64": {
		"Float64": 0,
		"Valid": false
	},
	"int32": {
		"Int32": 0,
		"Valid": false
	},
	"int64": {
		"Int64": 0,
		"Valid": false
	},
	"string": {
		"String": "",
		"Valid": false
	},
	"time": {
		"Time": "0001-01-01T00:00:00Z",
		"Valid": false
	}
}
@toqueteos
Copy link
Author

This can be solved by implementing MarshalJSON (and also UnmarshalJSON) but I think it qualifies as breaking change so it'd go to Go2. Can somebody confirm it?

@toothrot toothrot changed the title encoding/json: handle nullable fields from database/sql database/sql: customize JSON marshalling for nullable fields Jan 10, 2020
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 10, 2020
@toothrot toothrot added this to the Backlog milestone Jan 10, 2020
@toothrot
Copy link
Contributor

I think the current behavior might be the least surprising to me. Null fields marshal like any normal Go struct. I believe leaving the conversion of the data to the caller's target format is best left up to the caller.

/cc @bradfitz @kardianos

@kardianos
Copy link
Contributor

@toothrot

Please observe https://github.com/golang-sql/table . You can get custom table marshaling of arbitrary tables using this method.

@kardianos
Copy link
Contributor

We won't add a MarshalJSON method to Rows. I will be closing this issue.

@toqueteos
Copy link
Author

@kardianos I'm sorry but I don't understand, what has Rows to do with this issue?

@kardianos
Copy link
Contributor

Looking at your play link, I see more clearly what you mean. You would like to define the methods on the Nullable structs.

Yeah, that would likely be a backwards in-compatible change.

@toqueteos
Copy link
Author

I'm sad to hear that but I understand. Thanks!

@golang golang locked and limited conversation to collaborators Jan 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants