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
Milestone

Comments

@toqueteos
Copy link

@toqueteos toqueteos commented Jan 10, 2020

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

@toqueteos toqueteos commented Jan 10, 2020

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 this to the Backlog milestone Jan 10, 2020
@toothrot
Copy link
Contributor

@toothrot toothrot commented Jan 10, 2020

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

@kardianos kardianos commented Jan 24, 2020

@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

@kardianos kardianos commented Jan 24, 2020

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

@kardianos kardianos closed this Jan 24, 2020
@toqueteos
Copy link
Author

@toqueteos toqueteos commented Jan 24, 2020

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

@kardianos
Copy link
Contributor

@kardianos kardianos commented Jan 24, 2020

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

@toqueteos toqueteos commented Jan 24, 2020

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

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
3 participants
You can’t perform that action at this time.