You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I really appreciate the implementation of #60370. The sql.Null[T] type improves the visibility for the explicit absence of a certain value and its mapping to the db greatly.
However, I've come to notice that sql.Null[T] behaves somewhat counterintuitive when JSON (un)marhsalling. I guess, it's pretty easy to see, even without an example. Still, as visible in this playground a null-value marshalls into: "baz":{"V":"","Valid":false}, and a non-null-value into: "bar":{"V":"bar some value","Valid":true}.
At the same time, a valid not-null JSON unmarshalls into Bar:{V: Valid:false} anyway.
I'd appreciate, if sql.Null[T] marshalls into the value of T and null respectively, and null unmarshalls into a zero T and Valid:false (of course "some value", unmarshalls into V:"some value" and Valid:true).
Since I have implemented something like this for an sql.Null[T] wrapper type myself already, I would assume this feature should be pretty straightforward.
ianlancetaylor
changed the title
proposal: database/sql: Improvement for generic sql.Null[T] JSON (un)marshalling
proposal: database/sql: add MarshalJSON and UnmarshalJSON methods for Null[T]
Jul 10, 2024
Adding encoding/decoding methods to existing types may constitute a breaking change, as they can be used for serialization in communicating with programs written with different library versions. The policy for packages maintained by the Go project is to only allow the addition of marshaling functions if no existing, reasonable marshaling exists.
Proposal Details
I really appreciate the implementation of #60370. The
sql.Null[T]
type improves the visibility for the explicit absence of a certain value and its mapping to the db greatly.However, I've come to notice that
sql.Null[T]
behaves somewhat counterintuitive when JSON (un)marhsalling. I guess, it's pretty easy to see, even without an example. Still, as visible in this playground anull
-value marshalls into:"baz":{"V":"","Valid":false}
, and a non-null
-value into:"bar":{"V":"bar some value","Valid":true}
.At the same time, a valid not-
null
JSON unmarshalls intoBar:{V: Valid:false}
anyway.I'd appreciate, if
sql.Null[T]
marshalls into the value ofT
andnull
respectively, andnull
unmarshalls into a zeroT
andValid:false
(of course "some value", unmarshalls intoV:"some value"
andValid:true
).Since I have implemented something like this for an
sql.Null[T]
wrapper type myself already, I would assume this feature should be pretty straightforward.Possible MarshalJSON/UnmarshalJSON
Thanks for your attention, have a lovely day! :)
The text was updated successfully, but these errors were encountered: