-
Notifications
You must be signed in to change notification settings - Fork 18k
database/sql: customize JSON marshalling for nullable fields #36496
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
Comments
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? |
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 |
Please observe https://github.com/golang-sql/table . You can get custom table marshaling of arbitrary tables using this method. |
We won't add a MarshalJSON method to Rows. I will be closing this issue. |
@kardianos I'm sorry but I don't understand, what has Rows to do with this issue? |
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. |
I'm sad to hear that but I understand. Thanks! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Serialize any of the
database/sql
null fields as JSON usingencoding/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 theirValid == false
.What did you see instead?
The zero value.
The text was updated successfully, but these errors were encountered: