master
Charles Iliya Krempeaux 2024-08-07 00:37:51 -07:00
parent 73f29b1d15
commit fc545108d9
2 changed files with 8 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package nul
import ( import (
"encoding/json" "encoding/json"
"reflect"
"github.com/reiver/go-erorr" "github.com/reiver/go-erorr"
) )
@ -15,8 +16,10 @@ func (receiver Nullable[T]) MarshalJSON() ([]byte, error) {
case bool, string,json.Marshaler: case bool, string,json.Marshaler:
// these are OK. // these are OK.
default: default:
if reflect.Struct != reflect.TypeOf(receiver.value).Kind() {
return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is %T rather than bool, string, or json.Marshaler", receiver, receiver.value) return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is %T rather than bool, string, or json.Marshaler", receiver, receiver.value)
} }
}
if receiver.isnothing() { if receiver.isnothing() {
return nil, erorr.Errorf("nul: cannot marshal nul.Nothing[%T]() into JSON", receiver.value) return nil, erorr.Errorf("nul: cannot marshal nul.Nothing[%T]() into JSON", receiver.value)

View File

@ -2,6 +2,7 @@ package nul
import ( import (
"encoding/json" "encoding/json"
"reflect"
"github.com/reiver/go-erorr" "github.com/reiver/go-erorr"
) )
@ -21,8 +22,10 @@ func (receiver *Nullable[T]) UnmarshalJSON(data []byte) error {
case bool,string,json.Unmarshaler: case bool,string,json.Unmarshaler:
// these are OK. // these are OK.
default: default:
if reflect.Struct != reflect.TypeOf(receiver.value).Kind() {
return erorr.Errorf("nul: cannot unmarshal into something of type %T from JSON because parameterized type is %T rather than bool, string, or json.Unmarshaler", receiver, receiver.value) return erorr.Errorf("nul: cannot unmarshal into something of type %T from JSON because parameterized type is %T rather than bool, string, or json.Unmarshaler", receiver, receiver.value)
} }
}
if 4 == len(data) && 'n' == data[0] && 'u' == data[1] && 'l' == data[2] && 'l' == data[3] { if 4 == len(data) && 'n' == data[0] && 'u' == data[1] && 'l' == data[2] && 'l' == data[3] {
*receiver = Null[T]() *receiver = Null[T]()