Option: fix nullable types getting serialized to nil on JSON null

This commit is contained in:
mavolin 2020-11-22 18:31:52 +01:00 committed by diamondburned
parent fc5d10ced8
commit b9d7ba4476
4 changed files with 6 additions and 1 deletions

View File

@ -49,6 +49,7 @@ func (b *NullableBoolData) UnmarshalJSON(json []byte) (err error) {
s := string(json)
if s == "null" {
*b = *NullBool
return
}

View File

@ -58,6 +58,7 @@ func (i *NullableColorData) UnmarshalJSON(json []byte) error {
s := string(json)
if s == "null" {
*i = *NullColor
return nil
}

View File

@ -56,6 +56,7 @@ func (u *NullableUintData) UnmarshalJSON(json []byte) error {
s := string(json)
if s == "null" {
*u = *NullUint
return nil
}
@ -99,6 +100,7 @@ func (i *NullableIntData) UnmarshalJSON(json []byte) error {
s := string(json)
if s == "null" {
*i = *NullInt
return nil
}

View File

@ -22,7 +22,7 @@ type NullableStringData struct {
Init bool
}
// NullBool serializes to JSON null.
// NullString serializes to JSON null.
var NullString = &NullableStringData{}
// NewNullableString creates a new non-null NullableString with the value of the passed string.
@ -42,6 +42,7 @@ func (s NullableStringData) MarshalJSON() ([]byte, error) {
func (s *NullableStringData) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
*s = *NullString
return nil
}